Swiftmailer mails go into SPAM Folder
Asked Answered
S

2

5
$headers = "\r\n" . "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

$message = Swift_Message::newInstance()
                ->setSubject($subject)
                ->setFrom(array('[email protected]' => 'From Address'))
                ->setTo(array('[email protected]' => 'To Address'))
                ->setBody($message_plain_txt)
                ->addPart($message, 'text/html')
        ;
if ($file_name)
        {
            $message->attach(Swift_Attachment::fromPath($file_path));
        }

$result = $mailer->send($message);

In this case $filepath is the tmp path which I am using when a user attaches a files from a form and $file_name is the tmp file name $_FILES['file']['name'].

In this setup I am able to send mails but when there is an attachment, the mail goes into SPAM folder. If there is no attachment then mail goes into inbox.

This setup works perfectly fine when I am uploading a file from a location and not sending the attachment from a form.

I think it has got something to do with the email headers, but i am not able to figure out the error.

Can some one please help me with what mistake I am doing here.

Got it working by modifying headers to

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
Salol answered 10/2, 2012 at 20:48 Comment(0)
R
6

Add the following headers to avoid going to spam folder:

$headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         
Reseau answered 10/2, 2012 at 20:52 Comment(3)
thanks for ur help... even by adding the above code, mails are going into spam.Salol
I'm not sure what's the issue then, this worked for me, it might depend on the server spamfilter... hard to say what's the issue.Reseau
@Salol what additional modifications did you make?Penland
D
3

In versions of SwiftMailer in 2015, you would use built-in getHeaders() method to set headers.

$headers =& $message->getHeaders();
$headers->addIdHeader('Message-ID', "[email protected]");
$headers->addTextHeader('MIME-Version', '1.0');
$headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);
Dirndl answered 25/3, 2015 at 13:20 Comment(1)
From the doc, you don't need to return by ref (=&).Acinaciform

© 2022 - 2024 — McMap. All rights reserved.