简体   繁体   English

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

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

I am trying to take inputs from a form on my website using a simple PHP script as given below: 我正在尝试使用下面给出的简单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.';
}
?>

This worked perfectly. 这工作得很好。 Well, almost. 好吧,差不多。 The email I receive does not include the sender's name. 我收到的电子邮件不包含发件人的姓名。 I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.) 我希望看到一个正常的电子邮件回复(在收件箱的“名称”列中包含发件人的名称,打开邮件后我应该能够看到发件人的电子邮件地址。)

So I made a few changes after looking up some threads like this: 因此,在查找了类似以下内容的线程后,我进行了一些更改:

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

?>

Email response is still same. 电子邮件回复仍然相同。 What do I need to change ? 我需要更改什么?

Try this format, note the spaces and the \\r\\n's, change your variables accordingly: 尝试这种格式,注意空格和\\ 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);
?>

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

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

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