0

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Agusfn
  • 83
  • 2
  • 12
  • Where is your site hosted? Perhaps their IP range is on some blacklist (i.e. spammers used to host their servers there). Try sending emails from a whitelisted domain, or use a service like MailChimp or SendGrid. – Traveling Tech Guy May 26 '13 at 19:00

2 Answers2

4

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.

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
  • I use this hosting: http://www.neolo.com.ar/ ---- Okay, and, i have some @steambuy.com.ar (my domain) mails with google apps. Can i use a mail account with username and password to send the mails through google mail? – Agusfn May 26 '13 at 19:18
  • 1
    If you use the standard "web hosting", then this answer is spot on – Graham Walters May 26 '13 at 19:20
  • I've edited my answer, is there any way i can do what i said? – Agusfn May 26 '13 at 19:21
  • @Agusfn look into [Swiftmailer](http://swiftmailer.org/). It has a lot of great features, including the option to send emails from remote smtp servers such as the google apps mail servers. – Graham Walters May 26 '13 at 19:23
  • Also, you may find this question http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page useful in search for a solution @Agusfn – Morgan Wilde May 26 '13 at 19:31
1

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

?>
Graham Walters
  • 2,054
  • 2
  • 19
  • 30