Mail.php amp; Smtp Authentication Issue(邮件.php amp;SMTP 身份验证问题)
问题描述
我一直在尝试利用 jquery 可联系插件(在 google 上找到!)中的 mail.php 文件在我的网站上使用.尽管提供的脚本相当简单,但我在将其与主机的 SMTP 要求集成时遇到了问题.这是没有 SMTP 身份验证的原始脚本:
<p><strong>姓名:</strong>$name <br/><strong>电子邮件:</strong>$emailAddr <br/><strong>问题:</strong>$问题 </p><p><strong>消息:</strong>$comment </p><p><strong>发送IP:</strong>$_SERVER[REMOTE_ADDR]<br/><strong>发送方式:</strong>$_SERVER[HTTP_HOST]</div>";//发送并检查消息状态$response = (mail('mymail@mymail.com', $subject, $contactMessage, $headers) ) ?成功":失败";$output = json_encode(array("response" => $response));header('content-type: application/json; charset=utf-8');回声($输出);?>
我尝试使用来自 Google 的建议并玩了几个小时.这是迄今为止基于我对 php 的零理解的最新版本.-__- (基于此:http://blog.geek4support.com/php-mail-script-with-smtp-authentication-how-to-send-mails-by-php-mail-script-using-smtp-authetication/)
<p><strong>姓名:</strong>$name <br/><strong>电子邮件:</strong>$emailAddr <br/><strong>问题:</strong>$问题 </p><p><strong>消息:</strong>$comment </p><p><strong>发送IP:</strong>$_SERVER[REMOTE_ADDR]<br/><strong>发送方式:</strong>$_SERVER[HTTP_HOST]</div>";$smtp = Mail::factory('smtp',数组('主机' => $主机,'认证' =>真的,'用户名' =>$用户名,'密码' =>$密码));$response = ($smtp->send('mymail@mymail.com', $subject, $contactMessage, $headers)) ?成功":失败";$output = json_encode(array("response" => $response));header('content-type: application/json; charset=utf-8');回声($输出);?>我实际上遇到了一些问题.我的主机不支持 PHPMailer :-(.只有带有 SMTP 的 PearMail.他们建议调整上面列出的代码并将我现有的代码与它合并.确切地说,在在线发布之前我一直在尝试做的事情.回到广场1、有什么想法吗?
评论,建议,任何东西都将不胜感激!:-)
解决方案 对于发送邮件,试试 PHPMailer,它已经过测试,每个人都在使用它,而且它可以正常工作.它还具有许多功能和配置选项.
最新版本是这个,至于发送使用带有 PHPMailer 的 SMTP 发送邮件,这就是您需要的所有代码
//POST 请求收到的数据$name = stripcslashes($_POST['name']);$emailAddr = stripcslashes($_POST['email']);$issue = stripcslashes($_POST['issue']);$comment = stripcslashes($_POST['message']);$subject = stripcslashes($_POST['subject']);//发送邮件$mail = 新 PHPMailer();$mail->IsSMTP();//告诉类使用 SMTP//SMTP 配置$mail->SMTPAuth = true;//启用 SMTP 认证$mail->Host = "我的主机";//SMTP 服务器$mail->用户名 = "你的用户名@gmail.com";$mail->Password = "你的密码";//$mail->端口 = 465;//如果您不想使用默认值,则可选$mail->From = "my@email.com";$mail->FromName = "我的名字";$mail->主题 = $主题;$mail->AltBody = "要查看邮件,请使用与 HTML 兼容的电子邮件查看器!";//可选,注释掉并测试$mail->MsgHTML($issue . "<br/><br/>" . $comment);//添加任意数量$mail->AddAddress($emailAddr, $name);//如果你想附加一个文件,它的相对路径//$mail->AddAttachment("images/phpmailer.gif");//依恋$响应=空;if(!$mail->Send()) {$response = "邮件错误:" .$mail->错误信息;} 别的 {$response = "消息已发送!";}$output = json_encode(array("response" => $response));header('content-type: application/json; charset=utf-8');回声($输出);
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! :-)
解决方案 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);
这篇关于邮件.php &SMTP 身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:邮件.php &SMTP 身份验证问题
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01