PHP Swift Mailer Not Working with Google Apps

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

If you are struggling trying to get PHPMailer working with Google Apps then perhaps this is the solution for you.

I went to the PHPMailer site and found my way to a SourceForge download. After a lot of debugging and wrestling with this I just could not get it to work. In the end I looked at other projects that I knew were using PHPMailer for their mail and that were working with Google Apps or Gmail and found that their version was drastically more up to date than the one I downloaded from SourceForge.

In a nutshell - the version I had was never going to work.

A quick update later and I have PHPMailer working with Google Apps.

For reference the version here seems to be the up to date.

Here is some example code that IS working with Google Apps

    $to = '';
    $message = 'testing swift mailer';


    $mail = new PHPMailer();

    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = '';
    $mail->Port = 587;

    $mail->Username = '';
    $mail->Password = 'yourpasswordwhatever';

    $mail->From = $to;
    $mail->FromName = 'test';
    $mail->Subject = 'test';
    $mail->WordWrap = 50; // set word wrap

    $mail->Body = eregi_replace("[\]", '', $message);

    $mail->IsHTML(true); // send as HTML
    $mail->SMTPDebug = true;

    $mail->CharSet = 'us-ascii';
    $mail->Encoding = $mail->Charset != 'us-ascii' ? '8bit' : '7bit';

    if (!$mail->Send()) {
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
    echo "Message has been sent";

Tags: googlephpbuggmailemailsmtpappstlsphpmailerheloSTARTTLS