1

I have the following code to send my emails:

$EmailFrom= "staff@homes.com";
$EmailTo = "alejo@yahoo.com, hunterno1@gmail.com";
$Subject = "Contact The Loft";

$success = mail($EmailTo, $Subject, $Body, $EmailFrom);

When I submit my form it sends the mail correctly but on my email the header is not displayed correctly. Look the following image:

header-mail

I do not want THEHNOGK@SERVER179.WEB-HOSTING.COM be displayed as the header, so that is why i search thru google and i found out that i need to customize my header. So i did the follow, but now the email is not sent. it seems there is something wrong with the headers:

<?php
$EmailFrom= "From: HudsonLofts <staff@hobokenhomes.com>\r\n";
$EmailFrom.= "X-Mailer: PHP/" . phpversion()."\r\n";
$EmailFrom.= "MIME-Version: 1.0" . "\r\n";
$EmailFrom.= "Content-type: text/html; charset=iso-8859-1 \r\n";
$EmailTo = "alejo.ferguson@yahoo.com, hunternova01@gmail.com";
$Subject = "Contact The Hudson Loft";

And this is where i call the mail function:

$success = mail($EmailTo, $Subject, $Body, $EmailFrom);

When i submit the form and check my email account not email is received.

I will appreciate your help. Thanks in advance

I have realized that when i use FROM: the mail is not sent. Moreover the mail function does not work it returns false. BUT it i remove FROM and just use: HudsonLofts then it works but the headers it is not displayed properly on my email :(

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alejo_Blue
  • 603
  • 3
  • 12
  • 25
  • Consult the manual on mail http://php.net/manual/en/function.mail.php or use phpmailer/swiftmailer. It'll make this a lot easier. – Funk Forty Niner Oct 26 '16 at 15:43
  • thanks i will check it – Alejo_Blue Oct 26 '16 at 15:46
  • Btw, I removed the "phpmailer" tag from the question since your syntax isn't "phpmailer", it's "`mail()`", two different animals here. – Funk Forty Niner Oct 26 '16 at 15:50
  • thanks i did not find that one so that i use phpmailer instead people will notice it when see the code, but thanks – Alejo_Blue Oct 26 '16 at 15:53
  • Here `$EmailFrom = 'From: HudsonLofts ' . "\r\n";` that ought to fix this. and possibly `'Content-type: text/html; charset=iso-8859-1' . "\r\n";` all taken from "the manual". – Funk Forty Niner Oct 26 '16 at 15:53
  • Let me know if that above fixes it ^ and to post an answer for it, given there is one that was posted below but IMHO, don't believe is the solution here and I sure hope he doesn't edit it in regards to what I posted in comments. – Funk Forty Niner Oct 26 '16 at 15:58
  • hey this is what i tried: $EmailFrom= 'From: HudsonLofts' . "\n"; $EmailFrom.= 'MIME-Version: 1.0' . "\n"; $EmailFrom.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; BUT the mail function return false. WIll it be because the rest of the headers are qith quotation marks and not apostrophes? thanks bud! – Alejo_Blue Oct 26 '16 at 16:02
  • I have realized that when i use FROM: the mail is not sent. Moreover the mail function does not work it returns false. BUT it i remove FROM and just use: HudsonLofts then it works but the headers it is not displayed properly on my email :( – Alejo_Blue Oct 26 '16 at 16:50
  • Might help: http://stackoverflow.com/questions/2014081/problem-with-php-mail-from-header – CD001 Oct 27 '16 at 08:06
  • @CD001 thanks bud! i already tried adding that 5th parameters in that function without sucess – Alejo_Blue Oct 27 '16 at 14:53
  • One of the lower rated answers on that question points out that your email address format is, strictly speaking, invalid so the `From` header *may* fail - try just `From: staff@hobokenhomes.com` - you could also try adding a `Reply-To: staff@hobokenhomes.com` header. There's also the possibility that it's being stripped by the recipient's mail server. – CD001 Oct 27 '16 at 15:02
  • This is what i tried but not success. thanks though. $Headers= "From: staff@hobokenhomes.com"; $Headers.= "Reply-To: staff@hobokenhomes.com". "\n"; $Headers.= "X-Mailer: PHP/" . phpversion()."\n"; – Alejo_Blue Oct 27 '16 at 15:50

2 Answers2

0

See http://php.net/manual/function.mail.php
Try to use \n instead of \r\n in $EmailFrom.

BTW: I would rename $EmailFrom to $additionalHeaders ;-)

EDIT after comment:
If this doesn't work, try just with this one line for the headers:
$EmailFrom = "MIME-Version: 1.0";

Second guess: Omit the \r\n at the last header line.

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
  • #1: that shouldn't make a difference. #2: *"BTW: I would rename $EmailFrom to $additionalHeaders"* and do what with that exactly? – Funk Forty Niner Oct 26 '16 at 15:46
  • it did not work :( when i try to call mail(to, subject,,,,etc) just return a 0 :( thanks though – Alejo_Blue Oct 26 '16 at 15:49
  • That actually depends quite a **lot** - the RFC specs on line terminators for email headers are `\r\n` - *however* if you're using Postfix as the mailserver that expects the line terminators to be of the form native to the operating system ... practically though, it's unlikely to break either way. – CD001 Oct 26 '16 at 15:49
  • #1: Well, just following the manual's advice... #2: Just rename it. I (personally) wouldn't call a variable `$EmailFrom` if it holds things like `MIME-Version: 1.0`... – Thomas Landauer Oct 26 '16 at 15:50
  • i did what thomas suggested me but not fixed the issue :( thanks man – Alejo_Blue Oct 26 '16 at 16:04
0
$EmailFrom= "staff@homes.com";
$EmailTo = "alejo@yahoo.com, hunterno1@gmail.com";
$Subject = "Contact The Loft";

$success = mail($EmailTo, $Subject, $Body, $EmailFrom);

$EmailFrom should be:

$EmailFrom= "From: staff@homes.com";

or possibly - but I haven't checked this out -

$EmailFrom= "From: Staff <staff@homes.com>";

since the fourth parameter allows you to send any additional header, not just the From:, and hence you need to specify the full header.

If it still does not work, be aware that some providers rewrite the From: header to prevent abuse (such as setting "From: barack.obama@whitehouse.gov"). If this is the case, you won't be able to override that. But you might be able to clear the sender with your provider if you talk to them.

When using the wrong form of the parameter, everything "worked" because you weren't really setting any From field. When you started doing so, the server might have just dropped the email in the bit bucket without telling you anything; the mail() function would have succeeded, since the mail was delivered to the local queue. mail() could not know, much less tell you, that the local queue would then see the mail expunged.

LSerni
  • 55,617
  • 10
  • 65
  • 107