I'm trying to send e-mail using PHP. I took a piece of code that I have been using earlier, but it doesn't work anymore. I figure out the problem was coming from the subject.
When my subject is "test", the mail is sent, but when my subject is anything else with for example capital letters, like "Test", it doesn't work. I'm pretty sure this is a quick fix, but I can't find the answer.
Here is my code.
In index.php
$subject= 'test';
$message = implode("\n" , $_SESSION['data']);
mail_html($subject, $message, $email);
In functions.php
function mail_html ( string $subject, string $message, string $receiver) {
$headers = 'From: Template <me#me.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8';
mail($receiver, $subject, $message, $headers);
}
Thank you everyone for helping me. I feel stupid getting blocked by things like that.
Sending E-Mails is a very difficult process. So you can't send E-Mails so easy from your Computer or Server if you don't have alle the common SPAM prevention mechanisms enabled.
So when you don't a Reverse-DNS entry and you send an Email with "Test" you should not wonder if the emails can't send to the other site. So Take a look at the SPF-System, Reverse-DNS and generic names for servers (not working for some providers). Then you can send some E-Mails.
To find some problems check your mail log /var/log/mail.log.
When you try to prevent that problems use an SMTP account from your provider and send that E-Mails over SMTP.
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 7 years ago.
I have a web site that provides daily real estate updates. Users register, and we send them an email every day. However, Gmail is marking all of our emails as spam. What should we be looking out for?
Spam emails are based on Server, domain and blacklist history.
This is controlled by the Service Provider there is not a lot you can do to be honest.
The best thing is is to add the sender email to your safe list i.e. no-reply#example.com
Due to the simplicity of PHP, it is very easy to send a mail through
mail(), however there is 99% of chances that you are doing it wrong.
You need to follow the right guidelines to use mail(). My recommendation is use a third party mail service like Mandrill
If you still choose to go ahead with php mail(), please follow the below guidelines, which will help you to certain extend.
Set the right Headers:
$headers .= 'From: YourLogoName info#domain.com' . "\r\n" ;
$headers .= 'Reply-To: '. $to . "\r\n" ;
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$to = to#hello.com;
$subject = subject ;
$body = "<div> Email body goes here.. </div>";
mail($to, $subject, $body,$headers);
Message Sender Domain and Server Domain Should Match
Spammers are notorious for sending emails from one server and trying to make the recipient believe that it came from somewhere else. So if you are sending an email from sender#yourdomain.com, it is a good idea the the script reside on example.com.
The Server is not Blacklisted
When a server is blacklisted, it means that that server has identified as one that has been sending a lot of spam. This results in recipient mail servers rejecting or filtering any mail that is received from that server.
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..
I'm launching a product today and COMPLETELY OUT OF NOWHERE hotmail accounts can't receive email from my php mail() script.
I've searched around and found others with similar problems but haven't found a true solution
$Email = "my#email.com";
$Name = "My name";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ". $Name . " <" . $Email . ">\r\n";
$body = "bleh";
mail($recipient, "My email", $body, $headers);
It sends fine to GMail etc just not hotmail.
Anyone conquered a similar problem? It doesn't go into SPAM either
Try to use SMTP instead. Mails from mail() often go to spam folder, because servers can't verify source of message.
Is this the exact code you're using?
A long shot, but could there be a conflict in your e-mail validation (if there is one)? A while ago I had an issue where regex wasn't catching hotmail accounts, took me so long to figure it out.
Else, I agree with other responders that some mail services simply delete what they think is spam, I think there's a "spam level" thing where you set how aggressive the spambot is.
Hope this helps.
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.
If I send an email through GMail to this address nkhkhlkhlkjlkjkljlkjlk#gmail.com
I got error like:
Delivery to the following recipient failed permanently
My question is, if I send using the PHP mail function, how can I catch bounce emails?
My code:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
$to = "nkhkhlkhlkjlkjkljlkjlk#gmail.com";
$subject = "Testing";
$message = "Testing body";
mail($to, $subject, $message, $headers);
You can't detect such errors just by using mail(). You have to specify a valid return address and then connect to that mailbox (via IMAP or POP, there are functions for that) and check if any message bounced. Bear in mind that bounces may take a long time (even hours, due to transient errors) so you have to do this check asynchronously.
you could get the message by mail when you use this:
$bounce = 'test#domian.com';
mail($to, $subject, $message, $headers,"-f $bounce");
This will send all bounce back mails to your address
You could use PEAR MAil, it contains a parseAddressList() method which checks if the server of an email address accepts the email address. It also returns useful error messages.
The PHP mail() function does have an error status as the function return value, but it won't pick up this kind of error because it only checks that the email has been sent, not that it has been received.
(this is because it needs to return control back to the program immediately; it can't wait for a possible bounce message because they can take a long time to come back - you wouldn't want your program to sit and wait for five days just in case the email bounced, would you?)
Therefore, the only way to determine whether a message has bounced is to have a separate program which checks the mailbox which the bounces would be sent to.
You can specify the mailbox for bounces using the -f option in the mail options string (see the PHP mail() function man page for more info on this).
Then have a separate program periodically check that mailbox for bounce messages, and report them to you as required. (there are a number of PHP libraries that allow you to check a mailbox; Google will help here, or look in PEAR)
Determining which email it was will depend on the quality of the bounce message. You should definitely be able to see the intended recipient's email address, but you may not be able to tell which email it was, ie to match it up to a specific instance of when you called the mail() function in the first place.
Hope that helps.
You should login via IMAP or POP3 to your inbox and parse the email address and delivery error.
It's also worth mentioning that you can specify (while sending) custom headers to better track the message source, I believe MailChimp and others do this do track campaigns, etc...
PHP does not know anything about the mail after it got out of the mail function, so no way to do it at the same time as you send the mail.