如何在 xampp 上使用 phpmailer 发送邮件

how to send mail using phpmailer on xampp

我一直在尝试在 xampp 上使用 php 邮件程序发送邮件,但确实收到此错误提示 无法发送消息。邮件程序错误:以下发件人地址失败:xxxx2@gmail.com:在未连接的情况下调用了 Mail()

拜托,我需要有关如何解决此问题的帮助。 这是我的代码;

<?php
require( 'class.phpmailer.php' );

  $mail = new PHPMailer;
  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->Host = "tls://smtp.gmail.com";
  $mail->Port = 25;
  $mail->Username = "xxxx@gmail.com";
  $mail->Password = "xxxxx";
  //Sending the actual email
  $mail->setFrom('xxxx2@gmail.com', 'Aaron');
  $mail->addAddress('xxxx2@gmail.com', 'Aaron');     // Add a recipient
  $mail->isHTML(false);                                  // Set email format to HTML
  $mail->Subject = 'Calculation form results from ';
  $mail->Body = 'testing...';

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

字面复制自https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "yourpassword";

//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');

我尝试使用 Composer 和 github,但无法通过 xampp 获取最新版本的 phpmailer 来处理我的测试 php 电子邮件。当我 运行 本地主机上的程序时,它一直崩溃,因为电子邮件代码正在寻找 php 邮件程序 class。 所以我回到 google,忽略了 composer,并下载了 php mailer 5.2.0 的一个普通的普通 zip,并将这个 zip 直接解压到我位于 htdocs 中的 'websiteX' testing 文件夹中在 xampp.

在我的 'websiteX' 文件夹中,我有我的 testmail.php 文件以及我的 php 邮件程序解压缩文件夹,它包含 class.phpmailer,它实际上适用于我的情况。

我花了一个星期的时间胡思乱想,但现在我有 xampp php 封电子邮件完美地发送到我的测试 gmail 帐户。我也用 chrome 和 notepad++。

有趣的是,我收到了 php 封使用 php mail() 命令的电子邮件,尽管 Gmail 很难退回它们,这不是很好。上周我做的第一件事是让 Mercury 邮件(包含在 xampp 中)正常工作。我遵循此 link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows 并设法 xampp 与 gmail 通信,这太棒了!

祝你编码顺利。