Send Email with PHPMailer using SMTP

Posted on June 13, 2018 at 2:47 pm

PHPMailer is the most popular PHP email sending library:
https://github.com/PHPMailer/PHPMailer

Here is a simple function used to send an email using SMTP:

function phpmailer_send($subject, $from_name, $from_email, $message, $to) 
{
    require_once 'path/to/php-mailer/PHPMailerAutoload.php';
    $mail  = new PHPMailer();   
    $mail->IsSMTP();
    $mail->SMTPAuth = true;   
    $mail->SMTPDebug = 0;               
    $mail->SMTPSecure = "ssl";   
 
    // Example ssl0.ovh.net
    $mail->Host = "YOUR_SMTP_HOST_ADDRESS";
 
    $mail->Port = 465;                   
    $mail->Username = "YOUR_USERNAME"; 
    $mail->Password = "YOUR_PASSWORD";           
    $mail->From = $from_email;
    $mail->FromName = $from_name;
    $mail->Subject = $subject;
    $mail->MsgHTML( $message );
    $mail->AddAddress( $to );
    $mail->IsHTML( true ); 
 
    // Hide the XMailer for security reasons
    $mail->XMailer = ' ';
 
    return ( $mail->Send() ) ? true : false;
}

Usage is very simple:

$ret = phpmailer_send( "Subject", "From Name", "from@email.com", "HTML Message", "to@email.com" );
echo ( $ret ) ? "SENT" : "FAILED";

Receive updates via email

Other Posts

Updated Posts