【问题标题】:How to replay to sender email from PHP instead of CGI-Mailer如何从 PHP 而不是 CGI-Mailer 回复发件人电子邮件
【发布时间】:2020-02-04 06:21:05
【问题描述】:

我已经尝试了太多不同的方法来从 $_request[email] 获得回复,但它仍然使用 $from CGI-mailer 发送邮件,尽管我邮件中的所有正文都可以正常工作。 我尝试了太多方法,但找不到问题出在哪里。我在这里查看了这个问题的几个答案,但没有人能解决我的问题。这是我的代码。

<?php
$subject = 'Contacact from website'; 
$to = 'contact@myhosting.com'; 
$emailTo = $_REQUEST['email'];

// an email address that will be in the From field of the email.
$name = $_REQUEST['name'];
$email = $_REQUEST['email']; // i can't get this going to the reply-to section on the mail
$phone = $_REQUEST['phone'];
$msg = $_REQUEST['message'];

$email_from = $name.'<'.$email.'>';

$headers = "MIME-Version: 1.1";
$headers .= "Content-type: text/html; charset=utf-8";
$headers .= 'From:  ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
     'Reply-To: '.  $fromEmail . "\r\n" .
     'X-Mailer: PHP/' . phpversion();

// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));

$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;

if (@mail($to, $subject, $message, $email_from)) {
    // Transfer the value 'sent' to ajax function for showing success message.
    echo 'sent';
} else {
    // Transfer the value 'failed' to ajax function for showing error message.
    echo 'failed';
}
?>

这是我的表格:

<form name="contactForm" id='contact_form' method="post" action='email.php'>
    <div class="row">
        <div class="col-md-4">
            <div id='name_error' class='error'>write your name</div>
            <div>
                <input type='text' name='name' id='name' class="form-control" placeholder="Name">
            </div>

            <div id='email_error' class='error'>Write a valid email</div>
            <div>
                <input type='email' name='email' id='email' class="form-control" placeholder="
     email">
            </div>

            <div id='phone_error' class='error'>Write a phone number.</div>
            <div>
                <input type='tel' name='phone' id='phone' class="form-control" placeholder="Your name">
            </div>
        </div>
        <div class="col-md-8">
            <div id='message_error' class='error'>Please write your message here</div>
            <div>
                <textarea name='message' id='message' class="form-control"
                    placeholder="Message or quotation"></textarea>
            </div>
        </div>

        <div class="col-md-12">
            <p id='submit'>
                <input type='submit' id='send_message' value='Enviar' class="btn btn-line">
            </p>
            <div id='mail_success' class='success'>We received your message :)</div>
            <div id='mail_fail' class='error'>Please try again :/</div>
        </div>
    </div>
</form>

【问题讨论】:

    标签: php email header cgi


    【解决方案1】:

    您发布的内容有一些问题。

    首先,这段代码的第一行:

    $message .= 'Name : ' . $name . "\n";
    $message .= 'Email : ' . $email . "\n";
    $message .= 'phone : ' . $phone . "\n";
    $message .= 'Message : ' . $msg;
    

    $message .= 不应有前导点,应为:

    $message = 'Name : ' . $name . "\n";
    

    然后这一行:

    if (@mail($to, $subject, $message, $email_from))
    

    由于您使用$email_from 作为最后一个参数,mail() 就像您在发送邮件的其他实例中所做的那样,使用有效的From: 和电子邮件地址,因为它来自“来自",当第二个不包含该内容时,您只需将其声明为$email_from = $name.'&lt;'.$email.'&gt;';

    您需要做的是添加From:,就像您为第一个邮件实例所做的那样。

    旁注:@ 符号是一个错误抑制器。您可能希望在测试/开发期间删除它。

    有关更多详细信息,请参阅mail() 函数的手册:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 2013-03-03
      • 2013-06-29
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      相关资源
      最近更新 更多