Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() function will not send to gmail but will send to my non-gmail account

Tags:

php

email

For some reason the php mail() function is not working properly on a site I am building. I tried to troubleshoot the issue down to its simplest form, and came up with this file:

<?php
mail('[email protected]', 'the subject', 'the message', 'From: [email protected]', '[email protected]');
?>

when myEmail is a Gmail account, I never receive the message. However when I use a non-gmail account, I do receive the message. I am at a loss and have tried everything to figure this out. I am starting to think it is an obscure host/server issue. You can see the server specs here: http://aopmfg.com/php.php

Any ideas?

EDIT - let me also add that this was all working fine a few weeks ago, the last time I tested it. No significant code changes since then at all.

EDIT 2 - After reading a similar post I tried adding From and Reply-To headers... still no luck. New code:

<?
$headers = 'From: <[email protected]>' . "\r\n" .
'Reply-To: <[email protected]>';

mail('<[email protected]>', 'the subject', 'the message', $headers,
'[email protected]');
?>
like image 857
ARH3 Avatar asked Apr 05 '12 01:04

ARH3


People also ask

What to do if mail is not sending in Gmail?

Make sure you're connected to Wi-Fi or mobile data. If you're on a slow connection, it could take a while for your email to send. If you sent the email from an email client, like Apple Mail or Outlook, try sending it from mail.google.com or the Gmail app instead.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

Why is my PHP email not sending?

It could be, for example, because you're missing necessary parameters, have typos in your recipient's email address, or need to set up an SMTP relay. Out of all the possible options as to why your PHP mail() isn't sending email, we found that most issues stem from Postfix being configured incorrectly.


2 Answers

It turns out that Google blocked my server because another site on the same server was hacked and used for spam.

To test and ensure that it was a problem with the server, I created a simple PHP file that would send an email to my email address on page refresh. It worked when I sent to my exchange-based email address, but not to any Google-related accounts.

Code:

$headers = 'From: <[email protected]>' . "\r\n" .
'Reply-To: <[email protected]>';

mail('<[email protected]>', 'the subject', 'the message', $headers,
  '[email protected]');
?>

Thanks for the help all.

like image 52
ARH3 Avatar answered Oct 11 '22 08:10

ARH3


Try putting <> around the From and Reply to addresses. I had that same problem with work emails.

like image 22
Cassie Smith Avatar answered Oct 11 '22 08:10

Cassie Smith