我已经在我的 Ubuntu+Nginx 机器上设置了 Postfix 来发送邮件,但遇到了一个问题。
问题是发件人姓名www-data
与电子邮件一起[email protected]
。
由于这是一个“发送给朋友”的表格,所以发件人姓名应该是,$_POST['sendername']
这样电子邮件就会被发送并显示为来自发送用户和他们的电子邮件,并且不能被硬编码/etc/postfix/main.cf
或其他任何地方,因为它在这种意义上是动态的,如上所述。
即使将“发件人”标头测试为硬编码值(如您所见,我已将其注释掉),它仍然不会显示为发件人的姓名和电子邮件。
有什么想法吗?谢谢。
$to = $_POST['recipientemail'];
$subject = $_POST['subject'];
//$headers = "From: Me <[email protected]>\r\n";
$headers = "From: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers.= "Reply-To: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers = "To: " . $_POST['recipientname'] . " <" . $_POST['recipientemail'] . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$message = '<html><body>';
$message .= $_POST['message'];;
$message .= "</body></html>";
mail($to, $subject, $message, $headers);
/etc/postfix/main.cf
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:myuser:mypass
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587
后配置-n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
header_size_limit = 4096000
inet_interfaces = all
mailbox_size_limit = 0
mydestination = ip-123-45-67-890.ap-southeast-2.compute.internal, localhost.ap-southeast-2.compute.internal, , localhost
myhostname = ip-123-45-67-890.ap-southeast-2.compute.internal
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
readme_directory = no
recipient_delimiter = +
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:myuser:mypass
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes
答案1
我发布这个问题的时候,一定是一个漫长的星期五。现在有了清醒的“星期一”,我就能弄清楚了。标题的设置不正确,没有说明.=
每个标题的位置。
应该是:
$headers = "From: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers .= "To: " . $_POST['recipientname'] . " <" . $_POST['recipientemail'] . ">\r\n";