- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Send an email through PHP is just simple as same as you turn back your both hands.
haha...
In this post, I assumed that you already understand about basic of PHP and POST method.
Okay, first of all, you have to create a HTML form first with this code below :
form.html
send.php
Quite easy, huh?:D
You can add additional field in above form.
ex: Destination email address
Hope this will help you. :)
haha...
In this post, I assumed that you already understand about basic of PHP and POST method.
Okay, first of all, you have to create a HTML form first with this code below :
form.html
<form method="post">
<fieldset>
<legend>Email</legend>
<br />
<div class="form-group">
<label class="col-lg-2 control-label">Subject</label>
<br />
<div class="col-lg-5">
<input class="form-control" id="inputSubject" name="subject" placeholder="Email Subject" type="text" />
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="textArea">Message</label>
<br />
<div class="col-lg-8">
<textarea class="form-control" id="msg" name="msg" rows="3"></textarea>
<span class="help-block">Please type your message.</span>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="button">Send</button>
</div>
</div>
</fieldset>
</form>
send.php
require_once "Mail.php";
$to = "destination@gmail.com";
$from = "your_name@gmail.com";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "your_name@gmail.com";
$password = "your_password";
$subject = $_POST['subject'];
$body = $_POST['msg'];
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'text/html; charset="UTF-8';
$headers['Content-Transfer-Encoding'] = '7bit';
$headers['Date'] = date('r', $_SERVER['REQUEST_TIME']);
$headers['Message-ID'] = $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'];
$headers['From'] = $from;
$headers['To'] = $to;
$headers['Subject'] = $subject;
$smtp = Mail::factory('smtp', array('host' => $host,
'port' => $port, 'auth' => true,
'username' => $username, 'password' => $password));
$mail = $smtp -> send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<script language='javascript'>alert('" . $mail -> getMessage() . "');history.back();</script>");
}else{
echo "<script language='javascript'>alert('Success!');history.back();</script>";
}
Quite easy, huh?:D
You can add additional field in above form.
ex: Destination email address
Hope this will help you. :)
- Get link
- X
- Other Apps
Comments
Post a Comment
Please leave your comment politely and do not write a spam message.
Thank you. :)