PHP : How to use phpmailer to send email with smtp

phpmailer with smtp for php

Now as per email authentication some server does not allow to send email without smtp. This  is used to reduce bulk email and spamming.  In PHP phpmailer is widely use to send email with SMTP.  I am trying to explain in simple way.

Step -1 : Download phpmailer from

Step- 2 : Use given below SMTP script



require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP

//Enable SMTP debugging
//$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";

Note :

You may have to  change smtp user , password and host name as per your server configuration.

Perhaps SMTP port may not need to change from 25. Because in most of cased it will be same. However you also check port number.

If you use cpanle, then you can find these detail from “CONFIGURE  MAIL CLIENT” link under email account list page.

I hope this will help you to send email. You can also try ssl option. For that you may need to change port number.