duanlei1957 2012-07-13 20:27
浏览 69
已采纳

Mail.php和Smtp身份验证问题

I've been trying to utilize a mail.php file from the jquery contactable plugin (found on google!) to use on my website. Although the script provided is fairly simple I'm running into issues with integrating it with my Host's SMTP requirement. Here is the original script without SMTP authentication:

<?php
    // Assign contact info
    $name = stripcslashes($_POST['name']);
    $emailAddr = stripcslashes($_POST['email']);
    $issue = stripcslashes($_POST['issue']);
    $comment = stripcslashes($_POST['message']);
    $subject = stripcslashes($_POST['subject']);    

    // Set headers
    $headers  = 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

    // Format message
    $contactMessage =  
    "<div>
    <p><strong>Name:</strong> $name <br />
    <strong>E-mail:</strong> $emailAddr <br />
    <strong>Issue:</strong> $issue </p>

    <p><strong>Message:</strong> $comment </p>

    <p><strong>Sending IP:</strong> $_SERVER[REMOTE_ADDR]<br />
    <strong>Sent via:</strong> $_SERVER[HTTP_HOST]</p>
    </div>";

    // Send and check the message status
    $response = (mail('mymail@mymail.com', $subject, $contactMessage, $headers) ) ? "success" : "failure" ;
    $output = json_encode(array("response" => $response));

    header('content-type: application/json; charset=utf-8');
    echo($output);

?>

I've tried using suggestions from Google and played around with it for hours. Here is the latest version based on my nil-understanding of php thus far. -__- (Based on this: http://blog.geek4support.com/php-mail-script-with-smtp-authentication-how-to-send-mails-by-php-mail-script-using-smtp-authetication/)

<?php
 require_once "Mail.php";

    // Assign contact info
    $name = stripcslashes($_POST['name']);
    $emailAddr = stripcslashes($_POST['email']);
    $issue = stripcslashes($_POST['issue']);
    $comment = stripcslashes($_POST['message']);
    $subject = stripcslashes($_POST['subject']);    


 $host = "mail.mywebsite.com";
 $username = "mywebsitemail@mywebsiteaddress.com";
 $password = "mymailpassword";

    // Set headers
    $headers  = 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

    // Format message
    $contactMessage =  
    "<div>
    <p><strong>Name:</strong> $name <br />
    <strong>E-mail:</strong> $emailAddr <br />
    <strong>Issue:</strong> $issue </p>

    <p><strong>Message:</strong> $comment </p>

    <p><strong>Sending IP:</strong> $_SERVER[REMOTE_ADDR]<br />
    <strong>Sent via:</strong> $_SERVER[HTTP_HOST]</p>
    </div>";

 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));


 $response = ($smtp->send('mymail@mymail.com', $subject, $contactMessage, $headers))  ? "success": "failure";
$output = json_encode(array("response" => $response));  
    header('content-type: application/json; charset=utf-8');
    echo($output);

 ?>

I've actually run into a bit of a problem. My host doesn't support PHPMailer :-(. Only PearMail with SMTP. They have suggested tweaking the code listed above and incorporating my existing one with it. Exactly, what I've been trying to do before posting this online. Back to square 1, any ideas?

Comments, suggestions, anything would be most appreciated! :-)

  • 写回答

2条回答 默认 最新

  • doucan8049 2012-07-13 20:49
    关注

    For sending mails, try PHPMailer, it's tested, everybody uses it, and it just works. It also has a lot of features and configuration options.

    The latest version is this one, as for sending mails using SMTP with PHPMailer this is all the code you need

    // Data received from POST request
    $name = stripcslashes($_POST['name']);
    $emailAddr = stripcslashes($_POST['email']);
    $issue = stripcslashes($_POST['issue']);
    $comment = stripcslashes($_POST['message']);
    $subject = stripcslashes($_POST['subject']);   
    
    // Send mail
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    
    // SMTP Configuration
    $mail->SMTPAuth = true;                  // enable SMTP authentication
    $mail->Host = "myhost"; // SMTP server
    $mail->Username = "yourusername@gmail.com";
    $mail->Password = "yourpassword";            
    //$mail->Port = 465; // optional if you don't want to use the default 
    
    $mail->From = "my@email.com";
    $mail->FromName = "My Name";
    $mail->Subject = $subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($issue . "<br /><br />" . $comment);
    
    // Add as many as you want
    $mail->AddAddress($emailAddr, $name);
    
    // If you want to attach a file, relative path to it
    //$mail->AddAttachment("images/phpmailer.gif");             // attachment
    
    $response= NULL;
    if(!$mail->Send()) {
        $response = "Mailer Error: " . $mail->ErrorInfo;
    } else {
        $response = "Message sent!";
    }
    
    $output = json_encode(array("response" => $response));  
    header('content-type: application/json; charset=utf-8');
    echo($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢