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.
-
12Is your email spam? – James C May 09 '11 at 09:43
-
1no..but email went to spam folder – Partyboy May 09 '11 at 09:45
-
Sent mail domain should be as the same as your $from parameter, if not the mail wouldn't delivered at all. Configure $header with details, MIME type, charset, etc. Then mail() your email. – Hassan Fadaie Ghotbie May 11 '22 at 16:45
6 Answers
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!";
}
?>

- 14,632
- 7
- 51
- 70
-
19What 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
-
-
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
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)

- 31,254
- 3
- 43
- 68
<?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);
?>

- 1
- 1

- 85
- 1
- 3
-
2remove . from first $header `$headers = "Reply-To: The Sender
\r\n";` – Pooya Estakhri Aug 15 '20 at 12:59
Try PHP Mailer library.
Or Send mail through SMTP filter it before sending it.
Also Try to give all details like FROM
, return-path
.

- 26,156
- 14
- 79
- 87
$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:
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
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

- 4,945
- 4
- 36
- 40
-
1
-
If you are using gmail in 2023+ you need to see this video, as many current tutorials don't work. https://www.youtube.com/watch?v=sCsMfLf1MTg – vr_driver Jan 30 '23 at 00:53
<?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" ;
?>

- 814
- 6
- 22