以下代码适用于我的xampp本地服务器,但不会在远程主机上发送电子邮件。我收到这个错误:
无法发送消息.Mailer错误:SMTP连接()失败
require_once('header.php');
require_once('PHPMailer/PHPMailerAutoload.php');
function sendMail($address, $message){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mymail@gmail.com'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('mymail@gmail.com', 'ID Test'); // Add a recipient
$mail->addAddress($address); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Twitter search';
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
}
答案 0 :(得分:4)
在使用设置为TLS身份验证协议和端口号587的Gmail服务器时,PHPMailer过去遇到了麻烦。我不记得那个组合曾经为我工作过。但是,我使用SSL / 465时从来没有遇到任何问题。
而不是:
// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
试试这个:
// Updated Settings
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
更多信息: