Peter
Peter

Reputation: 1083

Prevent sent emails treated as junk mails using php mail function

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: [email protected]' . "\r\n";

// Email Variables
$toUser  = "[email protected]"; // 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?

Upvotes: 14

Views: 81380

Answers (14)

Jismon Thomas
Jismon Thomas

Reputation: 893

                   **This Works Perfectly fine for me**     
                        $to="[email protected]";
                        $subject="This is Your Message";
                        $from = 'Sender <[email protected]>';
                        $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);

Upvotes: 4

Gerhard
Gerhard

Reputation: 196

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!

Upvotes: 3

Sashi
Sashi

Reputation: 19

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.

Upvotes: 1

Masti Khan
Masti Khan

Reputation: 21

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.

Upvotes: 2

Anju
Anju

Reputation: 171

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");

Upvotes: 15

mike
mike

Reputation: 87

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..

Upvotes: 0

Ross
Ross

Reputation: 521

if your website domain is mydomain.com then in From headers make sure to use [email protected]

Upvotes: 1

Technorat
Technorat

Reputation:

  1. On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
  2. Make sure you send your e-mails from an existing account on your server/domain.
  3. Make sure you have the reply-to address in your header.

These are the basic things you can try.

Upvotes: 1

StackKrish
StackKrish

Reputation: 722

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.

Upvotes: 5

vartec
vartec

Reputation: 134581

You've got two solutions:

Upvotes: 0

Strae
Strae

Reputation: 19445

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)

Upvotes: 1

stefs
stefs

Reputation: 18549

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?

Upvotes: 0

markus
markus

Reputation: 40675

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

Upvotes: 7

Rad
Rad

Reputation: 8381

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.

Upvotes: 8

Related Questions