PHP Sendmail Gives Up - php

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.

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?

Mail issue on live server

I mail using php mail function to send e-mail. In my dev server its working fine but in live server e-mail goes to spam folder.
<?php
$to = "example#gmail.com";
$subject = "Registration";
$from = "example1#gmail.com";
$content = "Test";
$headers = "From: Test <" . strip_tags($from) . ">\r\n";
$headers .= "MIME-Version:1.0" ."\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$res = mail($to,$subject,$content,$headers);
$headers = "From: Test <" . strip_tags($to) . ">\r\n";
$headers .= "MIME-Version:1.0" ."\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$res2 = mail($from,$subject,$content,$headers);
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.
Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are gone.
https://github.com/PHPMailer/PHPMailer
answered by mrdognose
and use full headers..
http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/
Prevent sent emails treated as junk mails using php mail function

Form doesn't send info to email address (works on others though)

My client has a Wordpress content management system to which I added a simple contact form with a php form handler. The contact form sends the information by email correctly to all three of my email addressses, but when I change to my client's email address the email never arrives. I have run out of ideas where I could look for the problem. No it does not go to his junk mail folder. :)
Sounds like the email is being routed "internally" through your clients network and not out onto the internet. The chances are they have some restrictions on what machines can be used to send emails internally, or the mail routing system sees the internal email as being "different" and does something odd with it.
Try using (from a cli) :
echo "Testing " | mailx -"Test Subject Line" user#company.co.uk
What is the mail function that you are using? Do you attach a header to it? It sounds like it is being marked as spam from the exchange server. What I use (and have always worked for me) is something like this:
`
function mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="")
{
$subject = nl2br($subject);
$sendmailbody = nl2br($sendmailbody);
if($bcc!="")
{
$headers = "Bcc: ".$bcc."\n";
}
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8 \nContent-Transfer-Encoding: 8bit\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/"."MIME-Version: 1.0\n";
$headers .= "From: " . $from . "\n";
$headers .= "Content-Type: text/html\n";
mail("$sendto","$subject","$sendmailbody","$headers");
}
`

Mail generated in PHP going to spam [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: How to avoid a system generated e-mail going into spam?
How to send 100.000 emails weekly?
I am using the following script to send mail
$to = 'name#test.com' . ', ';
$to .= 'name2#test.com';
$subject = 'Green apple';
$message = 'Enquiry posted by test ';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Green Apple <info#greenappleme.com>' . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo "mail send successfully";
}
else
echo "mail can't send";
When I use this script to some servers, the mail is going to spam. But in some servers, it is going to the Inbox as desired.
How can I prevent email going to spam?
To prevent an email going into spam, don't send with mail from PHP. Send it with SMTP from your server, you can use PHP to connect to SMTP and submit the message. You will then need to set the SPF records on your server, and reverse DNS records with whoever your IP address comes from. If you do these three things then you'll be on the whitelist and all your emails will go into the inbox everywhere, assuming you don't abuse the privilege and get put on a blacklist.
So: send using SMTP, research SPF records and reverse DNS.
You won't be able to do this unless you have a dedicated server with dedicated IP for the domain from which you are sending the email from.
Be sure to set the RETURN PATH as the optional 5th parameter to the mail() function.
if (mail($to, $subject, $message, $headers, '-finfo#greenappleme.com'))
TO send ur majority mail in inbox set below headers and dont use test like keywords in your subject line
$headers = "From: My site<noreply#example.com>\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

php mail function not getting the emails

ok, so I put together a very basic mail function and while testing this, I used a couple of email accounts, one my google account and the other my work account. I get all emails at the google account, but not to those pointing at my work. I'm thinking that could be because they have been caught up with the anti-spam software. Any ideas on how can I develop the mail function to avoid being caught on with spam software?
Here is a copy of my mail function
$to = 'account#gmail.com';
$subject = 'The subject';
$message = 'Hello,'."\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers = 'From: me#mycompnay.com' . "\r\n" .
'CC: anotherone#mycompany.com' . "\r\n";
$mail_sent = mail($to, $subject, $message, $headers);
if($mail_sent) {
header("location:newlocation.php");
}
}
A lot of times this has less to do with PHP's mail() function and much more to do with the configuration of your mail transport agent. A lot of mail servers will bounce messages they assume are from spammers (i.e. unconfigured/misconfigured senders) before they even get passed to a spam filter.
If you check your MTA's logs you'll probably find some bounce messages the likes of, "Mail from this server not allowed, see blacklist info at [insert url].
Spam filters use a lot of different methods to determine if the mail coming in is actually spam or not.
Here are a few things that I would suggest:
Descriptive subject line
Descriptive message, careful with HTML and other rich content inside the body as sometimes spam filters will pick up on it as an "advertisement".
Full complete headers with realistic information in there as much as possible.
Try experimenting with different combination's and see if you can get one through to your work. The good thing is that your google account got the e-mail so you know its not an server side issue locally.
you probably need to format your headers and content properly. boundaries are missing.
Here's one simple function with HTML formatting mail:
<?php
function html_mail($i){
$to = $i['to'];
$to_name = $i['to-name'];
$subject = $i['subject'];
$html_message = $i['message'];
$from = $i['from'];
$from_name = $i['from-name'];
$reply_to = $i['reply-to'];
$reply_to_name = $i['reply-to-name'];
if(!$to || !validate::email($to)){return false;}
$email_message = '';
$email_subject = $subject;$email_txt = $html_message;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$email_to = ($to_name ? $to_name.'<'.$to.'>':$to);
$headers = "From: ".($from_name!='' ? $from_name.'<'.$from.'>':$from)."\n";
if($reply_to){
$headers .= "Reply-To: ".($reply_to_name ? $reply_to_name.'<'.$reply_to.'>':$reply_to)."\n";
}
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/html; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= $email_txt;
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/plain; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= trim(strip_tags(str_replace(array('<br/>','<br />','<br/>'),"\r\n",$email_txt)));
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}--";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
return $ok;
}
?>
when you have a properly formatted mail, you probably will be able to by pass the filters.
Spam determination is entirely determined by the software that's running the spam heuristics. You would have to look into the anti-spam software that your company uses and see why it's being caught as spam. More often than not it has to do with your mail server set up. A key factor that a lot of software uses is a valid reverse DNS entry, so you could look into that.
You have to realize that if there was an easy way around anti-spam software catching your email as spam just by modifying a few headers, then anti-spam software would be entirely useless, since the spammers would know those methods as well.
Adding a valid 'from' header would be the first thing to do, I think.
and thank you very much for all your suggestions. I found the answer on this post
How to change envelope from address using PHP mail?
It worked.
L.

Categories