8

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]');
?>
4
  • 4
    BREAKING NEWS: have you checked your spam label under your gmail? Apr 5, 2012 at 1:54
  • 1
    yes I checked my spam :)
    – ARH3
    Apr 5, 2012 at 2:08
  • Use a Mail Class like SwiftMailer. That helps getting away from this weird mail problems.
    – binarious
    Apr 5, 2012 at 2:15
  • this problem actually stemmed from a WordPress project I am doing. I am using the plugin ContactForm7, which is a very reputable plugin and one that I use all the time. I tested it on this site when I set it up a few weeks ago and it worked fine. I wanted to see if the issue was related to WordPress, ContactForm7, or something outside of that so I made the code shown in the question and discovered that that does not work either.
    – ARH3
    Apr 5, 2012 at 2:22

9 Answers 9

13

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.

1
  • Thanks. Worked like a charm.
    – emuigai
    Apr 11, 2016 at 21:22
5

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

1
  • Do you know why this behavior occurs, and which link in the chain is responsible? (php, sendmail, gmail, etc.) Jun 16, 2021 at 12:25
1

I had a similar problem with gmail. However my subject title was "See if you have won". When I changed this to something less marketing/spammy, it came through. So it's not always the PHP code who is causing this, but it can be the subject title as well which is blacklisted.

0

I was having the same problem. But when I checked my C:\xampp\mailoutput folder, the mail sent was received in this folder. Now please check and do the needful. It was sent in my testing local server. If it is sent on the real server, that may be on your hosting server and you have to check through site hoster

0

I have found that adding SPF and DKIM records to DNS solved the problem. I can use the phpmail() function to send to a list of Gmail subscribers.

  1. The domain in the email used in the -f option in the php.ini sendmail parameter or in the mail() extra parameters field, needs to have a valid SPF record for the domain.
  2. You should also use a domain key or DKIM. The trick here is that the domain key/DKIM is case sensitive!

After updating the records the mails get delivered to Gmail, Yahoo and AOL addresses.

Read the source here. Comments by ABOMB

https://www.php.net/manual/en/function.mail.php#107321

0

I had the same problem. I was using a BCC email address to record the messages sent. This is an advantage as if gmail or hotmail blocked the mail then the BCC was also blocked. I created an SPF record and that did not help on its own though is probably a good idea. What did seem to help me was putting the email addresses in <> and adding a reply to entry in the header.

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

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

The problem is the BCC option, remove the BCC or -f email, and that's all.

0

In my case the problem was that I didn't use From as email form but just a simple string. So I changed From: site.com to From: <[email protected]>

-2

gmail canceled the less secure app option . so now you need to generate a powerfull gmail password that gives who use this password to enter your account without any problem . follow my steps . 1.First go to your google account management and go to security. 2.Make sure your 2-step verification are enabled To your phone or whatever. 3.Then go to search in the gmail manager then search for : app passwords. 4. Select other in the select app dropdown menu, and named whatever you like. 5 then click generate, google will give you a password. make sure you copy it and save it somewhere else. instead using your real google account password in PHPMailer or laragon etc.. setting, use the password you just generate.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – jasie
    Aug 17, 2022 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.