【问题标题】:Sending email with PHPMailer使用 PHPMailer 发送电子邮件
【发布时间】:2020-12-14 15:18:11
【问题描述】:

我遵循了关于如何使用 PHPMailer 发送电子邮件的 yt 教程。这样做之后,我得到了下面的代码,但它对我不起作用。我没有收到任何错误消息,也没有收到任何电子邮件。这可能是什么原因?我错过了什么吗?我使用 XAMPP 作为本地服务器来运行我的代码。

require_once ('PHPMailer/PHPMailerAutoload.php');

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->isHTML();
$mail->Username = 'sender@gmail.com';
$mail->Password = 'password';
$mail->SetFrom('no-reply@example.com');
$mail->Subject = 'Hello World';
$mail->Body = 'A test email!';
$mail->AddAddress('reciever@gmail.com');

$mail->Send();

【问题讨论】:

  • 看起来那个教程已经过时了,你没有调试输出也没有错误检查/报告,所以你不知道发生了什么。 PHPMailer 提供的示例将为您提供更好的起点。
  • 我建议您使用 PHPMailer 提供的 gmailcontact form 示例重新开始。如果您遇到问题,请阅读the troubleshooting guide。如果您是 PHP 开发新手,那么您能做的最好的事情就是学习如何使用 Composer。
  • @Synchro 所以我尝试了一些新的东西并收到错误消息“SMTP connect() failed”。我找到了这个github.com/PHPMailer/PHPMailer/wiki/… 并想尝试一下。但是我不明白我应该对句子 Insert code 下面的代码行做什么。你知道该怎么做吗?
  • 忽略该代码。这是错误的建议。您已经可以从外部在 OAuth 客户端上设置选项,无需更改任何 PHPMailer 代码。此外,这与“connect() failed`”错误无关,因为这将在验证之前发生。请遵循指南中的建议,以便您了解正在发生的事情。

标签: php email phpmailer


【解决方案1】:

https://github.com/PHPMailer/PHPMailer 下载php mailer 并使用它。 要使用它:首先您需要创建一个名为“phpmailer”的文件夹。然后把你下载的php mailer文件夹放进去。 然后使用此代码:

<?php
    header("Access-Control-Allow-Origin: *");
    header('Access-control-Allow-Headers: Authorization,Content-Type ,X-Auth-Token , Origin');
   $username=$_POST["email"];
   $password=$_POST["password"];
   $to=$_POST["to"];
   $subject=$_POST["subject"];
   $body=$_POST["body"];

   // Import PHPMailer classes into the global namespace
   // These must be at the top of your script, not inside a function
   use PHPMailer\PHPMailer\PHPMailer;
   use PHPMailer\PHPMailer\SMTP;
   use PHPMailer\PHPMailer\Exception;

   // Load Composer's autoloader
   require 'phpmailer/src/PHPMailer.php';
   require 'phpmailer/src/SMTP.php';
   require 'phpmailer/src/Exception.php';

   // Instantiation and passing `true` enables exceptions
   $mail = new PHPMailer(true);

   try {
    //Server settings
     $mail->SMTPDebug = 2;                      // Enable verbose debug output
     $mail->isSMTP();                                            // Send using SMTP
     $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP    server    to send through
     $mail->SMTPAuth   = true;                                   // Enable SMTP      authentication
    $mail->Username   = $username;                     // SMTP username
    $mail->Password   = $password;                               // SMTP password
    $mail->SMTPSecure = 'tls';         // Enable TLS encryption; `  PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
   $mail->setFrom('no-reply@gmail.com',$username);
   $mail->addAddress($to);     // Add a recipient


   // Attachments
   // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
   // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

   // Content
   $mail->isHTML(true);                                  // Set email format to HTML
   $mail->Subject = $subject;
   $mail->Body    = $body;
   //$mail->AltBody =;

   $mail->send();

   echo "<script>";
   echo "window.alert('Email was sent')";
   echo "</script>";


  } catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
 ?>

另外,请确保您开启“不太安全的应用访问”以使其正常工作。因为此类代码使用现有电子邮件发送电子邮件。最简单的方法是使用您的 gmail 帐户。

【讨论】:

  • 您不必允许“不太安全的应用程序”; google 希望您使用 PHPMailer 也支持的 XOAUTH2 身份验证,但设置起来比较困难。
  • 你可以选择任何你想要的,但我给出了一个简单的解决方案。
  • 好的,但在这种情况下,您不应建议使用用户提交的凭据进行 gmail 身份验证;这是一个用于攻击 gmail 帐户的开放代理,并且对地址和正文内容执行相同的操作使该脚本成为一个开放的垃圾邮件网关。简单是好的,但脆弱是坏的。 PHPMailer 提供的股票 gmail 示例避免了这些问题。
  • 是的,您是对的,但我们可以创建一个 gmail 帐户,仅用于发送电子邮件,而不是使用我们用来保存游戏和备份进度的帐户。
  • 当我这样做时,我收到错误消息“SMTP 错误:密码命令失败:535-5.7.8 用户名和密码不被接受。”我将电子邮件和密码写到了我想作为用户名和密码发送电子邮件的 gmail 帐户中,这是错误的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
相关资源
最近更新 更多