Jump to content

Php Mail() Function


dozure
 Share

Recommended Posts

I recently came across a problem using mail() for the first time, and thought I would share this with you guys. When you use the mail() function, by default it sends the email as Apache on your server, which is the user Nobody, so the from and reply-to addresses are nobody@serverXX.totalchoicehosting.com After realizing this I asked a tech in chat if there was a way to circumvent this, and as far as they knew there wasnt. And there isnt, at least not on the server side of things. So I hopped over to php.net and looked at the info on the mail() function to see what I could see (as I should have to begin with) and as of v4.3 of PHP, you can pass more than 3 expressions to mail() and they are added on as headers. so... if you add a 4th expression that says "From: e@mail.com" the email will be sent with a from and reply-to address of whatever you put next to From:

 

ex:

>mail("recipient@domain.com", "this is a subject", "body of message", "From: me@****");

will send an email to recipient@domain.com with a subject of this is a subject, a body that says body of mesage, and the from and reply addresses will be me@****

 

 

 

Hope this is helpful to someone! Kick Me

Link to comment
Share on other sites

You are exactly right.

 

If you are planning on sending any email to hotmail users, then you should read the user comments at php.net as there are other headers that you need to put into your email function in order to make it work with some hotmail accounts.

 

http://us4.php.net/manual/en/ref.mail.php

 

Dear all,

 

Setting up a system which will ask a new member to confirm he asked for it

by mailing him I experienced a problem. If a new member fills in a Hotmail

e-mailaddress he will receive my mail in his Junk Mail directory.

 

Many tips I found where telling te configure the php.ini on my server but

that is not enough. I use Apache on a Win32 so some things will be different

for you Linux people but the problems I experienced do not have anything to

do with that I believe.

But, configure your php.ini setting the mail functions, for Win32:

 

SMTP = mail.yourisp.com

sendmail_from = justsomething@myserver.com

 

Now the most important.

When using mail() you should insert headers, and not just a few!!!

I give you an example I'm using.

Very important is also to include the ' To: ' header. I forgot this and when

I put it in my code Hotmail didn't refuse my e-mails anymore. Only when

people have there Junk Mail listed as "Exclusive". But when configured

"High" it will go ok.

 

Here the example:

 

$myname = "Me Myself";

$myemail = "myself@email.com";

 

$contactname = "Mister Contact";

$contactemail = "contact@email.com";

 

$message = "hello from happy me";

$subject = "A mail not refused by Hotmail";

 

$headers .= "MIME-Version: 1.0\r\n";

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "From: ".$myname." <".$myemail.">\r\n";

$headers .= "To: ".$contactname." <".$contactemail.">\r\n";

$headers .= "Reply-To: ".$myname." <$myreplyemail>\r\n";

$headers .= "X-Priority: 1\r\n";

$headers .= "X-MSMail-Priority: High\r\n";

$headers .= "X-Mailer: Just My Server";

 

mail($contactemail, $subject, $message, $headers);

 

I hope this helped someone.

 

Godfried van Loo

The Netherlands

Edited by surefire
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...