0

Im using swiftmailer for sending mails via PHP. Most times it works fine. But sometimes, my mail Mails are landing in Spam-Folder.

Here my code, which sends the mails

function sendMail2($from,$to,$subject,$body,$attachment=NULL) {
    require_once 'include_apth/swiftmailer/swift_required.php';

    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance($subject);
    $message->setFrom($from);
    $message->setTo($to);
    $message->setBody($body, 'text/html');

    if($attachment) {
        $message->attach(Swift_Attachment::fromPath($attachment));
    }

    if(@$mailer->send($message)) {
        return true;
    }
    else {
        return false;
    }
}

any ideas, why its landing sometimes in spam-folder?

George Brighton
  • 5,131
  • 9
  • 27
  • 36
AppGeer
  • 725
  • 1
  • 11
  • 27
  • 1
    Look at the spammed emails. Usually spam filters will include spam scoring information in the headers and that'll give you an idea of why it's happening. – Marc B Nov 07 '13 at 14:04
  • Some spam filters tell you why they classify a mail as spam, e.g. in the mail headers. Have a look there. – Reeno Nov 07 '13 at 14:04
  • This has nothing to do with SwiftMailer but with the content, type and authenticity of your email. – SamV Nov 07 '13 at 14:04
  • The content is default. Like: ... "thank you for registration. Please click following link..." – AppGeer Nov 07 '13 at 15:00

2 Answers2

0

Add the below code and it will work perfectly

$headers =& $message->getHeaders();
$headers->addIdHeader('Message-ID', "b3eb7202-d2f1-11e4-b9d6-1681e6b88ec1@domain.com");
$headers->addTextHeader('MIME-Version', '1.0');
$headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);

Solution get from the below question Swiftmailer mails go into SPAM Folder

Community
  • 1
  • 1
Sarim Shekhani
  • 128
  • 1
  • 5
0

I was having the same issues with deliver-ability of emails. Getting all of the correct DNS settings, headers and the like isn't enough.

Most, if not all cloud-hosting, and home-ISPs IP ranges are on various lists of IPs from where emails are not expected to be sent from - and so they are more likely to be marked as spam.

The easiest way to solve that is to use a dedicated service where emails are well known to come from, and that the company spends a great deal of effort to get email delivery properly configured.

There are a number of well known such companies, many of which offer significant free tiers, as long as you are well behaved and send appropriate emails that aren't being marked as spam, or bounce. If you are hosted on Amazon EC2 for example, you can get over 60,000 emails delivered per month via AWS/SES. My my own systems, I have an account, currently free, with Mailgun, and a 'limit' of 10,000 email sends per month.

For Swiftmailer, there are a number of plugins that can, for example, use a HTTP API to send email to the service, which then is sent over SMTP in the usual way - with greatly improved deliverabilty.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110