mail delivered as spam - phpmailer, how to avoid

2020-02-04 22:25发布

问题:

I know that in first look many users mark this question as duplicate, but after reading more than 10 question I did not get any satisfactory answer, almost all question has answers having words like "There's not much you can do about it.", "I am not sure", "There is no sure shot trick" etc. that's why I am writing this question, and I think this is very generalized questions and every php developer faces it at least once, ok enough speech :) , now my question is..

I working on a project management application and am using phpmailer to send mail when any task is created or anybody comments on any bug mails are sent to related users, actually instead of sending mails as soon the action happens I have created a 'notifications' table where I actually save all mail data and a cron script then send all mails, here is some part of my cron script.

$query = "select * from notifications where  1 ";
    $projects = $obj_basic->get_query_data($query);  

    if(!empty($projects))
    {
        foreach($projects as $data)
        {       
            $message = html_entity_decode($data['content'], ENT_QUOTES);            
            list($ton, $email) =  get_name_email($data['to']);      

            if(!empty($email))
            {
                $query = "select send_notification from users where email='$email' AND send_notification !='1' ";
                $users = $obj_basic->get_query_data($query);
                if(!empty($users))
                {
                    $deleteQuery = "delete from notifications where id ='".$data['id']."'";
                    $obj_basic->run_query($deleteQuery, 'DELETE');
                    continue;
                }

                $comment_id = $data['reference_id'];
                $attribute = $data['attribute'];
                $mail = new PHPMailer();
                list($fromName, $fromEmail) =  get_name_email($data['from']);       
                if(!empty($comment_id) && $attribute == 'comment')
                {
                    $fromEmail = 'comment@changewebaddress.com';
                }

                $mail->SetFrom($fromEmail, $fromName);
                $mail->AddReplyTo($fromEmail, $fromName);
                $mail->AddAddress($email, $ton);
                $mail->BouncedTo = $fromEmail;
                $mail->IsHTML(true);                               
                $mail->Subject  = $data['subject'];              
                $mail->Body =  $message;        
                $MessageID = "<".md5($comment_id.'_'.$email).'@changewebaddress.com>';
                $mail->MessageID= $MessageID;

                if($mail->Send()) {         
                    if(!empty($comment_id) && $attribute == 'comment')
                    {
                        $query = "SELECT message_id FROM `project_comments` WHERE `id`='$comment_id'; ";
                        $project_comments = $obj_basic->get_query_data($query, 'SELECT');

                        if(!empty($project_comments))
                        {
                            $project_comments[0]['message_id'] = html_entity_decode(trim($project_comments[0]['message_id'], ","));
                            $query = "UPDATE  `project_comments` SET `message_id`=CONCAT_WS(',',  '".mysql_escape_string($project_comments[0]['message_id'])."', '".mysql_escape_string(html_entity_decode($MessageID))."') WHERE `id`='$comment_id'; ";
                            $obj_basic->run_query($query, 'UPDATE');
                        }                       
                    }                   
                    $deleteQuery = "delete from notifications where id ='".$data['id']."'"; 
                    $obj_basic->run_query($deleteQuery, 'DELETE'); 
                }           
            }
        }
    }

as per what I have tested everything look good, since I am using phpmailer it sets required header it also sets 'Return-Path:' and 'Reply-To:' in header.

Is there any exact solution on this issue

回答1:

  1. Make sure all of the required headers are being set.
  2. Check to see if there are any additional, optional headers that you should be setting.
  3. Sending an HTML/multipart message with mismatched text/HTML sections is frowned upon by some filters.
  4. Absolutely any mail that you send programmatically should have either a link or instructions on how to opt out. This is usually only enforced by human-curated blacklists and the abuse department at your ISP.
  5. Make sure your SMTP server is not blacklisted or has a poor reputation.
  6. Make sure your web server does not have a poor reputation. Some scanners include the reputation of every MTA in the chain.
  7. Review the content of your messages before sending them. If anything in it could even roughly be construed as trying to sell something to someone, change it.
  8. Sacrifice a small animal to the dark gods of email and hope against hope.
  9. Check the headers of the messages marked as spam to see if the spam filtering system left any useful information about why it was blocked.
  10. Ask the receiving server's admins why the message was blocked.
  11. Accept that there is no, and never will be an "exact solution on this issue". Ever.


回答2:

Is there any exact solution on this issue

Unfortunately not. It is a non-trivial issue with much complexity which prevents exact solutions.

Instead you need to find out for each email that is marked as spam what led to the spam scoring of it and reverse-engineer that then to the part(s) of the software and systems you use - either configuration (improve the configuration and setup) or processing (patch the software you use and re-compile/deploy).

If you want to improve the situation here on this website - you wrote that many existing questions are disappointing - please keep a worklog of that doing and document each case. Put this as an answer here so that future users can benefit from that. That would be not doing the same mistakes as the previous users have been done on site not doing this documentation and hence the information is missing.



回答3:

The accepted answer has a lot of good advice in it. Additionally, adding an SPF record to my DNS helped avoid spam filters. Learn more about SPF records:

http://en.wikipedia.org/wiki/Sender_Policy_Framework

Microsoft has a tool to help create such records:

http://www.microsoft.com/mscorp/safety/content/technologies/senderid/wizard/



回答4:

I know this already has an accepted answer but this is for others who might have had the same problem like me and ended up here. I had a website and mails send from my website ended up in spam boxes. Even if I tried using phpmailer and adding our companies mail-server as an SMTP.

However this could easily be resolved by sort of white listing the IP address of the website to the send filter of your mail server. How this is done exactly I don't know because the admin of the mail server did this for me(google it I guess).

Doing this means that if the receiving side does a DNS lookup or an IP lookup to your mail server. The mail-server tells the receiving side that it indeed was sent from him or his associating IP addresses and therefor it will not end up in the spambox.

However if you do this there is no need for the phpmailer and the standard mail() function works too.