3

I have the following php script which loads a html template from a .txt file on my server and uses str_replace to populate the template with the Email content before sending the email.

At first I tried hardcoding my Webmail address on my server into the 'to' field of my mailer_send function which worked fine. The Email template displayed perfectly. However, when I then tried sending it to a Gmail account, it was marked as junk and the HTML was just displayed as plain text. Does anyone know whether there are any additional headers I need to include within the Email to ensure that Gmail treats it correctly?

Ideally I would like to send an Email to myself via webmail on my server and then send an automated response to the user thanking them for their enquiry but this will not be possible if the message is always treated as spam.

<?php
    function mailer_send($mailer_recipient, $mailer_subject, $mailer_message){
         $mailer_headers = 'From: webmaster@example' . '\r\n' .
        'X-Mailer: PHP/' . phpversion() . '\r\n' .
        'MIME-Version: 1.0\r\n' . '\r\n' . 
        'Content-Type: text/html; charset=ISO-8859-1\r\n';
         mail($mailer_recipient, $mailer_subject, $mailer_message, $mailer_headers);
     }

$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$message = strip_tags($_POST['inputMessage']);
$template = file_get_contents('email_templates/template.txt');
$template2 = $template;
$template = str_replace('[email_content]',$message, $template);

    mailer_send('test@gmail.com','Test Email',$template);

$message2 = '<h2>Thanks..</h2><br/><p>Dear' . $name . '</p><br/><p>Thanks for your enquiry. Jason will get back to you with a quote as soon as possible.</p>';
$template2 = str_replace('[email_content]', $message2, $template2);
mailer_send($email,'Thankyou for your Enquiry',$template2);
  ?>

Thanks in advance.

Additional:

When the Email is received in Gmail it appears to be ignoring the From header and just saying that the Email is from my web hosting providers servers.

jezzipin
  • 4,110
  • 14
  • 50
  • 94

1 Answers1

3

Theres a million reasons why gmail would suspect the email is spam.

The first thing I would do is use a library that deals with headers correctly -> http://swiftmailer.org/

Secondly, ensure your sender host is coming from the same IP as the server and have correct MX records.

John Cartwright
  • 5,109
  • 22
  • 25
  • Thanks for the suggestion although I would rather not use an additional library which will bloat my application further. – jezzipin Jul 19 '13 at 14:38
  • There are core libraries which should be used in any application. When email is involved, swiftmailer is pretty standard in the PHP community. – John Cartwright Jul 19 '13 at 14:41
  • @JohnCartwright - an alternative to your second point is to set up SPF: http://en.wikipedia.org/wiki/Sender_Policy_Framework – andrewsi Jul 19 '13 at 15:17
  • @JohnCartwright So if I use this library, does it handle all of the header processing for me? – jezzipin Jul 19 '13 at 15:39
  • 1
    Thanks John. I've been out of the game a bit on PHP since I left Uni and hadn't really heard of swiftmailer. Thanks for this though. I've now successfully sent my html email. – jezzipin Jul 19 '13 at 16:04
  • It will handle everything for you, and even introduces some nice features such as throttling. – John Cartwright Jul 22 '13 at 16:04