Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 248
Peter Avatar asked Apr 14 '09 08:04

Peter


People also ask

How do I prevent mails sent through PHP mail () from going to spam?

Try PHP Mailer library. Or Send mail through SMTP filter it before sending it. Also Try to give all details like FROM , return-path .

How do I stop emails being marked as junk?

From the top toolbar, select Not junk > Not junk (or Not spam > Not spam). You can also open the message and select the It's not junk link at the top.

Why emails are going to spam in PHP?

If php mail is going to spam, it may be necessary to update the spam filter of the receiver, such as whitelisting the sender address. Third-party search engines such as https://mxtoolbox.com/blacklists.aspx can be used to check if the domain is listed on Real Time Blacklists (RBL).

Is PHP mail secure?

This is perfectly secure; there is no way a hacker could manipulate who the E-mail gets sent to; PHP is server-side code. Thus, $email_to = "[email protected]"; cannot get manipulated from the form itself, as it is hard-coded into your PHP.


2 Answers

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");
like image 67
Anju Avatar answered Oct 08 '22 09:10

Anju


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.

like image 35
Sashi Avatar answered Oct 08 '22 11:10

Sashi