我在openshift服务器上安装了pear邮件包。
然后,我编写了这个PHP代码,让用户通过网络表单向我发送电子邮件。我没有收到任何错误输出,但我无法在我的电子邮件地址收到该消息。
<?php
require_once "Mail.php";
if($_POST)
{
$recipients = "myemailaddress";
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userSubject"]) || !isset($_POST["userMessage"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
else
{
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$headers['From'] = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$headers['To'] = $recipients;
$headers['Subject'] = filter_var($_POST["userSubject"], FILTER_SANITIZE_STRING);
$body = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$params['sendmail_path'] = '/usr/lib/sendmail';
//additional php validation
if(strlen($user_Name) < 4)
{
// SOME CODE HERE
}
$mail_object =& Mail::factory('sendmail', $params);
$sentMail = $mail_object->send($recipients, $headers, $body);
if(PEAR::isError($sentMail))
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'.$sentMail->getMessage()));
die($output);
}
else
{
$output = json_encode(array('type'=>'message', 'text' => 'Email sent successfully! <br/>'.$user_Name .' thank you for your email.'));
die($output);
}
}
}
?>
答案 0 :(得分:1)
垃圾邮件过滤器很可能会抓住您的电子邮件。解决这个问题的最佳方法是使用第三方电子邮件服务,例如SendGrid或您的google / yahoo电子邮件帐户,并进行身份验证。