I have a website which send a single mail for those who register, definitely not spam. And the thing is that I use mail() function in PHP but lots of people receive it as spam.
$title = "title";
$body = "message";
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "To: ".$_POST["name"]." <".$_POST["email"].">" . "\r\n";
$header .= "From: SteamBuy <contacto@steambuy.com.ar>" . "\r\n";
mail($_POST["email"], $title, $body, $header, "-f contacto@steambuy.com.ar");
So I want to know what am I doing wrong, and how can I fix it. I don't want my mails to appear as spam as some of they may contain valuable information.
The important part is not mail()
per se, but the host you're hosting your site on. Because your email contains all relevant information of your host - IP, and so on.
Since most shared hosts, I assume you're using one, have a ton of users hosted on a single server, and most/some may want to use mail()
, email providers may blacklist the IP of the host. Which means your site is included in that blacklist.
There is no way around this issue when using a shared host.
As @MorganWilde mentioned, the number one reason emails are marked as spam, is the host is as black listed. Normally this is because you are on a shared server, and other users may have abused the service in the past.
If you want to use your google apps smtp server to send the email, this is a great way to get around being marked as spam. The only thing is to make sure google apps is setup correctly, and the sent from email is the same as the email you're trying to send from. The easiest way to use the google apps smtp servers is to use a php mail library as the mail()
function is very basic. Here is some sample code to get you started using the Swiftmailer library
<?php
require_once "/Swift-Email/lib/swift_required.php"; // Make sure this is the correct path
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com')
->setPort(465)
->setEncryption('ssl')
->setUsername('EMAIL')
->setPassword('PASSWORD');
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(50, Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE));//Add a Throttler Plugin if need be.
$message = Swift_Message::newInstance($emailSubject); // Subject here
$message->setFrom(array('contacto@steambuy.com.ar' => 'Contact'));
// You can choose to only send one version, just replace the second parameter in the setBody() function to the type
$message->setBody(HTML_VERSION, 'text/html'); // Body here
$message->addPart(PLAIN_TEXT_VERSION, 'text/plain'); // and here
$message->setTo($_POST["email"]);
if ($mailer->send($message))
echo 'Sent';
?>