PHP email Headers - delivery option - php

I've added the following code to our PHP Mail app which sends out emails:
$email_header .= "Disposition-Notification-To: $from";
$email_header .= "X-Confirm-Reading-To: $from";
However, we are not receiving any 'Delivered' or 'Read' confirmations.
Any thoughts?
Thanks,
H.

The reason that you are not receiving any confirmations is that most people choose not to send read receipts. If you could, from your server, influence whether or not this happened, a spammer could easily identify active and inactive email addresses quite easily.
However, another reason why this might be failing, if sending read receipts is enabled, is that you need to include a new line at the end of every line:
$email_header .= "Disposition-Notification-To: $from\r\n";
$email_header .= "X-Confirm-Reading-To: $from\r\n";

I believe one header for getting read notifications was Return-Receipt-To:. Also, users usually have the option to ignore the "read receipt", and not send the e-mail back, if the header is not ignored completely by the client.

(maybe not the answer to the question, but the probable answer to what you are trying to achieve is). Your method won't work most of the times as confirmations are usually disabled. Use a image tag to record the email reads in the email. It won't work if the loading of images is disabled.

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 mailer setting from address in mail function

Ok so I set up a php mailer on 2 separate pages on a site I am working on. I had previously worked through everything in the mailer on a site I built and it was straight forward how to set the email after setting all the variables:
mail("$email",
"Receipt: $thesubject",
"$message",
"From: $replyemail\nReply-To: $replyemail");
So this was what was at the end of the contact (processing) page. I have $email and $replyemail being put into the $message and they come out correctly in the message that gets sent to my email specified by $email.
The part I don't really understand is the address the message says it is coming from is not that $replyemail but instead it says it is being sent from:
rtl.srv#gmail.com
I saw a few posts that were similar but none of them fixed the issue, I followed this and checked to make sure the servers php.ini files had SAFE_MODE = off. Then added
'-f $replyemail'
to the end of the mail function above like that link advised but it didn't change anything... I saw somewhere else that it explained you may have to add the email address to the file /etc/mail/trusted-users but the issue is that email is dynamic. Since that email name is whatever the user input as their email. Then the email is sent to the site admin to review, and I want the email to say it is from the users email that filled out the form.
I know the variables are set correctly because they are being printed out correctly inside the message of the email. So if anyone has any idea why this is happening or how I could go about fixing it I would appreciate any insight.
The project is built in wordpress but I just dropped the files onto the server through SSH, not sure if that has anything to do with it.
Thanks,
-Alan
There are a number of issues that can cause this, and I encountered this issue a while ago when I built my site. I solved it by first creating a function to handle the mailout for me:
function mailouthtml($to, $title, $body, $from){
if(!isset($from)){
$from = 'Default Sender<address#example.com>';
}
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "From: $from\r\n";
$header .= "Organization: Example.com, Inc.\r\n";
$header .= "Content-Type: text/html\r\n";
mail($to,$title,$body,$header,"-f $from");
}
Let me break this down a bit. The function calls for a To address, the email title, the email body, and the From address. The From address is used to add information to the email header. The header information of your email is what tells your mail server what to do with your email, and it needs a bit of info in order to handle the message properly.
The mail() function in PHP is formatted as follows:
mail($to,$title,$body,$headers,$additional_parameters);
The mailouthtml() function I've created here constructs the header manually, and adds the additional parameter "-f $from" to set the "From" field explicitly. The $from variable is optional in the function call; if it isn't present, it sets a default. You can find more information on the mail function Here.
Note the following:
Most MTA's require a Reply-To, Return-Path and From field, or it may be flagged as spam.
Content-Type is required tell what type of content the mail server is delivering, typically text/html or text/plain.
I have an if(){} statement that sets a default address if none is specified. You can omit 'Default Sender' if you would rather not specify a name. You can also change that to whatever you'd like.
Organization is optional.
I would also recommend you have SPF enabled on your domain name, and DKIM signing on your messages. These are both implemented via DNS entries for your domain name. If you don't have access to that, no big deal; they're meant to control spam.
Also, if you have a dedicated server and your own IP address, make sure you have a Reverse DNS record set up. This will also help foreign MTA's identify your message as authentic.
don't make this to yourself. Use some mature library like phpmailer or swiftmailer, they will help you to avoid these kind of troubles...I know there are more includes, etc, but there is not point to give fight to this. If you still want to do this, try setting the header Returh-path

Email newsletter from php mail function not sending anymore

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.

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