Mail.php 和 Smtp 身份验证问题

2024-03-19

我一直在尝试利用 jquery contactable 插件(在 google 上找到!)中的 mail.php 文件在我的网站上使用。尽管提供的脚本相当简单,但我在将其与主机的 SMTP 要求集成时遇到了问题。这是没有 SMTP 身份验证的原始脚本:

<?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' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // 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('[email protected] /cdn-cgi/l/email-protection', $subject, $contactMessage, $headers) ) ? "success" : "failure" ;
    $output = json_encode(array("response" => $response));

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

?>

我尝试使用谷歌的建议并尝试了几个小时。这是基于我迄今为止对 php 的零理解的最新版本。 -__-(基于此:http://blog.geek4support.com/php-mail-script-with-smtp-authentication-how-to-send-mails-by-php-mail-script-using-smtp-authentication/ 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 = "[email protected] /cdn-cgi/l/email-protection";
 $password = "mymailpassword";

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

    // 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('[email protected] /cdn-cgi/l/email-protection', $subject, $contactMessage, $headers))  ? "success": "failure";
$output = json_encode(array("response" => $response));  
    header('content-type: application/json; charset=utf-8');
    echo($output);

 ?>

我实际上遇到了一些问题。我的主机不支持 PHPMailer :-(。仅支持 SMTP 的 PearMail。他们建议调整上面列出的代码并将我现有的代码合并到其中。确切地说,在网上发布此内容之前我一直在尝试做的事情。回到广场1、有什么想法吗?

意见、建议,任何事情都将不胜感激! :-)


要发送邮件,请尝试PHP邮件程序 http://phpmailer.worxware.com/,它经过测试,每个人都使用它,而且它确实有效。 它还具有很多功能和配置选项。

最新版本是this one http://sourceforge.net/projects/phpmailer/files/latest/download?source=files,至于使用 SMTP 和 PHPMailer 发送邮件,这就是您需要的所有代码

// 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 = "[email protected] /cdn-cgi/l/email-protection";
$mail->Password = "yourpassword";            
//$mail->Port = 465; // optional if you don't want to use the default 

$mail->From = "[email protected] /cdn-cgi/l/email-protection";
$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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mail.php 和 Smtp 身份验证问题 的相关文章

随机推荐