Problem in PHP mail

I am using php mail function in my website. But when shoot the mail the mail goes to SPAM box. :cry:

any suggestion? The code showing below

$q = "SELECT seller_emailid FROM {$table_prefix}paypalid WHERE ID=1";
$admin_email = $wpdb->get_var($q);
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );


 if (isset($_POST['submit'])) {
  $fullname=$_POST['fullname'];  //die();


$from = "$sender"; 
$from = "$sender";
$from1 = "$to";
$subject = "Contact -";
$subject1 = "Contact -"; 

$message = "<html>
<p>Contact User Details</p>
<tr><th style='text-align:left;'>Name</th><th>:</th><td style='text-align:left;'> ".$fullname."</td></tr>
			<tr><th style='text-align:left;'>Email ID</th><th>:</th><td style='text-align:left;'> ".$Email."</td></tr>
			<tr><th style='text-align:left;'>Service</th><th>:</th><td style='text-align:left;'> ".$service."</td></tr>
			<tr><th style='text-align:left;'>Subject</th><th>:</th><td style='text-align:left;'> ".$Subject."</td></tr>
			<tr><th style='text-align:left;'>Website URL</th><th>:</th><td style='text-align:left;'> ".$websiteurl."</td></tr>
			<tr><th style='text-align:left;'>Phone Number</th><th>:</th><td style='text-align:left;'> ".$ph_no."</td></tr>
			<tr><th style='text-align:left;'>Country</th><th>:</th><td style='text-align:left;'> ".$country."</td></tr>
			<tr><th style='text-align:left;'>Message</th><th>:</th><td style='text-align:left;'> ".$message."</td></tr>
This e-mail was sent from a contact form on Local SEO Experts <br/>(

$message1 = "<html>
<p>Dear ".$name.",</p>  
<tr><td style='text-align:left;'>We welcome this opportunity to assist you and look forward to a continuing relationship which will be profitable to all concerned. <br/>
			One of our representatives will get in touch with you shortly.<br/><br/>--</td></tr><br/>
			<tr><td style='text-align:left;'>Kind Regards,<br/>Support Team<br/>E:<a target='_blank' href=''></a><br/>W:<br/>T: (0091) 9836720921 / 9903897980</td></tr>

$headers = "From: $from";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";

$headers1 = "MIME-Version: 1.0" . "\r\n";
$headers1 .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers1 .= 'From: <'.$from1.'>' . "\r\n";

@mail($to, $subject, $message, $headers); 
@mail($to1, $subject1, $message1, $headers1);

echo "sent";


use a battle tested mailing library, e.g.


There are sometimes issues with using a sender-address that the user filled in on the form - specifically that as your mail server is probably not configured to send mail for that domain, it might refuse to do it, or mark it spam, or the receiver might use various methods to check that the sending-server is the correct one for that domain, and mark it spam if it is not. You should use one of your own addresses as the from-address (“” or something, so you can identify it) and fill in the “reply-to” header with the address from the form.

Might not help, the library suggested by @dormilich might make more difference.

Most client email programs will shoot that type of email of to the spam folder for it contains HTML in it and that even if you use swiftmailer (I like the library) or not. That’s why I like just sending plain old text even if it’s not as snazzy as HTML. However, I still have a message when the email is sent to the user that states “Please check your spam folder for sometime email goes there even though it’s legit” or something like that.

I am not super knowledgeable in PHP. For a number of years I have used the same sendmail script in different projects and it always worked just fine but am just now finding out that some form submissions are not going through. From the little bit of research I’ve done and from looking at this thread (which is most of my research) it does appear that once the user submits the form that there could be in some instances something happening between the user sending and me receiving that keeps it from going through, and it might be caused by servers and whatnot getting more picky about what they will allow through. But I need to get a script working reliably and this existing thread seems close enough to my concern that it probably doesn’t warrant me starting a new thread but I am wondering if anyone can see some little fix I can implement in this script I’ve been using for probably over a decade now:

//This is a very simple PHP script that outputs the name of each bit of information (that corresponds to the <code>name</code> attribute for that field) along with the value that was sent with it right in the browser window, and then sends it all to an email address (once you've added it to the script).

if (empty($_POST)) {
	print "<p>No data was submitted.</p>";
	print "</body></html>";

//Creates function that removes magic escaping, if it's been applied, from values and then removes extra newlines and returns to foil spammers. Thanks Larry Ullman!
function clear_user_input($value) {
	if (get_magic_quotes_gpc()) $value=stripslashes($value);
	$value= str_replace( "\n", '', trim($value));
	$value= str_replace( "\r", '', $value);
	return $value;

if ($_POST['comments'] == 'Please share any comments you have here') $_POST['comments'] = '';	

//Create body of message by cleaning each field and then appending each name and value to it

$body ="Here is the data that was submitted:\n";

foreach ($_POST as $key => $value) {
	$key = clear_user_input($key);
	$value = clear_user_input($value);
	if ($key=='extras') {
	if (is_array($_POST['extras']) ){
		$body .= "$key: ";
		$counter =1;
		foreach ($_POST['extras'] as $value) {
				//Add comma and space until last element
				if (sizeof($_POST['extras']) == $counter) {
					$body .= "$value\n";
				else {
					$body .= "$value, ";
					$counter += 1;
		} else {
		$body .= "$key: $value\n";
	} else {

	$body .= "$key: $value\n";

//removes newlines and returns from $email and $name so they can't smuggle extra email addresses for spammers
$email = clear_user_input($email);
$name = clear_user_input($name);

//Create header that puts email in From box along with name in parentheses and sends bcc to alternate address
$from='From: '. $email . "(" . $name . ")" . "\r\n" . 'Bcc:' . "\r\n";

//Creates intelligible subject line that also shows me where it came from
$subject = 'Contact Submission';

//Sends mail to me, with elements created above
mail ('', $subject, $body, $from);


I looked at that Swiftmailer option and it is probably good, but it also appears like there may be too much of a learning curve with it to get it going at all.

1 Like

What happens if you change it to use your own email address as the from-address, and set the reply-to header to the users email address?

Hmm, lemme check…

It does seem to be of some help. Thanks.

Hi, MauiMan2
This code I use for my site. It’s working good. Thank you for sharing.

1 Like

Good to hear!

Sending email == Simple.

Having 100% deliverability == Gnarly Complex.

Either use MailGun or SendGrid… or strap yourself in for a time sucking adventure.

Here’s what’s required.

  1. You MX records provide reverse DNS lookup correctly.

  2. Your SPF DNS record must be correct.

  3. Each outgoing email message must be correctly DKIM signed.

  4. Your DMARC DNS record must be correct.

  5. You must correctly manage your return loop, so every temporary/permanent message returned to your server from another server must be processed + acted upon correctly.

  6. You must remove your IP from various blacklists, when your competitors flag you as a spammer.

  7. After #1 - #4 are correct, you must warm your IP which means sending some of your email each day out this new IP, so your IP builds reputation. This process can take days to weeks, based on many factors… including if you correctly set your header Precedence: bulk. If you some how skip/miss this, likely Google/Gmail will never accept email from you or email acceptance will be inconsistent/spotty.

So… Best to use MailGun or SendGrid. They take care of all this for you.

If emails landing to spam that means your IPS are cold not warm IP’s + other uses like signature, DKIM etc.

You can read about warm and cold IP’s here →

This may not completely solve your problem, but it does appear you’re using WordPress, which has its own mailer function, wp_mail.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.