Search code examples
phpemailsmtpgmailphpmailer

Cannot send email using PHPMailer


I am trying to send an email using phpmailer. This is the code that I have written.

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = '*********';
    $mail->Port = 25;

    $mail->From = '[email protected]';
    $mail->FromName = 'Shamir Towsif';
    $mail->addAddress('[email protected]', 'Shamir Towsif');
    $mail->addReplyTo('[email protected]', 'Information');

    $mail->isHTML(true);

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo "Message could not be sent.\n";
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

Here is the error that I am getting.

Message could not be sent. Mailer Error: SMTP connect() failed.

What am I doing wrong?


Solution

  • I am facing a similar problem, but I think you should try adding this to your code:

    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    

    This is PHPMailer recommended settings for GMail, you can see an example in their Github page .