Every time I run the PHP to send an e-mail it gets sent to spam, with the following message from google:
Why is this message in Spam? It is in violation of Google's recommended email sender guidelines.
Unlike the suggested duplicate solution "sending email via php mail function goes to spam". I am not trying to send an e-mail from a different e-mail than the server I am working with (i.e. send e-mail using @gmail.com
address from an server with example.com
.)
I wrote the php mail in two ways, and both provide me the same response.
First:
<?php
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$message = "Hi,\n\nHow are you?";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <$from>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
Second:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo ("<p>" . $mail->getMessage() . "</p>");
}
else
{
echo ("<p>Message successfully sent!</p>");
}
The end result was the same for both methods, email was sent to spam box.
Googles rules say:
- Use a consistent IP address to send bulk mail.
- Keep valid reverse DNS records for the IP address(es) from which you send mail, pointing to your domain.
- Use the same address in the 'From:' header on every bulk mail you send.
To address all these questions: As you can see my From is always the same on every header. I also don't send bulk mail. My IP address has not changed as it has been the same for about 2 years. My DNS is valid as it is the same for my website, that I am running.
I have done research and looked at post "Everytime my mail goes to spam in phpmailer " which says to use SMTP authentication. As you can see I am with my second example.
I also am sending the e-mail using my own server. For this example my server would be example.com. and my php script is sending from an email with @example.com
on the same server hosting example.com
.
Update/Answer:
I found the answer after hours of doing research on how to fix the problem that commenters hinted at. (side note: just finding out that the DNS record took for ever because tutorial don't say edit DNS record) Answer to this question is as follows.
First if you are in charge of the DNS you have to fix the SPF and DKIM records on your DNS provider. That is where you are looking to make this fix. My code was all correct. Both should have worked perfectly.
First test your domain by sending an e-mail to this website: http://www.mail-tester.com
If it shows you have SPF and DKIM problems continue with this solution.
In your DNS records you need to add the following lines.
To fix SPF you have to add to the DNS records: TXT <yourDomain.com> v=spf1 a mx ip4:<yourIP> ~all
Where:
- TYPE = TXT
- NAME = <yourDomain.com>
- VALUE = v=spf1 a mx ip4:<yourIP> ~all
Now send another e-mail to http://www.mail-tester.com. You should have improved your score by 2 points.
To fix DKIM you have to add two entries to the DNS records: 1) TXT _domainkey t=y; o=~;
2) TXT mail._domainkey k=rsa; p=<PUBLIC KEY>
First one:
- TYPE = TXT
- NAME = _domainkey
- VALUE = t=y; o=~;
Second one:
- TYPE = TXT
- NAME = mail._domainkey
- VALUE = k=rsa; p=<PUBLIC KEY>
The public key depending on encryption should be 216 characters long.
You can check to make sure the DKIM key works correctly by going to http://dkimcore.org/tools/ and typing in your domain information. Selector = mail
and Domain = YourDomain.com
Final check send another e-mail to http://www.mail-tester.com and everything should be working.
Remember you cannot send an e-mail with php from your server as follows: If you send an email from your server example.com, but you put in the from section of the header myemail@gmail.com. The e-mail will most likely be considered spam.