dongqijuan3786 2018-12-05 04:07
浏览 173
已采纳

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

I have a form validation email script which is using FROM email address entered to "Email" field; unfortunately my hosting accepting only one specific email address: no-reply@myhost.com

How I can specify "custom" FROM email address in this php script:

<?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 . ">
";
    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);
    }
}
?>

I have tried to replace :

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

with:

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

but it did not work; Sorry, my PHP knowledge is very small :(

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • doubi6303 2018-12-05 06:00
    关注
    $mailHeaders = "From: no-reply <no-reply@myhost.com>
    ";
    

    fixed the issue, just make sure there is space between no-reply and <no-reply@myhost.com>

    but now we lost "phone" instance- how I can combine

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

    together?

    found a solution by adding :

     $mail_body        = $content;
       $mail_body       .= "
    
    Name: ".$user_name; //sender name
       $mail_body       .="
    E-Mail: ".$user_email; //sender email
       $mail_body       .="
    Phone: ".$user_phone; //sender phone
       $mail_body       .="
    
    ";
    
    
    if (mail($toEmail, "Contact Mail", $mail_body, $mailHeaders)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?