问题
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
orfsockopen
(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