将HEADER电子邮件更改为自定义电子邮件

时间:2018-12-05 04:07:29

标签: php email header

我有一个表单验证电子邮件脚本,该脚本使用在“电子邮件”字段中输入的FROM电子邮件地址;不幸的是,我的托管人仅接受一个特定的电子邮件地址:no-reply@myhost.com

如何在此php脚本中通过电子邮件地址指定“自定义”:

<?php
if($_POST)
{
require('constant.php');

    $user_name      = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $user_email     = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
    $content   = filter_var($_POST["content"], FILTER_SANITIZE_STRING);

    if(empty($user_name)) {
        $empty[] = "<b>Name</b>";       
    }
    if(empty($user_email)) {
        $empty[] = "<b>Email</b>";
    }
    if(empty($user_phone)) {
        $empty[] = "<b>Phone Number</b>";
    }   
    if(empty($content)) {
        $empty[] = "<b>Comments</b>";
    }

    if(!empty($empty)) {
        $output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
        die($output);
    }

    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
        $output = json_encode(array('type'=>'error', 'text' => '<b>'.$user_email.'</b> is an invalid Email, please correct it.'));
        die($output);
    }

    //reCAPTCHA validation
    if (isset($_POST['g-recaptcha-response'])) {

        require('component/recaptcha/src/autoload.php');        

        $recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());

        $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

          if (!$resp->isSuccess()) {
                $output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
                die($output);               
          } 
    }

    $toEmail = "myemail@gmail.com";
    $mailHeaders = "From: " . $user_name . "<" . $user_email . ">\r\n";
    if (mail($toEmail, "Contact Mail", $content, $mailHeaders)) {
        $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', thank you for the comments. We will get back to you shortly.'));
        die($output);
    } else {
        $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
        die($output);
    }
}
?>

我尝试替换:

$mailHeaders = "From: " . $user_name . "<" . $user_email . ">\r\n";

具有:

$mailHeaders = "From: no-reply@myhost.com>\r\n";

但是没有用; 抱歉,我的PHP知识很少:(

谢谢。

1 个答案:

答案 0 :(得分:0)

$mailHeaders = "From: no-reply <no-reply@myhost.com>\r\n";

解决了该问题,只需确保在no-reply和<no-reply@myhost.com>

之间留有空间

但是现在我们丢失了“电话”实例-我该如何结合

$user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
$content   = filter_var($_POST["content"], FILTER_SANITIZE_STRING);

一起吗?

通过添加找到解决方案:

 $mail_body        = $content;
   $mail_body       .= "\r\n\r\nName: ".$user_name; //sender name
   $mail_body       .="\r\nE-Mail: ".$user_email; //sender email
   $mail_body       .="\r\nPhone: ".$user_phone; //sender phone
   $mail_body       .="\r\n\r\n";


if (mail($toEmail, "Contact Mail", $mail_body, $mailHeaders)) {