title: Bypass Gmail's spam filter (mails sent with PHP from a shared host)
body: <p><strong>TL;DR:</strong> Mails sent from shared hosting (such as a cheap domain from Unoeuro or One.com) end up in spam. How to fix?</p>
  
  <hr>
  
  <p>I made a mail-system, that first generated a PDF-file (using <a href="http://www.fpdf.org/" rel="nofollow noreferrer">FPDF</a>), whereafter it sent the PDF-file as an attachment with PHP's Swiftmailer. This email was sent out to 130 people (as a 'one-of' invoice). But it landed in the spam-filter for almost everybody. I tried adjusting SwiftMailers header-settings, but without any luck. Even mails that I haven't sent to before (thoroughly tested). This was my initial setup:</p>
  
  <pre><code>function sendMailEt($toEmail, $toName, $invoiceNumber){
  
  require_once('includes/lib/swift_required.php');
  
  $transport = Swift_SmtpTransport::newInstance('mailout.one.com', 25)
    -&gt;setUsername('EMAIL-ACCOUNT1@THE-DOMAIN.DK')
    -&gt;setPassword('THE-PASSWORD')
    ;    
  
  $mailer = Swift_Mailer::newInstance($transport);
  
  $message = Swift_Message::newInstance('FROM COMPANY')
        -&gt;setSubject('Thanks for signing up - COMPANY')
    -&gt;setFrom(array('EMAIL-ACCOUNT1@THE-DOMAIN.DK' =&gt; 'Company name'))
    -&gt;setTo(array($toEmail =&gt; $toName))
        -&gt;setBody('A brief body, that explains that this is an invoice and that it has to be paid within 5 days. (written in danish)')
        -&gt;addPart('A brief body, that explains that this is an invoice and that it has to be paid within 5 days. (written in danish)', 'text/html')
  
     -&gt;attach(Swift_Attachment::fromPath('/URL-TO-THE-PDF-FILE.pdf'))
    ;
  
  $result = $mailer-&gt;send($message);
  }
  </code></pre>
  
  <p>I also tried sending out the emails with PHP's native <code>mail()</code>-function, and then simply link to the invoice ( <code>http://www.company-domain-name.dk/invoice/base64_encoded-name.pdf</code> )... Same result (spam). </p>
  
  <p>I tried writing the entire header myself. I've read a numerous amount of forums about what headers should include, but they all wrote different things. So I tried a few different things (both emails I had sent to previously and emails I hadn't)... Same result (spam). </p>
  
  <p>Then I tried writing the header exactly as MailChimps does, in their header. That led me to this:</p>
  
  <pre><code> $headers = "Reply-To: Company name &lt;UNUSED-EMAIL-ACCOUNT-FROM-DOMAIN@DOMAIN-NAME.DK&gt;\r\n"; 
   $headers .= "Return-Path: Company name &lt;UNUSED-EMAIL-ACCOUNT-FROM-DOMAIN@DOMAIN-NAME.DK&gt;\r\n"; 
   $headers .= "From: Message from Company name &lt;UNUSED-EMAIL-ACCOUNT-FROM-DOMAIN@DOMAIN-NAME.DK&gt;\r\n"; 
   $headers .= "MIME-Version: 1.0\r\n";
   $headers .= "Sender: Message from Company name &lt;UNUSED-EMAIL-ACCOUNT-FROM-DOMAIN@DOMAIN-NAME.DK&gt;\r\n";
   $headers .= "Content-type: text/plain; charset=\"utf-8\"; \r\n";
   $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
  </code></pre>
  
  <p>And then I send the mail like this:</p>
  
  <pre><code>mail($toName . '&lt;'.$toEmail.'&gt;', utf8_decode('Faktura på depositumet'), utf8_decode($someMessage), $headers);
  </code></pre>
  
  <p>... Same result (spam). </p>
  
  <p>The webspace is with One.com, so I can't use <a href="https://github.com/PHPMailer/PHPMailer" rel="nofollow noreferrer">PHPmailer</a> (since that has to be installed, and that can't be done on one.com's servers). And I can't define a SPF with One.com. </p>
  
  <p>All I want, is to be able to send emails that doesn't go to spam.</p>
  
  <p>Here are my questions:</p>
  
  <ol>
  <li><p>Is it because my header is off, or is it something 'deeper down'?</p></li>
  <li><p>Does the Gmail-spam filter ban single email accounts (such as
  <code>this@example.com</code>) or does it ban entire domains (such as
  <code>@example.com</code>)? </p></li>
  <li>Can one get a blacklisted email whitelisted
  somehow?</li>
  </ol>
  
  <hr>
  
  <p><strong>* Addition 1 *</strong></p>
  
  <p>Ok... I have now tried a number of things: </p>
  
  <ul>
  <li>I tried adding LoneWolfPR's returnpath, like recommended, and it didn't help. </li>
  <li>I contacted One.com (the hosting company), and confirmed with them, that it isn't possible to set a SPF-record or a DKIM-record. It still isn't. </li>
  <li>I considered setting up an 'unsubscribe'-link, with a link to a website with a form, but I didn't believe that approach. I mean - invoices are sent all the time, with e-mails. And why should you be able to unsubscribe an invoice?! Since that made so far from sense in my head, then I only tried it for about 20 minutes (obviously, without results).</li>
  </ul>
  
  <p>Here is my current email header (gotten from Gmail, by clicking the 'View original'):</p>
  
  <pre><code>Delivered-To: NEWLY-CREATED-GMAIL-ACCOUNT@gmail.com
  Received: by 10.76.75.104 with SMTP id b8csp48728oaw;
          Sat, 16 Mar 2013 17:32:56 -0700 (PDT)
  X-Received: by 10.152.116.45 with SMTP id jt13mr7897860lab.0.1363480376067;
          Sat, 16 Mar 2013 17:32:56 -0700 (PDT)
  Return-Path: &lt;XXX111@DOMAIN-NAME.dk&gt;
  Received: from mail-out2.b-one.net (mail-out2.one.com. [91.198.169.19])
          by mx.google.com with ESMTP id p10si4637427lbb.120.2013.03.16.17.32.55;
          Sat, 16 Mar 2013 17:32:55 -0700 (PDT)
  Received-SPF: neutral (google.com: 91.198.169.19 is neither permitted nor denied by best guess record for domain of XXX111@DOMAIN-NAME.dk) client-ip=91.198.169.19;
  Authentication-Results: mx.google.com;
         spf=neutral (google.com: 91.198.169.19 is neither permitted nor denied by best guess record for domain of XXX111@DOMAIN-NAME.dk) smtp.mail=XXX111@DOMAIN-NAME.dk
  Date: Sat, 16 Mar 2013 17:32:55 -0700 (PDT)
  Message-Id: &lt;51450f37.6a0b700a.6239.5dbcSMTPIN_ADDED_MISSING@mx.google.com&gt;
  Received: from localhost.localdomain (srv18.one.com [193.202.110.18])
      by mail-out2.b-one.net (Postfix) with ESMTP id F3D0B10365
      for &lt;NEWLY-CREATED-GMAIL-ACCOUNT@gmail.com&gt;; Sun, 17 Mar 2013 01:32:53 +0100 (CET)
  Received: from 85.218.159.219 by www.DOMAIN-NAME.dk via URL_TO_THE_SCRIPT.php with HTTP; Sun, 17 Mar 2013 00:32:53 +0000
  To: RECIEVERS_NAME &lt;NEWLY-CREATED-GMAIL-ACCOUNT@gmail.com&gt;
  Subject: EMAIL-SUBJECT
  X-PHP-Originating-Script: 87486:NAME-OF-THE-SCRIPT-THE-E-MAIL-WAS-SENT-FROM.php
  Reply-To: COMPANY NAME &lt;XXX111@DOMAIN-NAME.dk&gt;
  From: Besked fra COMPANY NAME &lt;XXX111@DOMAIN-NAME.dk&gt;
  MIME-Version: 1.0
  Sender: Besked fra COMPANY NAME &lt;XXX111@DOMAIN-NAME.dk&gt;
  Content-type: text/plain; charset="utf-8"; 
  X-Mailer: PHP5.3.21
  </code></pre>