Unable to send email using Google Apps account using PHPMailer

Please note that I am using a Google Apps account and not a gmail account. I am trying to just send an email using my Google Apps account using php. I can send an email in a .net application using port 587 host smtp.googlemail.com and SSL. Username is my full email address.

require_once('PHPMailer_v5.1\class.phpmailer.php');

try {
    $mail  = new PHPMailer();
    $mail->Mailer   = 'smtp';
    $mail->SMTPSecure = 'tls';
    $mail->Host     = $host;
    $mail->Port     = 587;
    $mail->SMTPAuth = true;
    $mail->Username = $from;
    $mail->Password = $password;

    $mail->AddAddress($to, $to_name);   
    $mail->From       = $from;
    $mail->FromName   = $from_name;
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $mail->IsHTML(true);

    $mail->Send();
} catch (phpmailerException $e) {
    echo $e->errorMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}

Failed to get this to work, but I tried several different variations of this.

$mail->SMTPSecure = 'ssl'; 
$mail->Port     = 465;
// Error: Could not connect to SMTP host. This is expected as this isn't supported anymore.

$mail->SMTPSecure = 'tls';
$mail->Port     = 587;
// Takes forever, then I get "this stream does not support SSL/crypto PHPMailer_v5.1\class.smtp.php"

I don't care, but I need to send an email using gmail. It can be with this library or another.

+3
source share
1 answer

, -, .

    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }

. - PHPMailer SwiftMailer, gmail (http://www.swiftmailer.org/wikidocs/v3/connections/smtp)

+6

All Articles