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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?