PHP Mail not sending to gmail


I am confused as to why I cannot send mail to gmail. I am on a TLD and can send to all other providers. Obviously, there is something wrong with my code for gmail but as I say, I can send to other email addresses. I have only posted the relevant piece of code and all variables are assigned. would be grateful if someone could point out my error or how to troubleshoot this. Many thanks.

$to = "";
  $subject = "Account Successfully Unlocked";
  $header = "From:\n";
  $header .= "MIME-Version: 1.0\n";
  $header .= "Content-Type: text/html; charset=ISO-8859-1\n";
  $body = "<html><body>";
  $body .= "<div>" . "<img src=\"\" alt=\"Logo\">" . "</div>";
  $body .= "<div style=\"font-size: 14px; font-family: Verdana, Geneva, sans-serif; background-image: url(images/mailBack.png);\">";
  $body .= "<div style=\"font-size: 14px; padding: 40px; color: black;\">";
  $body .= "<br /><br />";
  $body .= "Dear " . $name;
  $body .= "<br /><br />";
  $body .= "Your account has successfully been unlocked and is now available to use.";
  $body .= "<br /><br />";
  $body .= "This request was actioned because of several unsuccessfull attempts to login to this account and as a security measure, was locked to prevent fraudulent use on this account. " . "<br /><br />" . "This was actioned on: " . '<span style="color: #0080FF; font-weight: normal; font-size: 15px;">' .$date. "</span>";
  $body .= "<br /><br />";
  $body .= "This request was generated from IP address: " . '<span style="color: #0080FF; font-weight: normal; font-size: 15px;">' .$ip. "</span>";
  $body .= "<br /><br />";
  $body .= "Please do not reply to this automated message. Thank you for using us.";
  $body .= "</div>";
  $body .= "</div>";
  $body .= "</html></body>";


Have you return from receiver?


Do you mean is the message bouncing back? then no


If you say, mails to another domains are successfull, then only idea I have: gmail spam filter stops your message due image (…).


I have removed the image but still the same. Thanks


The general advice on here is to not use this function to send mail from PHP, instead use something like PHPMailer or SwiftMailer, or maybe there are others now.

1 Like
$body .= "</html></body>";

They should be reversed.


Have reversed but still the same. Funny thing is that it works ok on wamp localhost. Many thanks for your help


What happens to it? Does it go into the Gmail spam trap, or just not arrive at all? What SMTP server do you use to send the email, and is it correctly configured to send email from the domain you are using in the “from” address? I wonder if Gmail is looking at something SPF that other emails might not be.


It dosen’t arrive at all
Thanks for reply. What is SPF?
Mail server is exim and as far as I can see it is configured correctly.


Sender Policy Framework. Domains can have an SPF record set that defines the IP address(es) of servers that are authorised to send email for that domain, and some receiving mail servers check to see that the mail is coming from that IP. Attempt to reduce spam, basically, but not taken up by everyone.

If it’s your own mail server, can you have a look in the logs to see the conversation between your exim server and the gmail server, to see why it’s getting upset? Or is it doing all that correctly, but still not arriving in the mailbox?


It is on 1and1 server. It is a dedicated server but has several domains on it. I shall check the logs and also look into SPF. Would it help if I posted mx records etc. Thanks


Oh, I thought when you said it was on exim that you were running the mail server yourself. If it’s on a commercial host, it’s possibly fair to say the configuration is OK. If it’s their mail server, you might not be able to see the logs, but you might be able to ask their tech support.


thanks droopsnoot for help.


Don’t ignore droopsnoot’s suggestion to not use the PHP mail function.


In my opinion, it’s not your code; it’s gmail. I use ASP not PHP and have the same issues. I think it’s the gmail filters that are difficult to get through.