【问题标题】:How to integrate SMTP auth into a sendmail PHP script如何将 SMTP 身份验证集成到 sendmail PHP 脚本中
【发布时间】:2018-03-31 07:49:32
【问题描述】:

请记住,我是 PHP 新手

您好,我无法从仅使用 sendmail 的脚本发送 SMTP 邮件。

下面的代码是我原来的旧代码,但在查看时会在 Gmail 上发出警告。它说此消息无法验证。我正在尝试通过密码重置表单向用户发送他们的凭据(我也将在注册表单上实现这一点)。

原代码:

    ##### Mail functions #####

    function sendLostPasswordEmail($myusername, $email, $newpassword)
    {

    global $domain;
    $message = "
    You have requested a new password on example.com

    Here is Your new password information:

    username:  $myusername
    password:  $newpassword


    Regards,
    example Administration
    ";

    if (sendMail($email, "Your password has been reset.", $message, "noreply@example.com"))
    {
    return true;
    } else
    {
    return false;
    }


    }

    function sendMail($to, $subject, $message, $from)
    {


    $from_header = "From: $from";

    if (mail($to, $subject, $message, $from_header))
    {
    return true;
    } else
    {
    return false;
    }
    return false;
    }

    function sendActivationEmail($myusername, $password, $uid, $email, $actcode)
    {
    global $domain;
    $link = "https://example.com/includes/activate.php?uid=$uid&actcode=$actcode";
    $message = "
    Thank you for registering on https://example.com,

    Your account information:

    username:  $myusername
    password:  $password

    Please click the link below to activate your account.

    $link

    Regards
    $domain Administration
    ";

    if (sendMail($email, "Please activate your account.", $message, "noreply@example.com"))
    {
    return true;
    } else
    {
    return false;
    }
    }

    ?>

要实现的代码(我认为)

    require_once "php/Mail/mail.php";

    $from = "Example Company <noreply@example.com>";
    $to = "<>";
    $subject = "Your Password Has Been Reset";
    $body = "$message";

    $host = "mail.example.com";
    $port = "465";
    $username = "noreply@example.com";
    $password = "***";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

我相信第二个代码 sn-p 会在某个地方与原始代码相匹配,但是,我已经尝试了几个小时来解决这个问题,但无济于事。感谢您的帮助,如果您需要查看实际的密码恢复表单代码或认为我可以改进我的问题,请告诉我。

【问题讨论】:

    标签: php email server smtp


    【解决方案1】:

    阻止您发送 smtp 电子邮件的原因有很多(假设您使用正确的 SMTP 库)

    • 您使用的是 465 端口,这可能是使用 smtps 连接,请检查您在 phpinfo() 中的主机是否支持 open_ssl 扩展。
    • 您的主机不允许 SMTP 连接(例如 A2hosting 共享主机)
    • 您的 SMTP 提供商需要通过您的域进行验证才能发送电子邮件。

    希望能有所帮助。

    【讨论】:

    • 我的麻烦不是连接,而是实现。我知道如何连接到 SMTP 服务器,但不知道如何将 SMTP 身份验证集成到我现有的脚本中。谢谢
    • 您使用的是哪个库? Google上有很多库,phpMailer, SwiftMailer ....
    • 你可以通过传递debug => true 来启用调试模式并检查结果吗?
    • 我的代码一定有很大的错误,页面只是给出了 500 错误。
    【解决方案2】:

    尝试使用PHPMailer。这个工具太强大了。您只需将其包含在您的网站中,进行配置即可开始使用。此外,良好的文档且易于理解。

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 2010-10-27
      • 2015-04-29
      • 1970-01-01
      • 2014-01-07
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多