PHP mail() not sending - php

I have a WebFaction server and the following code:
$to = "outreach#bmun.org";
$reply_to = "From: " . $_POST['email'];
$name = $_POST['name'];
$subject = "Outreach Request Session for " . $_POST['school'] . " on " . $_POST['date'];
$em = $_POST['message'] . "\n-" . $name;
$sentmail = mail($to, $subject, $em, $reply_to);
$sentmail returns true, but the email is not sending for some reason.

$sentmail returns true, but the email is not sending for some reason.
The mail function in PHP simply sends the mail via an MTA (mail transfer agent) on the server. A true can just mean the local MTA accepted it. But that is not all you need.
First, does your hosting provider actually allow outgoing mail? Or are messages sent to a virtual “black hole?”
Now, let’s assume that your local MTA—most likely sendmail—works, and the mail jumped off of the server & made it into the real world. Okay, great!
But not so fast…
The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.

You don't have any headers in your email.
Although they don't seem to be required, your emails will be blocked by spamcheckers a whole lot sooner.
This is an example:
<?php
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$status = mail($to, $subject, $message,$headers);
?>

Related

How to format an email that Hotmail / Outlook is happy with?

$body = 'This is a test';
$subject = 'Confirmation';
$headers = 'From: Testing Site' . "\r\n";
$headers .= 'Reply-To: admin#myserver.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
$headers .= 'Delivery-Date: ' . date("r") . "\r\n";
//$headers .= 'Message-Id: <20140316055950.DA8ED58A13CE#myserver.com>' . "\r\n";
mail("example#hotmail.com", $subject, $body, $headers, "-f admin#myserver.com");
mail("example#gmail.com", $subject, $body, $headers, "-f admin#myserver.com");
Emails send fine to Gmail but are always rejected by Hotmail with this error:
host mx1.hotmail.com[65.55.33.119] said: 550 5.7.0 (COL0-MC5-F28)
Message could not be delivered. Please ensure the message is RFC 5322
compliant. (in reply to end of DATA command).
Message ID header is generated automatically by the server but it doesn't help to supply one manually either.
Why isn't Hotmail happy?
Mail server has SPF record, reverse DNS, is not blacklisted and passes all checks at mxtoolbox.com.
The From header is invalid. It must have the following syntax:
From: "name" <email-address>
In your case:
From: "Testing Site" <admin#myserver.com>
The same goes for your Reply-To header:
Reply-To: "Testing Site" <admin#myserver.com>
Which you can omit if it's the same as the From header (like in your case).
PS: RFC 2822 doesn't state that the display-name in an address should be quoted. In other words: the following 3 headers should all work:
From: "Testing Site" <admin#myserver.com>
From: 'Testing Site' <admin#myserver.com>
From: Testing Site <admin#myserver.com>
If you're using WordPress, you can look up plugin for Hotmail/Outlook friendly emailing capability.
However if it is a standalone script you might wanna look into Microsoft's official answer to this query on the URL : http://answers.microsoft.com/en-us/outlook_com/forum/oemail-osend/why-are-the-emails-sent-to-microsoft-account/b64e3e4a-0d93-40c8-8e28-4be849012f9c
In-short Email-Server provider has to fill this form (once) : https://support.live.com/eform.aspx?productKey=edfsmsbl3&ct=eformts&wa=wsignin1.0&scrx=1
In order to get their emails accepted by Hotmail/Outlook.
Using the PHPMailer library to send mail instead of the mail() function has finally sorted this problem and is the working solution for me. Answer by Jasper N. Brouwer probably more correctly answers the question though I've not had a chance to try it.
1 ) Go to SPF record wizard
2) create a new SPF record for your DNS domain
3) Add that DNS record to your domain's DNS
4) if you fail somewhere in the process, read the detailed SPF record specification
After you complete this process HOTMAIL will be happy with your email.

from address come with server extension in mail

from address come with server extension, errror info#gmail.com via ecbiz132.hostername.com . how to solve this
$subject = "confirmation";
$from = "info#gmail.com";
$to = $email;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:<'.$to.'>' . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";
$senad_replay = mail($to, $subject, $content, $headers, $from);
// errror info#gmail.com via ecbiz132.hostername.com . how to solve this
Yes, you can get rid the "via" part. Here's the details:
1) SPF and DKIM
Firstly, you would need to set an SPF record for the domain you are sending emails from and enable DKIM as well. These are primarily for identifying your messages against spam.
2) "From: anything#yourdomain.com"
Secondly, make sure you are setting the “From: ” header to be an email address on the domain you are sending messages from. Don’t pretend to be someone else. Use “From: someone#abc.com” if you are sending the messages from abc.com, rather than anything else, such as blah#def.com, or yours#gmail.com, or whatever. If you want the recipient to reply to your Gmail email instead of your domain email, use the “Reply-To: ” header. “From: ” must always be the domain email that you are sending the email from.
3) "Return-Path: return#yourdomain.com"
Thirdly and most importantly, set the “Return-Path: ” header to be the same domain as that of the “From: ” header. Use the 5th parameter of the mail() function for this:
mail('recipient#example.com', 'Subject', "Message Body", $headers, '-freturn#yourdomain.com')
So the Return-Path of this message would be “return#yourdomain.com” (the email address immediately following the -f switch). The $headers parameter should contain all the necessary message headers. Make sure “From: ” is something#yourdomain.com.
After these steps and measures, Gmail should now completely trust your messages from yourdomain.com. The ‘via‘ field of your messages should be gone and the ‘mailed-by‘ field as well as the ‘signed-by‘ field should be correctly showing up as yourdomain.com.
Hope it helps!

PHP Mail function do not sent mails

I have som html mail which I want to send via PHP Mail function,
so I have this:
$to = "mail#mail.sk";
$from = "mail#mail.sk";
$subject = "Some subject"
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Return-path: $from" . "\r\n";
$headers .= "Bcc: $from\r\n";
$message = "some html";
mail($to, $subject, $message, $headers);
But it always didnt send a mail, if i test it:
if (mail($to, $subject, $message, $headers)) {echo "Sent";}
else {echo "Didnt send";}
I always get Didnt send and no error message. What can be wrong ?
Check your PHP error log. It might have more details as to what's failing. Usually an outright mail() fail is due to a misconfigured SMTP server setting, or lacking an SMTP server at all.
It can also fail if the SMTP server rejects the email, so check the SMTP server logs and see if it's complaining about something in your mail's setup.
Beyond that, don't use mail() for mime-formatted emails. Use something like PHPMailer or Swiftmailer to do it for you. They're far more reliable and provide MUCH better diagnostic error messages when something does blow up.
Check if all vars are filled. If so..
Turn error and warning reporting on <?php error_reporting(E_ALL); ?>
If nothing, check to see if your logs show any information.
Test to see if the mail arrives on other adresses. For example a gmail account.
If nothing check your server and SMTP settings.
If that all fails, try swiftmailer using an (external) SMTP.

how to know if php mail failed

I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if(!mail($to,$subject,$message,$headers))
{
echo 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.

php mail() doesnt work for gmail and hotmail

I am trying to send new system generated password using mail() in php. The thing is I am able to send it to yahoo but when I use gmail or hotmail I dont receive any emails although the function returns true. Following is the function:
if(mail($to,$subject,$body))
{
return true;
}
else
{
return false;
}
It probably ends up in the spam folder look there. If its there make sure your email headers are perfect.
You could look into librairies for what you want to achieve. Zend_Mail has everything you could need to connect to gmail and others.
If you are getting the mail successfully through yahoo, you should also post the headers that are coming through from yahoo here in the question. My bet is you will need to include a "from field" also to get through on hotmail, gmail, etc...
Your problem might be the antispam filters. E-mails sent from PHP are usually marked as spam by the mail servers and end up deleted or in the spam can.
You can Google for "php mail spam" to get some hints of how to work around this issue.
Maybe you're on a shared server and the IP is banned/blocked due to spamming by other users (websites) of your server.
Try adding SPF records.
Ensure that your envelope-FROM (a.k.a. return path) is set to a valid email address that you have access to. If you're not seeing the message in the spam folders, it should be getting bounced; the bounce message may offer a clue as to why the mail is not getting through.
I would try to include your own headers in your mail function
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Date: ". date('r'). " \r\n";
$headers .= "Return-Path:youremail#domain.com\r\n";
$headers .= "Errors-To:youremail#domain.com\r\n";
$headers .= "From:youremail#domain.com <youremail#domain.com>\r\n";
$headers .= "Reply-to:youremail#domain.com \r\n";
$headers .= "Organization: YourOrg \r\n";
$headers .= "X-Sender:youremail#domain.com \r\n";
$headers .= "X-Priority: 3 \r\n";
$headers .= "X-MSMail-Priority: Normal \r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to,$subject,$body,$headers);
Try checking if your mailserver ip is blacklisted anywhere?
http://www.mxtoolbox.com/blacklists.aspx
If not, try harder with the headers that are being sent with the mail.

Categories