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:

$mail = new \PHPMailer();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = 'login';
$mail->SMTPSecure = 'ssl';
$mail->Host = '';
$mail->Port = 465;
$mail->Username = '';
$mail->Password = 'somepassword';
$mail->SetFrom('', 'Example');
$mail->Subject = 'The subject';
$mail->Body = 'The content';

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

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


    go to this page login and make it enable.

    2. code is bit change,
    isSMTP(); // Set mailer to use SMTP
    $mail->Host = ‘’; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = ‘’; // 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 = ‘’;
    $mail->FromName = ‘Mailer’;
    $mail->AddAddress (“ur recipent@email”);// 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