Adding a SMTP mailer to your form

May be you had a situation that you wanted to send a mail from your form using SMTP not the regular mail() function, in my case i had to use smtp to send mails as my emails delay getting mail from the regular mail() function and sometimes the mail doesn’t reach the receipt

I used PHPMailer to do that, it’s easy to setup and solve this problem .

Firstly you have to upload the folder to your host and create a file with any name ( in my case called mail.php ) and insert the following code in it .

you have to include the path to PHPMailerAutoload.php path in the script

</pre>
require 'path/to/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP
 $mail->Host = 'hostname'; // Specify main and backup server
 $mail->Port = 465; // your port
 $mail->SMTPAuth = true; // Enable SMTP authentication
 $mail->Username = 'email@domain.com'; // SMTP username
 $mail->Password = 'password here'; // SMTP password
 $mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted

$mail->From = 'email@domain.com';
 $mail->FromName = 'Name';
 $mail->addAddress('email1@domain.com', 'email2@domain.com'); // Add a recipient
 //$mail->addAddress('email@domain.com'); // Name is optional
 $mail->addReplyTo('email@domain.com', 'Information'); // reply to
 //$mail->addCC('email@domain.com');
 //$mail->addBCC('email@domain.com');

$mail->WordWrap = 50; // Set word wrap to 50 characters
 //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
 //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
 $mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'Subject';
 $mail->Body = "Body content here";
 $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
 echo 'Message could not be sent.';
 echo 'Mailer Error: ' . $mail->ErrorInfo;
 exit;
 }

echo 'Message has been sent';

}
else
{
 header('Location: http://www.future-trans.com/');
 exit;
}
<pre>
Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: