【问题标题】:Is there something wrong with this email function?这个电子邮件功能有问题吗?
【发布时间】:2019-05-08 02:56:27
【问题描述】:

我编写了一个函数来为我正在开发的一个爱好应用程序发送一封确认电子邮件。我以为我已正确设置了所有内容,但是在调用该函数时出现以下错误:

2019-05-08 02:43:30 Connection failed. Error #2: failed loading cafile stream: `C:\xampp\apache\bin\curl-ca-bundle.crt' [C:\Users\Michael\Desktop\InviteMe Application\vendor\phpmailer\src\SMTP.php line 405] SMTP Error: Could not connect to SMTP host.

我已经设置了我的 gmail 帐户以允许不太安全的应用访问该帐户,并且我已经生成了一个密码。

我已尝试阅读我正在使用的库的故障排除文档,但未能找到解决我遇到的特定问题的任何内容。

这里是电子邮件功能:

function send_confirmation_email($name, $email, $user_id) {
    $confirmationUrl = generate_confirmation_url($user_id);
    $body = "";
    $body .= "<html><body>";
    $body .="<h1>Confirm your account</h1>";
    $body .= "<p>Follow the link below to confirm your account:</p>";
    $body .= "<a href='" . $confirmationUrl . "'>confirm account</a>";

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 3; //debugging purposes
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = "invitemedemo@gmail.com";
    $mail->Password = "password";
    $mail->setFrom('invitemedemo@gmail.com', "InviteMe");
    $mail->addAddress($email, $name);
    $mail->Subject = "Confirm Account";
    $mail->Body = $body;
    $mail->isHTML(true);

    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        return false;
    } else {
        return true;
    }
}

我看到的是上面粘贴的错误,而不是发送的电子邮件。

【问题讨论】:

  • 我知道脚本无法连接到 SMTP 服务器。我就是不明白为什么。
  • CA 证书 (C:\xampp\apache\bin\curl-ca-bundle.crt) 是否存在?有效吗?
  • 如果您在本地,phpmailer 类可能无法正常工作,因此请在主机上对其进行测试,如果它在那里,请联系您的托管服务提供商...
  • @Raptor 所说的内容,并阅读 PHPMailer 故障排除指南对 CA 证书包的说明。
  • CA 证书应该是有效的。我直接从 repo 下载了文件。我有一种感觉,这可能都是因为我创建了一个新的作用域,而新函数作用域内的方法无法访问它之外的属性。

标签: php email phpmailer


【解决方案1】:

验证您的 smtp 电子邮件服务。

如果您有 MAC 操作系统,请参阅如何使用您的 gmail 帐户为 Gmail SMTP 启用 postfix:https://gist.github.com/loziju/66d3f024e102704ff5222e54a4bfd50e

使用 Ubuntu Linux 参考:https://gist.github.com/adamstac/7462202

检查 php.ini 文件中的 PHP 设置以获取您正在使用的活动 php 版本。 即为MAC OS使用MAMP,gt到Applications -> MAMP -> conf -> php7.0.32(必须是您正在使用的活动版本,所以如果您使用的是php 5.3.14,请转到该目录)->php .ini

编辑 php.ini 文件:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well 
(default: "sendmail -t -i").
;sendmail_path = sendmail -t -i -f example@gmail.com
sendmail_path = "env -i /usr/sbin/sendmail -t -I"

更新 php.ini 文件后重启 apache2。

如果使用 MAC OS 停止和重启邮件服务:sudo postfix stop && sudo postfix start sudo postfix reload

测试电子邮件 从终端 CLI 发送测试电子邮件:

echo "Test sending email from Postfix" | mail -s "Test Postfix" example@gmail.com

另外,我发现我可以使用以下命令从终端 CLI 发送测试消息:

echo "test message" | sendmail -v example@gmail.com

使用终端 CLI 命令检查 sendmail 的错误:

mailq

您可以使用 php 测试 SMTP 功能,例如:

<?php
    // testemail.php
    $to_email = 'example@gmail.com';
    $subject = 'Testing PHP Mail';
    $message = 'This mail is sent using the PHP mail function';
    $message = wordwrap($message, 70);
    $headers = 'From: yoursending@email.com';
    mail($to_email,$subject,$message,$headers);
    $mail = mail($to_email,$subject,$message,$headers);
    if ($mail) { echo "Thank you for using mail";}
    else { echo "Mail sending failed.";}
    ini_set('display_errors',1);
?>

【讨论】:

  • 这完全相关。 1. OP 使用的是 XAMPP,而不是 MAMP。 2. OP 使用的是 Windows,而不是 Linux(因此没有 Postfix,没有 sendmail)。 3. OP的问题是由于无法加载CA证书导致与SMTP主机的网络连接。
猜你喜欢
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2013-01-27
  • 2021-05-20
  • 2020-03-26
相关资源
最近更新 更多