ARH3
ARH3

Reputation: 213

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

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]');
?>

Upvotes: 8

Views: 50003

Answers (9)

mevsme
mevsme

Reputation: 529

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]>

Upvotes: 0

locopump
locopump

Reputation: 559

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

Upvotes: 0

hamode massarwi
hamode massarwi

Reputation: 1

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.

Upvotes: -3

Graham
Graham

Reputation: 11

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);

Upvotes: 1

Iwan Ross
Iwan Ross

Reputation: 354

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

Upvotes: 0

Dharam Jindal
Dharam Jindal

Reputation: 41

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

Upvotes: 0

Wim
Wim

Reputation: 21

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.

Upvotes: 2

ARH3
ARH3

Reputation: 213

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.

Upvotes: 13

Cassie Smith
Cassie Smith

Reputation: 501

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

Upvotes: 5

Related Questions