45

I am using PHP's mail() function to send emails (sendmail process is running). But all the mails are going to spam (in case of gmail). I have tried many tricks that I found on the net but none is working, please tell me about any sure-shot trick.

alimack
  • 611
  • 2
  • 9
  • 20
Partyboy
  • 478
  • 1
  • 4
  • 7

6 Answers6

46

You must to add a needle headers:

Sample code :

$headers = "From: myplace@example.com\r\n";
$headers .= "Reply-To: myplace2@example.com\r\n";
$headers .= "Return-Path: myplace@example.com\r\n";
$headers .= "CC: sombodyelse@example.com\r\n";
$headers .= "BCC: hidden@example.com\r\n";

if ( mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
   } else {
   echo "The email has failed!";
   }
?> 
Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • 19
    What is a "needle header"? Do you mean the return-path header or the reply-to header - or both? – Roland Seuhs Mar 16 '16 at 13:07
  • How we could add these headers in wordpress? – K Ravi Aug 01 '17 at 06:27
  • We have a wordpress website and our inquiry mails are sending to spam folders. could you suggest what possibly we could do? – K Ravi Aug 01 '17 at 06:28
  • 1
    [This article](https://www.postmastery.com/blog/about-the-return-path-header/) says that the return path should not be set by the sender. It also says that *"As of PHPMailer version 5.2.8 the Return-Path header is no longer set in created messages."* – Accountant م Feb 13 '18 at 12:05
24

There is no sure shot trick. You need to explore the reasons why your mails are classified as spam. SpamAssassin hase a page describing Some Tips for Legitimate Senders to Avoid False Positives. See also Coding Horror: So You'd Like to Send Some Email (Through Code)

Oswald
  • 31,254
  • 3
  • 43
  • 68
9
<?php

$subject = "this is a subject";
$message = "testing a message";
  


  // declare variable
  $headers = "Reply-To: The Sender <sender@domain.com>\r\n";

  // add more info
  $headers .= "Return-Path: The Sender <sender@domain.com>\r\n"; 
  $headers .= "From: The Sender <sender@domain.com>\r\n";  
  $headers .= "Organization: Sender Organization\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  $headers .= "X-Priority: 3\r\n";
  $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;

  
  
mail("reciever@domain.com", $subject, $message, $headers); 


?> 
Community
  • 1
  • 1
Ahmed Medhat
  • 85
  • 1
  • 3
6

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

Rikesh
  • 26,156
  • 14
  • 79
  • 87
1
$fromMail = 'set your from mail';
$boundary = str_replace(" ", "", date('l jS \of F Y h i s A'));
$subjectMail = "New design submitted by " . $userDisplayName;


$contentHtml = '<div>Dear Admin<br /><br />The following design is submitted by '. $userName .'.<br /><br /><a href="'.$sdLink.'"><b>Click here</b></a> to check the design.</div>';
$contentHtml .= '<div><a href="'.$imageUrl.'"><img src="'.$imageUrl.'" width="250" height="95" border="0" alt="my picture"></a></div>';
$contentHtml .= '<div>Name : '.$name.'<br />Description : '. $description .'</div>';

$headersMail = '';
$headersMail .= 'From: ' . $fromMail . "\r\n" . 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'Return-Path: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= "Content-Type: multipart/alternative; boundary = \"" . $boundary . "\"\r\n\r\n";
$headersMail .= '--' . $boundary . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headersMail .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$headersMail .= rtrim(chunk_split(base64_encode($contentHtml)));

try {
    if (mail($toMail, $subjectMail, "", $headersMail)) {
        $status = 'success';
        $msg = 'Mail sent successfully.';
    } else {
        $status = 'failed';
        $msg = 'Unable to send mail.';
    }
} catch(Exception $e) {
    $msg = $e->getMessage();
}

This works fine for me.It includes mail with image and a link and works for all sorts of mail ids. The clue is to use all the header perfectly.

If you are testing it from localhost, then set the below before checking:

How to set mail send from localhost xampp:

  1. comment everything in D:/xampp/sendmail/sendmail.ini and mention the below under

    [sendmail]

    smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=yourmailid@domain.com auth_password=your-mail-password force_sender=yourmailid@domain.com

  2. In D:/xampp/php/php.ini a. Under

    [mail function]

    SMTP = smtp.gmail.com smtp_port = 587

b. set sendmail_from = yourmailid@domain.com c. uncomment sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t" Hence it should be look like below

sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"

d. comment sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe" Hence it should be look like below

;sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"

e. mail.add_x_header=Off

Ipsita Rout
  • 4,945
  • 4
  • 36
  • 40
1
<?php 
$to1 = 'test@gmail.com';
$subject = 'Tester subject'; 

    // To send HTML mail, the Content-type header must be set

    $headers .= "Reply-To: The Sender <sender@sender.com>\r\n"; 
    $headers .= "Return-Path: The Sender <sender@sender.com>\r\n"; 
    $headers .= "From: sender@sender.com" ."\r\n" .
    $headers .= "Organization: Sender Organization\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
?>
SAVe
  • 814
  • 6
  • 22