PHP: Send Mail From SMTP Server Authentication Required

The native PHP’s mail() fucntion doesn’t allow you to chose the SMTP server of your choice, nor does it support SMTP authentication.

Fortunately, you can use the free PHPMailer email sending library to overcome PHP’s built-in shortcomings.

Here is a sample usage:

require_once('/library/PHPMailer/PHPMailerAutoload.php');
$mail = new \PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = 'login';
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'example@gmail.com';
$mail->Password = 'somepassword';
$mail->SetFrom('example@gmail.com', 'Example');
$mail->Subject = 'The subject';
$mail->Body = 'The content';
$mail->IsHTML(true);
$mail->AddAddress('receiver@gmail.com');
$mail->Send();

4 Replies to “PHP: Send Mail From SMTP Server Authentication Required”

  1. This is not happening beceause google disableing the unauthorized login authtencation.

    1. https://www.google.com/settings/security/lesssecureapps

    go to this page login and make it enable.

    2. code is bit change,
    isSMTP(); // Set mailer to use SMTP
    $mail->Host = ‘smtp.gmail.com’; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = ‘88888@gmail.com’; // SMTP username
    $mail->Password = ‘***********’; // SMTP password
    $mail->SMTPSecure = ‘ssl’; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465; // TCP port to connect to

    $mail->From = ‘88888@gmail.com’;
    $mail->FromName = ‘Mailer’;
    $mail->AddAddress (“ur recipent@email id.com”);// Add a recipient

    $mail->WordWrap = 50; // Set word wrap to 50 characters
    // Optional name
    $mail->isHTML(true); // Set email format to HTML

    $mail->Subject = ‘Here is the subject’;
    $mail->Body = ‘This is the HTML message body in bold!‘;
    $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;
    } else {
    echo ‘Message has been sent’;
    }

    ?>

Post a Comment