繁体   English   中英

在通过用户输入表单通过电子邮件发送消息时,如何包括发件人的姓名,而不仅仅是电子邮件地址

[英]How do I include sender's name and not just the email address while emailing message from user input form

我正在尝试使用下面给出的简单PHP脚本从网站上的表单中获取输入:

<?php 
$toemail = 'xyz@anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
    echo 'Your email was sent successfully.';
} else {
    echo 'There was a problem sending your email.';
}
?>

这工作得很好。 好吧,差不多。 我收到的电子邮件不包含发件人的姓名。 我希望看到一个正常的电子邮件回复(在收件箱的“名称”列中包含发件人的名称,打开邮件后我应该能够看到发件人的电子邮件地址。)

因此,在查找了类似以下内容的线程后,我进行了一些更改:

<?php 

$toemail = 'xyz@anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;


if(mail($toemail, 'Subject', $message, $header)) {
    echo 'Your email was sent successfully.';
} else {


echo 'There was a problem sending your email.';
    }

?>

电子邮件回复仍然相同。 我需要更改什么?

尝试这种格式,注意空格和\\ r \\ n,相应地更改变量:

$email_headers  = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <sender_email@yourdomain.com>' . "\r\n";
$email_headers .='Reply-To: reply_to_email@yourdomain.com' . "\r\n";

mail($recipient_address, $email_subject, $email_body, $email_headers);
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

引用自http://php.net/manual/en/function.mail.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM