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

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 = ''; // SMTP username
 $mail->Password = 'password here'; // SMTP password
 $mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted

$mail->From = '';
 $mail->FromName = 'Name';
 $mail->addAddress('', ''); // Add a recipient
 //$mail->addAddress(''); // Name is optional
 $mail->addReplyTo('', 'Information'); // reply to

$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;

echo 'Message has been sent';

Leave a comment

Leave a Reply

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

You are commenting using your 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: