Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top