PHP mail() -f parameter is NOT working - php

I've researched this intensely. Here, at Stack Overflow, I've figured out that one needs to use an -f parameter with the php mail() function, if one wants undeliverable mail to bounce back. Following is my script (as it stands now):
//Send Confirmation email. Following are the variables for the email
// mail function best practices: http://collaborate.extension.org/wiki/Best_Practices_Using_the_PHP_mail_Function
$sendto = $email; // this is the email address collected from the foreach routine.
$e_subject = stripslashes($subject); // Subject
//$message = "<html>" . stripslashes($body) . "</html>";
$message = "
<html>
<body>
<p>Hello " . stripslashes($fName) . ":</p>
<div>" . stripslashes($body) . "</div>
</body>
</html>
";
// Always set content-type when sending HTML email
$header = "MIME-Version: 1.0" . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
// extract user domain so you can set up X-Mailer
$u_domain=substr(strrchr($user_email, '#'), 1);
$dom_array = explode(".",$u_domain);
$user_domain = $dom_array[0];
$header .= "X-Mailer: ". $user_domain ."\r\n";
$header .= "X-Sender-IP: {$_SERVER['REMOTE_ADDR']}\r\n";
$header .= "X-Originating-IP: [".getenv("REMOTE_ADDR")."]\r\n";
$header .= "From: " . $user_email . "\r\n";
$header .= "Sender: ". $user_email . "\r\n";
// The "envelope sender" is the address listed in the "Return-Path:" header - and controls where the email is sent to in the event that a recipient address bounces. http://collaborate.extension.org/wiki/Best_Practices_Using_the_PHP_mail_Function
$header .= "Return-Path:" . $user_email . "\r\n";
$header .= "Reply-To:" . $user_email . "\r\n";
$bounceTo = "-f". $user_email;
// Collect variables from above and insert into the mail() function.
mail($sendto, $e_subject, $message, $header,$bounceTo);
You'll notice a lot of commenting - I'm just trying to figure this out. My mail() sends wonderfully. The mail is coming into my inbox with formatting as it should be. But... the $bounceTo variable ("-f" . $user_email) is not working. I've intentionally mailed to 3 known inactive addresses, and I'm not getting any bounce backs.
All the header settings in the above code are in place because I've learned that these may affect bounce backs. I'm totally willing to get rid of un-necessary headers and add what is necessary. But... at this point the script seems to be a mess -which is not producing bounce backs.
Any suggestions are welcome.
Thanks Much:
Pavilion

Have you looked at the comment posted at http://www.php.net/manual/en/function.mail.php#107321. That might lead you in the right direction.

This thread explaining bounced mail in php might be of benefit to you. I personally have never had to use the -f parameter to handle bounced emails.

Overriding the bounce address with -f is not allowed on all servers, especially if you are on a shared hosting server this is often not possible. In this case, it's often better not to use the very limited mail() function but use a smtp library like phpmailer or swiftmailer instead.
Btw: you don't have to send mails to an inactive address to check your bounce address. Send them to an active account, in the message source look for the "Return-Path" header, this is the bounce address.

Related

PHP Sending Mail with Headers to Gmail

The mail() function in PHP works fine when sending to Gmail if I don't specify any headers. However, as soon as I try to add in headers, the function still returns true but I never get the email. The Gmail server seems to reject the delivery.
These are the headers I'm using:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $username . ' <' . $email . '>' . "\r\n";
$headers .= 'From: Blahblah <email#example.com>' . "\r\n";
mail($email, 'Subject', '<html><body>Body of message</body></html>', $headers);
I was hoping that someone could point out to me the flaw? I grabbed most of this code straight from the PHP manual, so one could understand my confusion and frustration. Thanks!
The bigger email services will not deliver mail sent from your own personal server in this manner.
Due to SPAM issues. All the big mail providers require SPF records, DKIM, and reverse DNS before they will accept your mail.
Some live/hotmail user's are not receiving html mail
Personally I prefer to be rid of that hassle and use a 3rd party mail server for all of my outgoing mails to my users.
$Headers .= "From: $Yourname <$YourEmail>\r\n";
or
$Headers .= 'From: '.$Yourname.'<'.$Youremail.'>'."\r\n";
or
$Headers .= 'From: '.$Yourname.' <".$Youremail.">' . "\r\n";
Give a try if any of this work?

PHP mail from address header

I'm using PHP mail function to send messages from my web app.
I've created a table which holds the e-mail addresses and messages to send. The script fetches a certain number of messages and sends them.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=' . $charset . "\r\n";
$headers .= 'To: ' . $destName . ' <' . $destAddress . '>' . "\r\n";
$headers .= 'From:' . $fromName . ' <' . $fromAddress . '>' . "\r\n";
$headers .= "Reply-To: ". $fromName . ' <' . $fromAddress . '>' . "\r\n";
mail($destAddress, $subject, $msgBody, $headers);
My problem is that, even setting the FROM and REPLY-TO header, the address shown on the list of received messages is strange (not the one I've sent). See picture below,
But when I open the message, it seems all is OK, ie the sender is the one I've configured.
Can anyone help me?
If there would be nothing that shows that the "from" and "reply-to" field are different from the address from where the mail was sent, then everyone would be able to sent you a mail coming from for instance obama#whitehouse.org
If you want the email of your choice to be shown, you should use smtp, log in to the mailserver with the correct account, and sent the mail. This will cause the mail to be verified and trusted.
http://www.9lessons.info/2009/10/send-mail-using-smtp-and-php.html
This is probably an issue with the web interface of dispostable.com which does not show the contents of the "from" but the address of the sending mailserver in the overview. This might be a security measure. Dis you try to send to a "normal" address?
By the way, the address in "from" should be resolvable to the server from where it was sent. For example a mail from admin#abcdefg.com should come from the server ip of abcdefg.com. Else it will be calssified as spam by some mail clients or mail servers.

PHP Sendmail Gives Up

So, its coming towards the time of year again when our sports club mailing list gets swamped with new members (happens with the new academic year).
Last year we tried sending emails using php's mail() function.
This worked fine for around the first 50 or so (and continues to work fine sending one email at a time). However, after around 50, mail() claimed it had sent the mail, but no one ever received them on the other end.
I should point out, that in my implementation it simply does a loop through all the emails in our database and runs the following function:
function sendMail($from,$fromname,$to,$subject,$body){
$subject = stripslashes($subject);
$body = nl2br(stripslashes($body));
$headers = '';
$headers .= "From: $fromname <$from>\n";
$headers .= "Reply-to: $fromname <$from>\n";
$headers .= "Return-Path: $fromname <$from>\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Date: " . date('r', time()) . "\n";
return mail($to,$subject,$body,$headers);
}
Does anyone know what could have caused this?
You are probably being blocked by ratelimit on the SMTP relay.
I would suggest instead of sending individual emails, set everyone to the BCC field, with no one in the TO and CC field.
$headers .= 'Bcc: ' . implode(",", $email_array) . "\r\n";
Using Bcc without "To:"-Header makes the E-Mail "To:"-Header "undisclosed-recipients", those mails usually getting blocked by strict servers. I would not recommend this for newsletter stuff,ยด you will get blacklisted.
If you send this mail to a few users at the same ISP, you will get blacklisted for sure.
I would recommend a script which sends an amount of mails every 30 mins or so.

php mail() recipient address missing

My website runs the following code: (I BCC myself so that I have a copy of all the emails that my website sends out)
//prepare email headers
$headers = "From: " . "info#mysite.com" . "\r\n";
$headers .= "Reply-To: ". "info#mysite.com" . "\r\n";
$headers .= 'Bcc: sent#mysite.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = getMsg( ... );
mail( $buyer_email, 'mysite.com - Verify your information.', $message, $headers );
$message = getMsg( ... );
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers );
The emails get sent out perfectly fine. The problem is with the second email that gets BCC'ed to me. The recipient's email address is blank so I can't see who the email was sent to. The first email that's BCC'ed to me is fine, all the info shows up. In other words, I can see $buyer_email, but I can't see $seller_email. Any ideas?
You can debug it like this
echo "Seller Email: $seller_email";
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers )
The page will print the seller email and you can see what it actually is.
Addition
If you can not use the above code because you have to test it a user (which is normal btw), use the following technique.
Since you are getting the first email, send $seller_email as part of test code in that email and see what value it has.
$message = getMsg( ... );
mail( $buyer_email, "mysite.com - Test Seller Email: $seller_email .", $message, $headers );
You will find out the seller email value in the email you get.
Does sending additional headers help? (see mail()) That way you don't have to use 2 mail functions.
Like this:
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
I'm very sorry, but I made a huge mistake. Long story, but basically I was confusing code on one page with very similar code on a different page. I was testing the validity of $seller_email on the wrong page. On the page in question, it was in fact NOT being set. Once again, sorry. I should have posted the entire code.

PHP mail() function does not work on web-host

I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.
I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.
Thanks,
Amit
For clarification purposes, here is the mail-script.
<?php
$to = 'my#email.com';
$subject = 'Contact from your website';
$message =
'Below are details from the Contact Us Form ' . "\n\n" .
'Name: ' . $_REQUEST['name'] . "\n\n" .
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" .
'E-mail address: ' . $_REQUEST['email'] . "\n\n" .
'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];
if( $str != "10" || $strE == "")
{
echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"><!-- EDIT THIS -->
<?php
} else {
mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"> <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
}
?>
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail#test.com>\n";
$headers .= "Reply-to: <sendingemail#test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail#test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail#example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

Categories