如何让PHPMailer使用Gmail的SMTP发送电子邮件?

时间:2017-05-07 20:45:45

标签: php phpmailer windows-server-2012-r2

我已经将PHPMailer安装到Windows Server 2012 R2上(使用IIS),我正在尝试使用它来发送电子邮件,这是我的代码:

<?php

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                        // Enable verbose debug output

$mail->isSMTP();                               // Set mailer to use SMTP
$mailer->Host = 'tls://smtp.gmail.com';        // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                        // Enable SMTP authentication
$mail->Username = 'myemailaddress@gmail.com';  // SMTP username
$mail->Password = 'MyPassword';                // SMTP password
$mail->SMTPSecure = 'tls';                     // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                             // TCP port to connect to

$mail->setFrom('myemailaddress@gmail.com', 'Martin');
$mail->addAddress('anotheraddress@emaildomain.co.uk', 'Hello');   // Add a recipient
//$mail->addAddress('ellen@example.com');      // Name is optional
$mail->addReplyTo('anotheraddress@midlandairexpress.co.uk', 'Hello');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                    // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent';
}

?>

正如你所看到我启用了调试输出,当我尝试运行它时,我得到:

     2017-05-07 20:36:28    Connection: opening to localhost:587, timeout=300, options=array ( ) 2017-05-07 20:36:30    Connection failed. Error #2: stream_socket_client(): unable to connect to localhost:587 (No connection could be made because the target machine actively refused it. ) [C:\inetpub\wwwroot\email\PHPMailer\class.smtp.php line 292] 2017-05-07 20:36:30 SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it. (10061) 2017-05-07 20:36:30    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我已经安装了PHP screenshot of info

我试过了:
1.浏览GitHub文档
2.在SMTP服务器名称之前指定协议(第10行)
3.根据Google支持页面检查SMTP信息 4.检查我的电子邮件地址的凭据(发送自己)

PHPMailer文件夹位于服务器上名为电子邮件

的文件夹中
 (Path to PHPMailerAutoload.php)
 C:\inetpub\wwwroot\email\PHPMailer\PHPMailerAutoLoad.php

我对PHP很新,但我对它是如何工作有基本的了解。 有没有人有任何建议?

0 个答案:

没有答案