tags:

views:

221

answers:

3

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.

Thanks!

A: 

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

xaoc
+1  A: 

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" <[email protected]>"; // substituting your email address
$param_to  ='"param" <[email protected]>";
$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.

symcbean
Thanks! I can indeed just test it :)Btw: there are two syntax errors in your example, line 1 and 2 end in a double quote.
Niels Bom
(doh! - unfortunately S.O. doesn't have Vim interface!)I would guess you got two emails? What about the subject?
symcbean
+1  A: 

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.

Álvaro G. Vicario
Good one about the From and To, it's good to know. Low-level email when using universal character sets and varying attachments is quite the jungle with all the RFC's and stuff.
Niels Bom