How to avoid spam mail() function in php

  • diyath
  • Newbie
  • Newbie
  • diyath
  • Posts: 5

Post 3+ Months Ago

I use this class send mail but mails are gone as spam

PHP Code: [ Select ]
<?php
/***
   Email addresses can validate using email_validation(...) method.
   
   This class is use to send mails to valid email address(es)
   by calling methods as following order,
   1) set_header(.....)
   2) set_body(...)
   3) send_mail(.....)
***/
 
class GenerateMails{
   private $headers = array();
   private $recipients;
   private $contenttype;
   private $subject;
   private $email_msg;
   private $hdr;
   
   public $msg;
 
   
   /* This function use for validate email address(es).
      para :
         $email_addrses = This can be list of email addresses seperated by commas
                      or email addresses array
      return :
         true/false
   */
   public function email_validation($email_addrses)
   {
      $validate = true;
      if(!is_array($email_addrses))
      {
         $email_arry = explode(',',$email_addrses);
         $validate=$this->chk_email_arry($email_arry);
      }
      else
      {
         $validate=$this->chk_email_arry($email_addrses);
      }
      return $validate;
   }
   
 
   /* This function use for generate msg for email address(es)
      para :
         $email_arry = Email array
      return :
         true/false
   */
   private function chk_email_arry($email_arry)
   { 
      $rtn_val=true;
      foreach($email_arry as $val)
      {
         if(!filter_var($val,FILTER_VALIDATE_EMAIL))
         {
            $this->msg .= $val.' - Invalid Email Address.<br />';
            $rtn_val=false;  
         }
      }
      return $rtn_val;
   }
 
 
   /* This function use to set sender email & name
      para :
         $email = sender email
         $name = sender name
   */
   private function from($email,$name)
   {
       @ini_set('sendmail_from','test');
       $this->headers[] = 'From: '.$email.' <'.$name.'>';
   }
 
 
   /* This function use to set reply to email & name
      para :
         $email = Reply email
         $name = Reply name
   */
   private function reply_to($rplyto_email,$rplyto_name)
   {
       $this->headers[] = 'Reply-To: '.$rplyto_email.' <'.$rplyto_name.'>';
   }
 
 
   /* This function is use for add reciepients.
      para :
         $recipient_list = recipient list should separate by comma.
   */
    private function add_recipient($recipient_list)
    {  
      $this->recipients =$recipient_list;
    }
   
 
   /* This function is use for add CC.
      para :
         $cc_address_list = cc address list should separate by comma.
   */
    private function set_cc($cc_address_list)
    {
      $this->headers[] = "CC: ".$cc_address_list;
    }
   
 
   /* This function is use for add BCC.
      para :
         $bcc_address_list = BCC address list should separate by comma.
   */
    private function set_bcc($bcc_address_list)
    {
      $this->headers[] = "BCC: ".$bcc_address_list;
    }
   
 
 
   /* This function use to set header values.
      para :
         $header_type = normal - without attachment
                      attachment - with attachment
         $ishtml = true - content type is HTML
                  false - content is plain text
   */
   public function mail_header($header_type, $ishtml = true)
   {
      $this->headers[]  = "MIME-Version: 1.0";
     
      switch($header_type)
      {
         case "normal":
            if ($ishtml) {
               $this->contenttype = "text/html";
            } else {
               $this->contenttype = "text/plain";
            }
             $this->headers[]  = "Content-type: ".$this->contenttype."; charset=UTF-8";
         break;
         
         case "attachment":
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
           
             $this->headers[]  = "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
         break;
         
      }
   }
   
   
   /* This function use to set priorities
   */
   private function priority()
   {
       $this->headers[]  = "X-Priority: 3";
       $this->headers[]  = "X-Mailer: php";
   }
   
 
   /* This function use to set mail subject.
   */
   private function set_subject($subject)
   {
      $this->subject = $subject;
   }
   
 
   /* This function use to set mail attachmant(s)
      para :
         $attfile = file name(s) array
   */
   private function set_attachment($attfile)
   {
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
   
      // preparing attachments
      $file = fopen($attfile,"rb");
      $data = fread($file,filesize($attfile));
      fclose($file);
     
      $data = chunk_split(base64_encode($data));
     
      $message = "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$attfile\"\n" .
      "Content-Disposition: attachment;\n" . " filename=\"$attfile\"\n" .
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
      $message .= "--{$mime_boundary}\n";
 
      return $message;
   }
   
   
   /* This function use to set mail header
      para :
         $header_type = normal - without attachment
                      attachment - with attachment
         $ishtml = true - content type is HTML
                  false - content is plain text
         $from_email = from email address
         $from_name = from name
         $recipient_list = recipient email(s) seperated by commas
         $cc_address_list = CC email(s) seperated by commas
         $bcc_address_list = BCC email(s) seperated by commas
         $subject = email subject
   */
   public function set_header($header_type, $ishtml=true, $from_email, $from_name, $rplyto_email, $rplyto_name,$recipient_list, $cc_address_list, $bcc_address_list, $subject)
   {
      $this->mail_header($header_type, $ishtml=true);
      $this->priority();
      $this->from($from_email,$from_name);
      $this->reply_to($rplyto_email,$rplyto_name);
      $this->add_recipient($recipient_list);
      $this->set_cc($cc_address_list);
      $this->set_bcc($bcc_address_list);
      $this->set_subject($subject);
   }
   
   
   /* This function use to set mail body
      para :
         $email_msg = massege
         $email_msg_type = normal - without attachment
                    attachment - with attachment
         $attfile = file name(s) array
   */
   public function set_body($email_msg,$email_msg_type,$attfile)
   {
     
      switch($email_msg_type)
      {
         case "normal":
            $this->email_msg = wordwrap($email_msg,70);
         break;
         
         case "attachment":
         
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
     
            $this->email_msg = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_msg . "\n\n";
            $this->email_msg .= "--{$mime_boundary}\n";
           
            if(is_array($attfile))
            {
               for($x=0;$x<count($attfile);$x++)
               {
                  if(file_exists($attfile[$x]))
                  {
                     $this->email_msg .= $this->set_attachment($attfile[$x]);
                  }
                  else
                  {
                     $this->email_msg .= ' No Data found for the file '.$attfile[$x]."--{$mime_boundary}\n";
                  }
               }
            }
/*          else
            {
               $this->email_msg = $this->set_attachment($attfile);
            }
*/
         break;
      }
      $this->email_msg = wordwrap($this->email_msg,70);
   }
   
   
   /* This function use to send mail
      return :
         treu/false
   */
   public function send_mail()
   {
      $this->hdr = implode("\r\n",$this->headers);
      unset($this->headers);
      return mail($this->recipients, $this->subject, $this->email_msg, $this->hdr);
   }
}
 
?>
  1. <?php
  2. /***
  3.    Email addresses can validate using email_validation(...) method.
  4.    
  5.    This class is use to send mails to valid email address(es)
  6.    by calling methods as following order,
  7.    1) set_header(.....)
  8.    2) set_body(...)
  9.    3) send_mail(.....)
  10. ***/
  11.  
  12. class GenerateMails{
  13.    private $headers = array();
  14.    private $recipients;
  15.    private $contenttype;
  16.    private $subject;
  17.    private $email_msg;
  18.    private $hdr;
  19.    
  20.    public $msg;
  21.  
  22.    
  23.    /* This function use for validate email address(es).
  24.       para :
  25.          $email_addrses = This can be list of email addresses seperated by commas
  26.                       or email addresses array
  27.       return :
  28.          true/false
  29.    */
  30.    public function email_validation($email_addrses)
  31.    {
  32.       $validate = true;
  33.       if(!is_array($email_addrses))
  34.       {
  35.          $email_arry = explode(',',$email_addrses);
  36.          $validate=$this->chk_email_arry($email_arry);
  37.       }
  38.       else
  39.       {
  40.          $validate=$this->chk_email_arry($email_addrses);
  41.       }
  42.       return $validate;
  43.    }
  44.    
  45.  
  46.    /* This function use for generate msg for email address(es)
  47.       para :
  48.          $email_arry = Email array
  49.       return :
  50.          true/false
  51.    */
  52.    private function chk_email_arry($email_arry)
  53.    { 
  54.       $rtn_val=true;
  55.       foreach($email_arry as $val)
  56.       {
  57.          if(!filter_var($val,FILTER_VALIDATE_EMAIL))
  58.          {
  59.             $this->msg .= $val.' - Invalid Email Address.<br />';
  60.             $rtn_val=false;  
  61.          }
  62.       }
  63.       return $rtn_val;
  64.    }
  65.  
  66.  
  67.    /* This function use to set sender email & name
  68.       para :
  69.          $email = sender email
  70.          $name = sender name
  71.    */
  72.    private function from($email,$name)
  73.    {
  74.        @ini_set('sendmail_from','test');
  75.        $this->headers[] = 'From: '.$email.' <'.$name.'>';
  76.    }
  77.  
  78.  
  79.    /* This function use to set reply to email & name
  80.       para :
  81.          $email = Reply email
  82.          $name = Reply name
  83.    */
  84.    private function reply_to($rplyto_email,$rplyto_name)
  85.    {
  86.        $this->headers[] = 'Reply-To: '.$rplyto_email.' <'.$rplyto_name.'>';
  87.    }
  88.  
  89.  
  90.    /* This function is use for add reciepients.
  91.       para :
  92.          $recipient_list = recipient list should separate by comma.
  93.    */
  94.     private function add_recipient($recipient_list)
  95.     {  
  96.       $this->recipients =$recipient_list;
  97.     }
  98.    
  99.  
  100.    /* This function is use for add CC.
  101.       para :
  102.          $cc_address_list = cc address list should separate by comma.
  103.    */
  104.     private function set_cc($cc_address_list)
  105.     {
  106.       $this->headers[] = "CC: ".$cc_address_list;
  107.     }
  108.    
  109.  
  110.    /* This function is use for add BCC.
  111.       para :
  112.          $bcc_address_list = BCC address list should separate by comma.
  113.    */
  114.     private function set_bcc($bcc_address_list)
  115.     {
  116.       $this->headers[] = "BCC: ".$bcc_address_list;
  117.     }
  118.    
  119.  
  120.  
  121.    /* This function use to set header values.
  122.       para :
  123.          $header_type = normal - without attachment
  124.                       attachment - with attachment
  125.          $ishtml = true - content type is HTML
  126.                   false - content is plain text
  127.    */
  128.    public function mail_header($header_type, $ishtml = true)
  129.    {
  130.       $this->headers[]  = "MIME-Version: 1.0";
  131.      
  132.       switch($header_type)
  133.       {
  134.          case "normal":
  135.             if ($ishtml) {
  136.                $this->contenttype = "text/html";
  137.             } else {
  138.                $this->contenttype = "text/plain";
  139.             }
  140.              $this->headers[]  = "Content-type: ".$this->contenttype."; charset=UTF-8";
  141.          break;
  142.          
  143.          case "attachment":
  144.             $semi_rand = md5(time());
  145.             $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  146.            
  147.              $this->headers[]  = "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
  148.          break;
  149.          
  150.       }
  151.    }
  152.    
  153.    
  154.    /* This function use to set priorities
  155.    */
  156.    private function priority()
  157.    {
  158.        $this->headers[]  = "X-Priority: 3";
  159.        $this->headers[]  = "X-Mailer: php";
  160.    }
  161.    
  162.  
  163.    /* This function use to set mail subject.
  164.    */
  165.    private function set_subject($subject)
  166.    {
  167.       $this->subject = $subject;
  168.    }
  169.    
  170.  
  171.    /* This function use to set mail attachmant(s)
  172.       para :
  173.          $attfile = file name(s) array
  174.    */
  175.    private function set_attachment($attfile)
  176.    {
  177.       $semi_rand = md5(time());
  178.       $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  179.    
  180.       // preparing attachments
  181.       $file = fopen($attfile,"rb");
  182.       $data = fread($file,filesize($attfile));
  183.       fclose($file);
  184.      
  185.       $data = chunk_split(base64_encode($data));
  186.      
  187.       $message = "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$attfile\"\n" .
  188.       "Content-Disposition: attachment;\n" . " filename=\"$attfile\"\n" .
  189.       "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  190.       $message .= "--{$mime_boundary}\n";
  191.  
  192.       return $message;
  193.    }
  194.    
  195.    
  196.    /* This function use to set mail header
  197.       para :
  198.          $header_type = normal - without attachment
  199.                       attachment - with attachment
  200.          $ishtml = true - content type is HTML
  201.                   false - content is plain text
  202.          $from_email = from email address
  203.          $from_name = from name
  204.          $recipient_list = recipient email(s) seperated by commas
  205.          $cc_address_list = CC email(s) seperated by commas
  206.          $bcc_address_list = BCC email(s) seperated by commas
  207.          $subject = email subject
  208.    */
  209.    public function set_header($header_type, $ishtml=true, $from_email, $from_name, $rplyto_email, $rplyto_name,$recipient_list, $cc_address_list, $bcc_address_list, $subject)
  210.    {
  211.       $this->mail_header($header_type, $ishtml=true);
  212.       $this->priority();
  213.       $this->from($from_email,$from_name);
  214.       $this->reply_to($rplyto_email,$rplyto_name);
  215.       $this->add_recipient($recipient_list);
  216.       $this->set_cc($cc_address_list);
  217.       $this->set_bcc($bcc_address_list);
  218.       $this->set_subject($subject);
  219.    }
  220.    
  221.    
  222.    /* This function use to set mail body
  223.       para :
  224.          $email_msg = massege
  225.          $email_msg_type = normal - without attachment
  226.                     attachment - with attachment
  227.          $attfile = file name(s) array
  228.    */
  229.    public function set_body($email_msg,$email_msg_type,$attfile)
  230.    {
  231.      
  232.       switch($email_msg_type)
  233.       {
  234.          case "normal":
  235.             $this->email_msg = wordwrap($email_msg,70);
  236.          break;
  237.          
  238.          case "attachment":
  239.          
  240.             $semi_rand = md5(time());
  241.             $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  242.      
  243.             $this->email_msg = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_msg . "\n\n";
  244.             $this->email_msg .= "--{$mime_boundary}\n";
  245.            
  246.             if(is_array($attfile))
  247.             {
  248.                for($x=0;$x<count($attfile);$x++)
  249.                {
  250.                   if(file_exists($attfile[$x]))
  251.                   {
  252.                      $this->email_msg .= $this->set_attachment($attfile[$x]);
  253.                   }
  254.                   else
  255.                   {
  256.                      $this->email_msg .= ' No Data found for the file '.$attfile[$x]."--{$mime_boundary}\n";
  257.                   }
  258.                }
  259.             }
  260. /*          else
  261.             {
  262.                $this->email_msg = $this->set_attachment($attfile);
  263.             }
  264. */
  265.          break;
  266.       }
  267.       $this->email_msg = wordwrap($this->email_msg,70);
  268.    }
  269.    
  270.    
  271.    /* This function use to send mail
  272.       return :
  273.          treu/false
  274.    */
  275.    public function send_mail()
  276.    {
  277.       $this->hdr = implode("\r\n",$this->headers);
  278.       unset($this->headers);
  279.       return mail($this->recipients, $this->subject, $this->email_msg, $this->hdr);
  280.    }
  281. }
  282.  
  283. ?>
Moderator Remark: added [code] tags
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

When you say the mails are "gone as spam", do you mean the e-mail isn't even able to be sent as it comes back with an error? Or does the e-mail get sent but at your destination the e-mail is showing up in the spam or bulk folder? Could you be more specific what you mean?
  • diyath
  • Newbie
  • Newbie
  • diyath
  • Posts: 5

Post 3+ Months Ago

the e-mail get sent but at my destination the e-mail is showing up in the spam
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

That could be a few things.

The first thing I could do is make sure your sending proper headers along with your emails. The next thing would to check to see if your server IP has been black listed.

http://whatismyipaddress.com/blacklist-check
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

For the e-mail at your destination where it is showing up as spam, I would suggest looking at the e-mail headers there to see if anything stands out such as failed SPF checks, or any other tags that could indicate why its being labeled as spam. Also I would check every IP address in the headers besides your own to see if they are blacklisted as SpooF mentioned. Sometimes when you send e-mail out they go through a SMTP server that is blacklisted which is why I would check more IP addresses than just your own.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 54 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.