[英]How to format an email address line (with name) with PHP?
I'm trying to generate email messages. 我正在尝试生成电子邮件。 The recipient's email address and name are specified by a user.
收件人的电子邮件地址和名称由用户指定。 What is the correct way to do this with PHP:
用PHP执行此操作的正确方法是什么:
$to_header = "To: $name <$email>" # incorrect!
I already use a decent method for validating the email addressess (let's not go to that now...), but how do I properly encode $name with eg. 我已经使用了一种体面的方法来验证电子邮件地址(现在不去讨论……),但是如何正确编码例如$ name。 QP when needed?
何时需要QP? For example, if the recipient is called "Foo Bär", I should produce (eg.) something like:
例如,如果收件人被称为“ FooBär”,我应该制作(例如)以下内容:
To: =?utf-8?Q?Foo_B=C3=A4r?= <foo.bar@example.com>
Update: Earlier I wasn't using a ready-made mailer such as PHPMailer for other reasons (we already had an external queue system). 更新:由于其他原因,我之前没有使用现成的邮件程序,例如PHPMailer(我们已经有一个外部队列系统)。 Anyway, now I'm ending up using SwiftMailer .
无论如何,现在我最终要使用SwiftMailer 。 Thanks for all the answers!
感谢所有的答案!
您可以使用imap_rfc822_write_address
如果可用)。
You should try to use a third party libraby such as PhpMailer or Zend_Mail. 您应该尝试使用第三方库,例如PhpMailer或Zend_Mail。 They provide simple methods to set up all these parameters.
它们提供了设置所有这些参数的简单方法。 They take care of encoding too, and enable some powerful control over sending/authenticating
它们也负责编码,并可以对发送/验证进行一些强大的控制
Otherwise, try going the hardway using the different imap methods : http://fr2.php.net/manual/fr/book.imap.php 否则,请尝试使用不同的imap方法努力: http : //fr2.php.net/manual/fr/book.imap.php
I've used iconv_mime_encode
to encode the Subject -header. 我已经使用
iconv_mime_encode
编码Subject -header。 I presume the same could be used for encoding the name in the To -header as well. 我假设同样可以用于在To-header中对名称进行编码。
I do, however, as others have, recommend using an existing library or package to handle the encoding for you. 但是,我确实像其他人一样,建议使用现有的库或包来为您处理编码。
I've used Mail mime available from PEAR. 我使用了PEAR提供的Mail mime 。 Not perhaps the best out there, but as an alternative to the other ones suggested.
也许不是最好的,但是作为其他建议的替代。
var_dump(
iconv_set_encoding('output_encoding', 'UTF-8'),
iconv_set_encoding('internal_encoding', 'UTF-8'),
iconv_set_encoding('input_encoding', 'UTF-8'),
iconv_mime_encode('To', 'Bäråör Zückefém') . " <foo@bar.com>"
);
Here is how I do it (with a bit of overkill): 这是我的操作方法(有些过分强调):
function check_referrer($referrers) {
if (count($referrers)) {
$found = false;
$temp = explode("/",getenv("HTTP_referrer"));
$referrer = $temp[2];
if ($referrer == "") {
$referrer = $_SERVER['HTTP_referrer'];
list($remove, $stuff) = split('//', $referrer, 2);
list($home, $stuff) = split('/', $stuff, 2);
$referrer = $home;
}
for ($x = 0; $x < count($referrers); $x++) {
if (eregi ($referrers[$x], $referrer)) {
$found = true;
}
}
if ($referrer == "") {
$found = false;
}
if (!$found){
error_log("[Store Checkout] Illegal Referrer. (".getenv("HTTP_referrer").")", 0);
return "<div id='error'><p>You are coming from an <strong>unauthorized domain.</strong></p>\r\n"."\t<ul>\r\n".$error_list."\t</ul>\r\n"."</div>\r\n";
}
return $found;
} else {
return "<div id='error'><p>You are coming from an <strong>unauthorized domain.</strong></p>\r\n"."\t<ul>\r\n".$error_list."\t</ul>\r\n"."</div>\r\n";
}
} /* end function check_referrer */
function mail_it($content, $subject, $sender, $recipient) {
$referrers = array("example.com");
$authorizedDomain = check_referrer($referrers);
if($authorizedDomain === FALSE) {
return $authorizedDomain;
}
$sender = remove_headers($sender);
$recipient = remove_headers($recipient);
if($content !== FALSE && $subject !== FALSE && $sender !== FALSE && $recipient !== FALSE) {
$headers = "from: ".$sender."\r\n";
mail($recipient, $subject, $content, $headers);
}
return;
} /* end function mail_it */
$content = "email body content";
$subject = "email subject";
$sender = "Your Name <yname@example.com>";
$recipient = $name . "<" . $email . ">";
mail_it($content, $subject, $sender, $recipient);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.