Email newsletter from php mail function not sending anymore - php

I have a page that I set up in php to send newsletters to emails who sign up on my website. The code worked just fine when I sent it to 30 emails the other day. Everyone received the email newsletter. Two new people signed up and now when I try to send one, it goes through as if it sent without any errors in the code, but no one ever receives the newsletter.
Would it have anything to do with an invalid email address that stops it from working? As if one of the new ones is not valid and it shuts it down? I did nothing to change the code from when it was working to the previous 30 emails. My hosting company allows over 250 emails per day, so that is not the problem at all.
$mailto = "members#example.com" ;
$headers = "From: chris#example.com \r\n";
$headers .= "Reply-To: chris#example.com \r\n";
$headers .= "BCC: $emails \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$from='members#example.com';
$subject="TEST";
$body='<html><body>';
$body.= "<table><tr><td>$content etc...</td></td> </body></html>";
if (mail($mailto,$subject,$body,$headers)) {
code... ; }
else {
code... ; }

What I would do, is use some SMTP server, say google, and PHPMailer, or anything similar. THis is probably the best solution for you too, because the usage of mail function in PHP might be a bit dangerous. Many mail servers think that these emails are spam, due to them not being sent by a known service.
At first, try finding out if the unreceived emails have landed in the spam folder.
If not, and if your time doe not allow a PHPMailer implementation, try sending the emails manually from a PHP snippet code, see what really happens.

Based on this line:
$headers .= "BCC: $emails \r\n";
you put all the recipients into one single header line.
This may fail for at least 3 reasons:
as soon as this line is longer that 998 characters, the message violates rfc2822 and so the mail server may reject it.
most SMTP servers have a limit on the number of recipients in a single message, so at some point this limit is going to be reached (30 recipients don't seem that much, though).
any serious syntax error in an email address may invalidate the whole line.
Also, it may help to know which SMTP server is used here. If you have access to the mail server logs, obviously you should check that, too.
As a workaround, you may want to loop through your list of subscriber and send one different message to each subscriber in the To field and not use Bcc at all. This is also better to get through spam filters, anyway.

Related

Is there a way reduce the Email Delay of PHP Mail()

I am using a fastcomet website, and I am trying to create a feedback messaging system.
I have created the following function, for organizing a template for sending emails along with the headers accompanied to it.
send_email.php
<?php
function send_email($to,$from,$title,$sending_message){
$message="<html><body>";
$message.=$sending_message;
$message.="</body></html>";
$headers = "From: ". $from . "\r\n";
$headers .= "Reply-To: ". $from ."\r\n";
$headers .= "Return-Path:". $from . " \r\n";
$headers .= "CC:\r\n";
$headers .= "BCC:\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
return mail($to, $title,$message,$headers);
}
?>
With other pages, I use the following code include 'send_email.php', and use the function send_email($to, $title,$message,$headers), and it works. The only problem is that the receival of the email either takes too long, or arbitrary in nature.
Sometimes, I test the feedback system by sending 3 emails. For example: Email1, Email2, Email3 are sent in the following order. Sometimes, I'll receive Email3 first than Email1 or Email2 first than Email3.
In terms of time, sometimes I'll receive the email in a few minutes, but some in a few hours which makes testing difficult because I am not able to know the results of the code until a few hours, which leaves me to wonder whether my code worked or not.
Note:
By using the following code echo "<p>". error_get_last()['message'] . "</p>"; I am able to figure out that the function did run successfully.
I tried testing using both AJAX request and Form submission, and I found that generally Form submission takes less time for it to be received. (although I like the concept of AJAX request, where I don't have to change page)
My question is, is there a way to reduce the time it takes to receive the email? or is there a better way to approach this problem?
You're asking yourself questions where the answer would really lie with a dedicated email provider.
PHP does not "send" the email with mail(). PHP delegates to either a SMTP server or sendmail on the server PHP is running on; from there, each avenue has additional latency:
Depending on the version of software providing sendmail, your mail may fall into a queue to be processed/sent later
Depending on the SMTP server, this queue may also exist there
The recipient SMTP server may have its own processing requirements and ruleset in place, which might introduce more delay.
The "delay" difference you are seeing between AJAX and/or form submission are not due to the method you're doing your request with. They're purely down to statistics and underlying queues.
Switching and effectively "outsourcing" (it's not, but it's the closest word) your outbound email capabilities to a third-party will provide some clarity into this along with a lot of additional features. Most will also allow you to see emails being delayed/not sent/bounced on top, which is another major win. And, to be fair, it's also pretty cheap.

Mail ends up in junk folder - PHP contact form [duplicate]

I wrote a PHP script to send emails.
My script is like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: abc#yahoo.com' . "\r\n";
// Email Variables
$toUser = "someone#yahoo.com"; // recipient
$subject = "testing"; // subject
$body = "<html><body><p>
Example of including an image via html \<img\> tag:
<br>
<img src='../images/profile.jpg'>
<br>
My new picture
<br></p></body></html>"; // content
if (mail($toUser,$subject,$body,$headers)) {
echo "sent";
} else {
echo "failed";
}
Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?
Please try this:
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
Perhaps the problem is that yahoo uses domainkeys verification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.
When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.
For example: SpamAssassin score list
1. Check mail content
As others have hinted it is probably marked as spam because your mail looks like spam.
I am not sure if you the script that you have posted is the actual one that you are testing.
If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9
X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
version=3.2.5
Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.
2. Mail server's IP
Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.
3. Blocklists
Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.
Use mxtoolbox.com to check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.
Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.
Hope that helps!
**This Works Perfectly fine for me**
$to="reciever#reciever.com";
$subject="This is Your Message";
$from = 'Sender <noreply#sender.com>';
$body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
$headers = "From: " .($from) . "\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($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";
mail($to,$subject,$body,$headers);
I was having the same problem:
The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.
But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.
As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.
So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.
Another one is the html contents without the alternative plain text contents, but, this one is less important.
I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)
On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
Make sure you send your e-mails from an existing account on your server/domain.
Make sure you have the reply-to address in your header.
These are the basic things you can try.
if your website domain is mydomain.com then in From headers make sure to use someone#mydomain.com
Remove the Content-type: text/html and add $headers .= "X-Priority: 2\nX-MSmail-Priority: high"; to get rid of Spam. This method has been tried and tested.
the problem is, the server you're sending the mail from is not a yahoo server. most spam filters check if they match, otherwise it would (and is - or was) possible to easily fake the sender. ever wondered why you get spam from bill.gates AT microsoft.com or your own mail address?
You've got two solutions:
use Yahoo's SMTP using abc#yahoo.com credentials to send mail from abc#yahoo.com;
use other from, with your own domain;
You can try the mail class and test file that I have created here. I have tested the files and can send emails to my hotmail and gmail under a different mail name. The main reason why the emails are mark as junk is because the structure (both header and message) is not correctly done. In most cases, it is the line feed that is causing the problem.
I can use it to send mail with attachments to Gmail. However, the attachments dont work for hotmail. Hope this helps =)
You can check the files here..

PHP mail may be filtered out by spam?

I am creating a small company, and would like to send out emails to my clients once they have signed up for my service to activate their accounts. I am currently using PHP's mail() function, however I am worried that my emails are being filtered out by spam filters. Is there a better way to go about this?
$email = 'XZY Client Email address # somedomain.com';
$emailSubject = "Welcome to XYZ Service!";
$to = $email;
$subject .= "".$emailSubject."";
$headers .= "From: no-reply#XYZService.com\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= "Welcome to XYZ Service! \n Activate your account by clicking the following link: link...";
mail($to, $subject, $message, $headers);
Is there a way to authenticate these emails so that my clients know that they are from my actual service? Thank you in advance!
You can try to get your mail server white listed by some of the major postmasters (AOL, Gmail, Hotmail, etc.).
I am not sure if this is what you want, but you can setup a mail server to handle all your emails.
Try using that mail server for sending emails and see if they are still filtered or not.
BTW, setting up a mail server on *nix systems is free and worth giving a try.
I have a simple arcade site that people from my highschool use and for quite a while I've been using the method that you showed. I didn't look at my code to verify that they're identical, but they look fairly close and mine works with gmail and all other big name services I've ran into!
Just remember that some hosting companies limit the amount of PHP mail that can be sent per minute. Mine only allows 9 per minute, I know I've gotten suspended a few times on accident while running test mail sends, haha.

Safely send many emails in PHP

I'm building a website in PHP that allows users to send invitation emails. They are allowed to send more than one at once.
I'm told that using PHP's mail() function is a bad choice because it can easily be marked as spam. Is there some way that I can set up this script so that the emails are not marked as spam?
I've done quite a lot of reading and from what I've read today it seems like a few factors can influence whether mail is marked as spam including: the words used in the emails; the domain that the emails say they are sent from compared to where they actually originate; formatting of headers; and the frequency of emails sent.
The email addresses that we use are with Gmail Apps. So, if the emails I send don't "look" spammy, I send them from Gmail, and I can limit the frequency of the emails sent (maybe 10 every 10 minutes) then I should be able to do this without a problem right?
EDIT
This is not for a newsletter. Do I even have to worry about being marked as spam for invitation emails?
while your conclusion is correct, your reasoning is not quite right. php's mail function will not cause your emails to be marked as spam, because it sends the same emails as anything else would.
the problem with php's mail function is that it is very low level, and so if you do not know how to use it properly, and leave email injection vulnerabilities, people could use your site to send spam messages through it, and in turn this could get your server blacklisted as a source of spam by isps. as long as you strip \n and \r from your extra header fields (From:, etc), you should be safe.
Another issue is that you have to add your own valid headers to your emails.
Yet another issue is that it would connect and disconnect from your smtp server for each time you call mail(), although I think there is a way you can send multiple emails using one call to mail. this would be complicated though since it would probably involve looking through rfc282 and figuring out how to do it.
at first you need an valid mx.record for your domain...
if you use the mail function you should send valid headers like these:
function send_mail($from = FALSE,$to = FALSE, $msg = FALSE, $subject = FALSE, $html= TRUE){
if($from && $to && $msg && $subject){
if ($html){
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
}
$header .= 'From: '.$from. "\r\n";
$header .= 'Reply-To: '.$from. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
return mail ($to,$subject,$msg,$header);
}
return "fail";
}
you need to use a SMTP server and after that connect using any php library for php. Here you have some useful libraries and some examples as well
http://phpmailer.worxware.com/
http://swiftmailer.org/
http://framework.zend.com/manual/en/zend.mail.html

E-mails sent from shared hosting, blocked. Is there a way to avoid that?

in my website i've a PHP script that automatically mails to my customers a confirmation about the order.
My website's domain is registered to a company that hosts my website too.
I have a lot of problems to send e-mails using mail() function to some e-mails accounts ... a lot of my users contact me saying that they have never received my automatic e-mail! So this is a very big problem!
The accounts, that give me more problems, are comcast.net, uol.com, mchsi.com and others! I've contacted the support center of these e-mail service providers asking for removing my ip from the block list too.
The header of the e-mail looks like this:
$header = "Sender: $from_mail\n";
$header .= "From: Account <$from_mail>\n";
$header .= "Reply-To: Account <$from_mail >\n";
$header .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\n";
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$body = "\n--$mixed_boundary\n";
.
.
.
.
$body .= "\n\n--$mixed_boundary--";
mail($to, $subject, $body, utf8_encode($header), "-f$from_mail");
There is something that I can try to avoid this problem?
Someone knows where can I look to know what is the IP address of the server used to deliver e-mails thought the PHP mail() function?
Thanks in advance for your replies!!!
Hi,
still today some e-mail provider service such as Comcast, continue to block my IPs addresses saying that my mail server sends spams ... I requested the removing from the blocklist, but their system continue block them! I don't know what else can I do ... I've followed your suggestions and the code look like this:
$md5 = md5(date('r', time()));
$mixed_boundary = "PHP-Mixed-$md5";
$alt_boundary = "PHP-Alt-$md5";
$header = "Sender: $from_mail\r\n";
$header .= "Errors-To: $from_mail\r\n";
$header .= "From: account <$from_mail>\r\n";
$header .= "Reply-To: $from_mail\r\n";
$header .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\r\n";
$header .= "Mime-Version: 1.0\r\n";
$header .= "X-Mailer: PHP/".phpversion()."\r\n";
$body = "\n--$mixed_boundary\n";
$body .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n";
.
.
.
.
$body .= "--$mixed_boundary\n";
$body .= "Content-Disposition: attachment filename=\"...\"\n";
$body .= "Content-Type: application/octet-stream; x-unix-mode=0644; name=\"...\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
.
.
.
.
$body .= "\n\n--$mixed_boundary--";
mail($to, $subject, $body, utf8_encode($header), "-f$from_mail");
Suggestions?
Thanks again!
Let me explain the battle you are facing. Forget about the technical details for a minute.
There are hundreds of email providers out there. The big ones are Yahoo, Gmail, AOL, Hotmail, etc. If you are blocked on anyone of these services your business could be severely affected.
These email providers are extremely concerned about spam and in order to combat spam have gone to the extreme of blocking any messages that might be spam. You might never get spam complaints but if another business, on your shared hosting plan, gets spam complaints you will be effected. It doesn’t matter how you configure your application if you are on a suspected spam IP address your messages will be sent to the spam folder.
Even if you have your own server you will still have issues. Over time if you send emails some of the recipients will hit the spam button. This is a fact of life and there is nothing that you can do to prevent it.
The only solution is to outsource you email delivery. Companies like Aweber or iContact deal with email delivery issues for you. They have relationships with all major email providers and work hard to ensure that your messages get into the recipients inbox.
You no longer have to worry about contacting Yahoo or Gmail because someone hit the spam button. You can focus on more important things.
Your message headers are not valid according to RFC2822 Internet Message Format.
From 2.1 General Description:
Messages are divided into lines of characters. A line is a series of
characters that is delimited with the two characters carriage-return
and line-feed; that is, the carriage return (CR) character (ASCII
value 13) followed immediately by the line feed (LF) character (ASCII
value 10). (The carriage-return/line-feed pair is usually written in
this document as "CRLF".)
As I pointed out in my comments, the reason why your email may work with most mail servers anyway is that they may be liberal in what they accept. There may be some mail servers, however, that will discard your messages since they don't comply with RFC2822.
EDIT: Although the use of "\r\n" is advocated in the PHP documentation for the mail() function, there is some debate about whether that's really the right thing to do.
The mail() function will communicate with the local sendmail(8) command (or whatever is configured in sendmail_path), and the line endings may be processed differently depending on what mail transfer agent implementation is used. From what I understand, sendmail(8) should be OK with "\r\n", but qmail(7) for example will replace "\r\n" with "\r\r\n", which will probably break the message.
This all happens before the email is delivered to its final destination, so it may be easily tested whether the line endings are processed properly by sending one's self an email message constructed with "\r\n" and by verifying that all headers are present.
See also: RFC2822, PHP mail() function, sendmail(8), qmail(7)
You could send yourself an email and check the headers. That will give you an idea of the path an email might take, but there's really nothing preventing emails to those other domains from going through a different path.
In general those big ISP's have really heavy duty spam filters, so mailing to them from a shared host is going to be tough. If you can get your own IP address and send mail from there, it will probably help. Then you can set up SPF records. No guarantees, but that will definitely elevate you out of the pit of commercial spammers a bit.
You could also do some digging with link text and some of the other anti-spam services and see if you're doing anything else wrong.
Asking customers to add your from address to their spam whitelist probably wouldn't hurt either.
I've found that setting Return-Path, Sender, and Errors-To headers help in some cases.
Use PHPMailer (http://phpmailer.worxware.com/) for building the email-messages. Maybe it's not the IP, but something else. There are a lot of things, where spamfilters react allergic (like missing Message-ID etc.). Plus with PHPMailer you have the opportunity to send your mails via mail() or if it dont't work due to shared-hosting-problems via SMTP over an other host (like Gmail).
I've noticed that with my Linux hosting server, I have to replace all occurrences of CRLF with LF in both the extra headers and the mixed/alternative headers. About that the PHP documentation says:
"If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822."
I've tried to send a mail (only with "\n") to my personal account and I've looked for "\r" and "\n" in the raw message ... each line is ended by a CR and started with a "\n"!
QUESTION: As the PHP mail() documentation says, I use the function wordwrap() to cut the line length to 70 characters.
There is a workaround to let the mail client to display the message with its original formatting and not as a column where each line isn't longer than 70 chars?
ANSWER [SOLUTION]: I have solved setting up quoted-printable as Content-Transfer-Encoding:
$body .= "Content-Transfer-Encoding: quoted-printable\n";
$body .= "Content-Type: text/plain;\n\tcharset=utf-8;\n\tformat=flowed;\n\tdelsp=yes\n";
$body .= "\n" . quoted_printable_encode($message);
The quoted_printable_encode() function is available only with PHP 5.3, the implementation is suitable in the documentation page.

Categories