如何SMTP认证整合成一个sendmail的PHP脚本

问题描述:

请记住,我是新来的PHP如何SMTP认证整合成一个sendmail的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, "[email protected]")) 
    { 
    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, "[email protected]")) 
    { 
    return true; 
    } else 
    { 
    return false; 
    } 
    } 

    ?> 

代码来实现(我认为)

require_once "php/Mail/mail.php"; 

    $from = "Example Company <[email protected]>"; 
    $to = "<>"; 
    $subject = "Your Password Has Been Reset"; 
    $body = "$message"; 

    $host = "mail.example.com"; 
    $port = "465"; 
    $username = "[email protected]"; 
    $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); 

我相信第二届代码片段将适合于与原来的地方,但我一直在努力了好小时解决这个问题无济于事。我很感激任何帮助,如果您需要查看实际的密码恢复表单代码或认为我可以改善我的问题,请告诉我。

有很多理由阻止你发送SMTP电子邮件(假设你使用正确的SMTP库)

  • 您正在使用端口465,这是一个使用SMTPS可能的连接,请检查您的phpinfo主机( )如果它支持open_ssl扩展。
  • 您的主机不允许SMTP连接(例如,A2hosting共享主机)
  • 您的SMTP提供商需要从您的域验证以允许发送电子邮件。

希望能有所帮助。

+0

我的麻烦不是连接,而是实现。我知道如何连接到SMTP服务器,但不知道如何将SMTP认证集成到我现有的脚本中。谢谢 –

+0

你正在使用哪个库?在Google,phpMailer,SwiftMailer上有很多库... –

+0

这是一个梨包http://pear.php.net/package/Mail/docs/1.4.1/Mail/Mail_smtp.html –

尝试使用PHPMailer。这个工具非常强大。你只需要将它包含到你的网站,配置,你就可以开始了。此外,良好的文档和易于理解。