简体   繁体   中英

PHPMailer Using Gmail Not Sending

I am using PHP Mailer ( https://github.com/PHPMailer/PHPMailer ) and for some reason it is not working with gmail. Does anyone know if there are any other settings I should be using?

 function send_email($mail_content,$subject,$email,$name)
        {
            include_once('third-party/mail/phpmailer-master/PHPMailerAutoload.php');     
            $php_mailer_mail = new PHPMailer;   
            $php_mailer_mail->isSMTP();
            $php_mailer_mail->Host = 'smtp.gmail.com';
            $php_mailer_mail->SMTPAuth = true;
            $php_mailer_mail->Username = 'email@gmail.com';
            $php_mailer_mail->Password = 'password';
            $php_mailer_mail->SMTPSecure = 'ssl';
            $php_mailer_mail->Port = 465;
            $php_mailer_mail->From = 'email@gmail.com';
            $php_mailer_mail->FromName = 'Me';
            $php_mailer_mail->addAddress($email, $name);  
            $php_mailer_mail->isHTML(true);
            $php_mailer_mail->Subject = $subject;
            $php_mailer_mail->Body = $mail_content;
            $php_mailer_mail->send();
        }

Make sure you enable IMAP in your gmail settings. smtp is not IMAP, but Google will discard smtp requests for your account unless that is enabled in your account settings.

You may also want to try port 587 with TLS encryption.

Do you get any errors?

if $php_mailer_mail->send(); returns false you may inspect $php_mailer_mail->ErrorInfo

Anyway you should use port 587 and tls instead of ssl .

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

You may take a look at

https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Furthermore (from the example at the link above)

SMTP needs accurate times, and the PHP time zone MUST be set This should be done in your php.ini, but this is how to do it if you don't have access to that

date_default_timezone_set('Etc/UTC');

This line must be at the beginning of the script. Of course double-check username and password.

Hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM