First off, this isnt a coding problem but a general question. I have written a PHP routine that sends email from a form. It works, because I got the test mail message straight away, and correctly formatted, on my non-gmail account, but it did not get through to my gmail account. Has anyone else had this problem and if so, was it solved? Is it maybe a problem with PHP mail?
The two most common reasons for this are - 1) using a domain in the From: mail address that doesn’t actually correspond to the sending mail server at your web hosting, or 2) missing or misconfigured DNS records at your web hosting such that the receiving mail server at gmail cannot confirm that the sending mail server at your web hosting is authorized to send emails for the domain in the From: mail address.
The reason this may work for your non-gmail account is that the mail server for that address is either hosted at the sending mail server at your web hosting, and you are actually sending the email to yourself (the sending and receiving mail server is the same) or the receiving mail server is less strict about checking for missing or misconfigured DNS records.
Thanks. In regard to point (1), the From: in the header does check out.
Point (2) looks like the reason. It seems I need the hosting company to configure something called an SPF at the domain with the tag v=spf1 whose key is at least 1024b long. I have contacted them and they are pursuing it, so I hope this information may be of help to anyone else who is having the same issue with gmail - there appear to be quite a few!
OK, I have looked at the domain details and all the required acronyms are present and (I assume) correct - SPF, DKIM and another one whose acronym escapes me at the moment. From looking at similar issues experienced by others it looks as though the php mail() function is the source of the problem. Not the coding, which is OK, but the actual function itself.
Ah well… c’est la vie.
The phpmailer class, which generally has good success at getting an email to be accepted, uses php’s mail() function internally by default. The problem is in the usage, not in the function itself. If you have made a large number of attempts to send to a gmail address, your sending mail server may have been blacklisted by gmail. You can contact a gmail ‘postmaster’ to attempt to find out why emails you are sending are not being accepted.
One specific problem, which you seem to have stated wasn’t the case, is using the email address being entered in the form, as the From: mail header. This is certainly incorrect when trying to send to a gmail address, since your sending mail server doesn’t correspond to a gmail mail server. The email address being entered in the form, can go into the Reply-to: mail header, after validating that it is exactly and only one properly formatted email address. What is your actual code leading up to the mail() call?
Hi PHDR and thanks (again!)
I have only used my gmail address to test. Certainly not many attempts, probably less than 10 overall. I’d love to know how to contact a gmail ‘postmaster’! I didnt even know there was such a thing. So here is what I have at the moment:
$thehead = "From: LYRA Admin<[email protected]>";
...
//previous condition block to email all members (not needed for this example)
...
{$sql = "select emailadd from members where firstname = 'michael'";
$result = mysqli_query($conn,$sql);
while ($row = $result->fetch_assoc())
{
$theiradd = $row['emailadd'];
mail($theiradd,$thesubj,$themsg,$thehead);
}
}
I am the only ‘Michael’ in the DB so I entered both my email addresses into it. My non-gmail one works perfectly (so the code in itself works). Incidentally, the total membership of the group I am doing this for is quite small, around 20. I know the above code will iterate through the membership database so may take a little time to complete. This test using my name will be replaced ultimately by the members who are ‘admin’; there’s only 3 or 4 of them so that should take even less time!
I’m assuming you have checked the junk/spam folder at your gmail address?
You need to have a logic test in your code for the value returned by the mail() call, for error handling. If it is a false value for the gmail address, there would be an error message that would help find the cause of the problem.
I recommend that you switch to the phpmailer class. It sets up other mail headers that may help get the email to be sent/received.
Cheers for that.
Yes, I checked all possibilities - spam, junk, ‘all mail’. I know the mail() is working because it sends to my non-gmail address. The gmail address is also correct. One thing I have just thought about (in the bath!!) is to do with authentication. Currently the mail is coming from an address I set up for the client on their server. Should I change the ‘from’ address to the ‘native’ email that was created when the domain was initialised? This address includes the actual server name (it’s ganesh - their servers appear to be named after hindu gods)
The gmail postmaster you recommended was very helpful, and replied almost instantly which, coming from a mammoth company like google, is pretty impressive.
This is from the 1st reply in this thread -
Does the domain in the From: mail address - lyraastro.co.uk correspond to the sending mail server at the web hosting or not? If it does not, you must either setup DNS records for that domain that indicates that sending mail server is authorized to send email for that domain or you must use a From: mail address that corresponds to the sending mail server.
If these conditions are not met, gmail is going to discard the email because it says it is From a domain that isn’t traceable back to the sending mail server that is trying to get gmail to accept the email.
Cheers again. I am a bit all at sea here (as you can probably guess!) - the actual server is ganesh.aihosting.co.uk and the ‘native’ email is lyraastr@that address. By ‘native’ I mean what was there by default when the domain was bought, i.e., not an address I personally created on that domain, which is [email protected]. I’m just not sure which one of these would be causing the problem at Google’s end. The hosting company have confirmed that mail was sent to my gmail address - it just wasn’t received!