开发者

PHP mail, header and call to mail() function, what has override?

开发者 https://www.devze.com 2022-12-21 12:35 出处:网络
I\'ve been struggling with low level mail in PHP and I know I should be using a library for this, but that\'s not an option right now.

I've been struggling with low level mail in PHP and I know I should be using a library for this, but that's not an option right now.

When doing mail in PHP, you can manually set additional headers, like From, Cc and Bcc, but you can also set Subject, To and a Body. When you call the function you pass the headers along to the mail() function, but that function also "asks for" a Subject, body and To.

My question then is: how does PHP handle the double intention in this? If you manually set the header to have Subject : foo, but then in the call to mail pass 'foo' along as the subject...?

I can't read C, so opening up PHP source probably won't help me here.

Th开发者_StackOverflow中文版anks!


Well, no need to read C, just test it: it's a one liner :)

If you specify a subject in both places PHP does nothing special: you get an e-mail message with two subject headers. Which one gets displayed in your e-mail client is something I don't know; perhaps it's defined in e-mail protocols, perhaps it's a per-client choice.

About the "To" header, PHP sets one from the $to parameter when you don't specify a header manually; if you set one, your header prevails.

It's worth noting that the "From" and "To" headers have no effect in who sends and receives the message: they're purely informative. Mail server software requires senders and recipients to be specified implicitly; headers are not parsed for this purpose.


The question here seems to be if you specify a 'To' or a 'Subject' in the additional headers, what appears in the email produced.

If so, then the simple answer is to test it:

$add_to    ='to: "added" <user@example.com>"; // substituting your email address
$param_to  ='"param" <user@example.com>";
$add_subj  ='subject: Subject in header';
$param_subj='Subject in param';

$add_hdr=$add_to . "\n" . $add_subj;
mail($param_to, $param_subj, "body - test", $add_hdr);

Then have a look at the message you get back.

C.


Right way - don't use Subject, To and a Body in headers. You can remove it from heders with regular expression, if you take headers as is. Other way - you can use PEAR library - http://pear.php.net/manual/en/package.mail.mail-mime.example.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号