Correct settings for PHPMailer via a gmail account

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 00:25:46

问题


I am trying to set up a simple email function on my site with PHPMailer. I am testing out using my own gmail account and simply sending a test email to myself. I am running MAMP PRO on a Mac. I have seen loads of questions on this and spent an afternoon researching it but everything I have seen comes down to conflicting advice on the correct settings, use port 487 versus 465 etc etc. I have tried multiple combinations of the various settings below ($mail->SMTPSecure = 'tls' with port 587, but 'ssl' for port 465 for example. I keep getting this connection error. If anyone has some thoughts on whats wrong with this I would be very grateful. I have remmebered to include the relevant files in my code.

 mail = new PHPMailer();
 $mail->IsSMTP();  
 $mail->Host     = "smtp.gmail.com"; 
 $mail->From     = "me@gmail.com";
 $mail->AddAddress("me@gmail.com");
 $mail->SMTPAuth = true;
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;
 $mail->username = "me@gmail.com";
 $mail->password = "mypassword";
 $mail->Subject  = "First PHPMailer Message";
 $mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
 $mail->WordWrap = 50;

 if(!$mail->Send()) {
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
 } else 
   echo 'Message has been sent.';
 }

The error message is - "Message was not sent.Mailer error: SMTP connect() failed."


回答1:


Your code looks good - I'd definitely go with TLS on 587. This error is usually down to the environment you're running in - check these things:

  • That your DNS is working (try substituting an IP for smtp.gmail.com)
  • That stream_socket_client or fsockopen (or fopen wrappers in general) is not blocked or disabled in php.ini
  • Check that your firewall isn't blocking you
  • Read the troubleshooting docs


来源:https://stackoverflow.com/questions/24310003/correct-settings-for-phpmailer-via-a-gmail-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!