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

<pre>
    <?php
    $to = 'info@edmondscommerce.co.uk';
    $message = 'testing swift mailer';

    require("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;

    $mail->Username = 'your@email.com';
    $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
//To
    $mail->AddAddress($to);
    $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;
        exit;
    }
    echo "Message has been sent";

Tags: googlephpbuggmailemailsmtpappstlsphpmailerheloSTARTTLS