RedVodkaJelly Logo

PHP Email Class

POSTED AT 15:34 on 23rd May 2008

This is an easy to use PHP Email class making it easy to send emails with attachments in PHP.

It supports:

  • Sending To, CC, BCC.

  • Sending email attachments.

  • Setting email priorities.

  • Sending plain and HTML emails.

This class is by no means complete and if you want more features I suggest you check out PHPMailer.

Source Code

  1. <?php 
  2. /* 
  3. * PHP Email Class 
  4. * Class to deal with sending emails in PHP 
  5. * Makes it easy to send emails in PHP with attachments and such. 
  6. *  
  7. * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com 
  8. *  
  9. * LICENSE 
  10. * ——- 
  11. * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used. 
  12. * USAGE 
  13. * —– 
  14. *   $objEmail = new RVJ_mail(); 
  15. *   $objEmail->addUser("jacob@redvodkajelly.com"); 
  16. *   $objEmail->setFrom("Frozensheep Ltd <admin@frozensheep.com>"); 
  17. *   $objEmail->setHTML(); 
  18. *   //$objEmail->setPlain(); 
  19. *   $objEmail->setPriority(3); 
  20. *   $objEmail->addReadConfirmationEmail("admin@frozensheep.com"); 
  21. *   $objEmail->addAttachment("logo.gif", ‘logo.gif’, ‘image/gif’); 
  22. *   $objEmail->addAttachment("word.doc", ‘word.doc’, ‘application/msword’); 
  23. *   $objEmail->addAttachment("excel.xls", ‘excel.xls’, ‘application/vnd.ms-excel’); 
  24. *   $objEmail->setSubject("Testing new class"); 
  25. *   $objEmail->setContent("hey <b>dude</b>"); 
  26. *   $objEmail->send(); 
  27. */ 
  28.  
  29. class RVJ_mail { 
  30.     
  31.    var $arrTo = array(); 
  32.    var $arrCC = array(); 
  33.    var $arrBCC = array(); 
  34.    var $strContent = ""; 
  35.    var $strSubject = ""; 
  36.    var $arrHeader = array(); 
  37.    var $arrAttachments = array(); 
  38.    var $numPriority = 3; 
  39.    var $strCharSet = "iso-8859-1"; 
  40.    var $strContentType = "text/plain"; 
  41.    var $strEncoding = "8bit"; 
  42.  
  43.    /* 
  44.    * 
  45.    *   @Method:      addUser 
  46.    *   @Parameters:   1 
  47.    *   @Param-1:      strAddress - String - Holds an email address 
  48.    *   @Description:   Adds a user to send the email to 
  49.    * 
  50.    */     
  51.    function addUser($strAddress){ 
  52.       if($this->emailCheck($strAddress)){             
  53.          $this->arrTo[] = $strAddress; 
  54.          return 1; 
  55.       }else{ 
  56.          return 0; 
  57.       } 
  58.    } 
  59.  
  60.    /* 
  61.    * 
  62.    *   @Method:      removeUser 
  63.    *   @Parameters:   1 
  64.    *   @Param-1:      strAddress - String - Holds an email address 
  65.    *   @Description:   Removes an address from the array 
  66.    * 
  67.    */     
  68.    function removeUser($strAddress){ 
  69.       //create array or arrays to check 
  70.       $arrVars = array(’arrTo’, ‘arrCC’, ‘arrBCC’); 
  71.       foreach($arrVars as $resVars){ 
  72.          //sees if the item exists in the array 
  73.          $numKey = array_search($strAddress, $this->{$resVars}); 
  74.          if(isset($numKey)){ 
  75.             //remove the email address 
  76.             $this->{$resVars}[$numKey] = "";                         
  77.          } 
  78.          unset($numKey); 
  79.       } 
  80.    } 
  81.  
  82.    /* 
  83.    * 
  84.    *   @Method:      addCC 
  85.    *   @Parameters:   1 
  86.    *   @Param-1:      strAddress - String - Holds an email address 
  87.    *   @Description:   Adds a CC user to send to 
  88.    * 
  89.    */        
  90.    function addCC($strAddress){ 
  91.       if($this->emailCheck($strAddress)){    
  92.          $this->arrCC[] = $strAddress; 
  93.          return 1; 
  94.       }else{ 
  95.          return 0; 
  96.       } 
  97.    } 
  98.     
  99.    /* 
  100.    * 
  101.    *   @Method:      addBCC 
  102.    *   @Parameters:   1 
  103.    *   @Param-1:      strAddress - String - Holds an email address 
  104.    *   @Description:   Adds a BCC user to send to 
  105.    * 
  106.    */     
  107.    function addBCC($strAddress){ 
  108.       if($this->emailCheck($strAddress)){ 
  109.          $this->arrBCC[] = $strAddress; 
  110.          return 1; 
  111.       }else{ 
  112.          return 0; 
  113.       } 
  114.    } 
  115.  
  116.    /* 
  117.    * 
  118.    *   @Method:      addReplyTo 
  119.    *   @Parameters:   1 
  120.    *   @Param-1:      strEmail - String - Holds the reply-to email address 
  121.    *   @Description:   Adds a reply-to email address 
  122.    * 
  123.    */     
  124.    function addReplyTo($strEmail){ 
  125.       if($this->emailCheck($strEmail)){ 
  126.          $this->addHeader("Reply-to: $strEmail"); 
  127.       } 
  128.    } 
  129.  
  130.  
  131.    /* 
  132.    * 
  133.    *   @Method:      setContent 
  134.    *   @Parameters:   1 
  135.    *   @Param-1:      strText - String - Holds the email content 
  136.    *   @Description:   Adds the content of the email 
  137.    * 
  138.    */     
  139.    function setContent($strText){ 
  140.       $this->strContent = $strText; 
  141.    } 
  142.  
  143.    /* 
  144.    * 
  145.    *   @Method:      setSubject 
  146.    *   @Parameters:   1 
  147.    *   @Param-1:      strText - String - Holds the email subject 
  148.    *   @Description:   Adds the email subject 
  149.    * 
  150.    */        
  151.    function setSubject($strText){ 
  152.       $this->strSubject = $strText; 
  153.    } 
  154.  
  155.    /* 
  156.    * 
  157.    *   @Method:      setFrom 
  158.    *   @Parameters:   1 
  159.    *   @Param-1:      strText - String - Holds the details of who the email is from 
  160.    *   @Description:   Adds the sender info to the headers 
  161.    * 
  162.    */        
  163.    function setFrom($strText){ 
  164.       $this->addHeader("From: $strText"); 
  165.    } 
  166.  
  167.    /* 
  168.    * 
  169.    *   @Method:      setHTML 
  170.    *   @Parameters:   0 
  171.    *   @Description:   Sets the email to allow html 
  172.    * 
  173.    */        
  174.    function setHTML(){    
  175.       $this->strContentType = "text/html";    
  176.    } 
  177.  
  178.    /* 
  179.    * 
  180.    *   @Method:      setPlain 
  181.    *   @Parameters:   0 
  182.    *   @Description:   Sets the email to plain text 
  183.    * 
  184.    */        
  185.    function setPlain(){    
  186.       $this->strContentType = "text/plain";       
  187.    } 
  188.  
  189.    /* 
  190.    * 
  191.    *   @Method:      setPriority 
  192.    *   @Parameters:   1 
  193.    *   @Param-1:      numPriority - Number - The email priority 
  194.    *   @Description:   Sets the email priority from 1 - Urgent to 5 - not so 
  195.    * 
  196.    */        
  197.    function setPriority($numPriority){ 
  198.       $this->numPriority = $numPriority; 
  199.    }    
  200.  
  201.    /* 
  202.    * 
  203.    *   @Method:      addReadConfirmationEmail 
  204.    *   @Parameters:   1 
  205.    *   @Param-1:      strEmail - String - Email address to send read confirmation to 
  206.    *   @Description:   Ensures that a confirmation email is sent when read 
  207.    * 
  208.    */        
  209.    function addReadConfirmationEmail($strEmail){ 
  210.       if($this->emailCheck($strEmail)){ 
  211.          $this->addHeader("Disposition-Notification-To: <$strEmail>"); 
  212.       } 
  213.    } 
  214.     
  215.    /* 
  216.    * 
  217.    *   @Method:      addHeader 
  218.    *   @Parameters:   1 
  219.    *   @Param-1:      strText - String - Holds the header details 
  220.    *   @Description:   Adds the header details 
  221.    * 
  222.    */        
  223.    function addHeader($strText){ 
  224.       $this->arrHeader[] = $strText; 
  225.    } 
  226.  
  227.    /* 
  228.    * 
  229.    *   @Method:      addAttachment 
  230.    *   @Parameters:   3 
  231.    *   @Param-1:      strFile - String - The file that you want to attach 
  232.    *   @Param-1:      strName - String - The name of the attachment you want displayed 
  233.    *   @Param-1:      strType - String - The MIME type of the file 
  234.    *   @Description:   Adds an attachment to be sent with the email 
  235.    * 
  236.    */        
  237.    function addAttachment($strFile, $strName, $strType){ 
  238.       //check to make sure the file exists 
  239.       if(file_exists($strFile)){ 
  240.          $this->arrAttachments[] = array(’path’ => $strFile, ‘name’ => $strName, ‘type’ => $strType); 
  241.       } 
  242.    } 
  243.     
  244.    /* 
  245.    * 
  246.    *   @Method:      send 
  247.    *   @Parameters:   0 
  248.    *   @Description:   Sends the email to all the required people 
  249.    * 
  250.    */     
  251.    function send(){ 
  252.       //get all the emails in a string to use 
  253.       $strTo = implode(", ", $this->arrTo); 
  254.        
  255.       //add any CC addresses if needed 
  256.       if($this->arrCC){ 
  257.          $this->addHeader("Cc: ".implode(", ", $this->arrCC)); 
  258.       } 
  259.  
  260.       //add any BCC addresses if needed 
  261.       if($this->arrBCC){ 
  262.          $this->addHeader("Bcc: ".implode(", ", $this->arrBCC)); 
  263.       } 
  264.        
  265.       //append any attachments to the end of the content 
  266.       if(count($this->arrAttachments)){ 
  267.          $this->appendAttachments(); 
  268.       } 
  269.        
  270.       //add the additional headers 
  271.       $this->addAdditionalHeaders(); 
  272.        
  273.       //implode the headers as they need to be in a single string 
  274.       $strHeader = implode("\r\n", $this->arrHeader); 
  275.        
  276.       //send the email 
  277.       mail($strTo, $this->strSubject, $this->strContent, $strHeader); 
  278.    } 
  279.  
  280.    /* 
  281.    * 
  282.    *   @Method:      appendAttachments 
  283.    *   @Parameters:   0 
  284.    *   @Description:   Appends any attachments to the end of the email content 
  285.    * 
  286.    */     
  287.    function appendAttachments(){ 
  288.       //create a unique boundary value 
  289.       $strBoundary = "H2O-".time(); 
  290.        
  291.       //add the boundary to the headers 
  292.       $this->addHeader("Content-Type: multipart/alternitive; boundary=$strBoundary"); 
  293.        
  294.       //if there is content to the email already we need to add the boundary and content type 
  295.       if($this->strContent){ 
  296.          $strContent = "–$strBoundary\r\n"; 
  297.          $strContent .= "Content-Transfer-Encoding: {$this->strEncoding}\r\n"; 
  298.          $strContent .= "Content-type: {$this->strContentType}; charset={$this->strCharSet}\r\n\r\n"; 
  299.           
  300.          $this->strContent = $strContent.$this->strContent."\r\n\r\n\r\n"; 
  301.       } 
  302.        
  303.       //loop through all the attachments 
  304.       foreach($this->arrAttachments as $arrFile){ 
  305.          //read the content of the file into a string and base64 encode and split into the correct chunk size 
  306.          $strData = chunk_split(base64_encode(implode("", file($arrFile['path'])))); 
  307.           
  308.          //add the attachments       
  309.          $strAttachment = "–$strBoundary\r\n"; 
  310.          $strAttachment .= "Content-Transfer-Encoding: base64\r\n"; 
  311.          $strAttachment .= "Content-Type: {$arrFile['type']}; name={$arrFile['name']}\r\n"; 
  312.          $strAttachment .= "Content-Disposition: attachment; filename={$arrFile['name']}\r\n\r\n"; 
  313.          $strAttachment .= "$strData\r\n"; 
  314.           
  315.          //add the attachment to the email content 
  316.          $this->strContent .= $strAttachment; 
  317.           
  318.          unset($strAttachment); 
  319.       } 
  320.        
  321.       //add the closing boundary    
  322.       $this->strContent .= "–$strBoundary–"; 
  323.    } 
  324.  
  325.    /* 
  326.    * 
  327.    *   @Method:      addAdditionalHeaders 
  328.    *   @Parameters:   0 
  329.    *   @Description:   Adds additional headers for content type and priority etc 
  330.    * 
  331.    */     
  332.    function addAdditionalHeaders(){ 
  333.       $this->addHeader("MIME-Version: 1.0"); 
  334.        
  335.       //add the content type and charset if there are no attachments 
  336.       if(!count($this->arrAttachments)){ 
  337.          $this->addHeader("Content-type: {$this->strContentType}; charset={$this->strCharSet}");       
  338.       } 
  339.        
  340.       //add the priority 
  341.       $this->addHeader("X-Priority: $this->numPriority");    
  342.    } 
  343.     
  344.    /* 
  345.    * 
  346.    *   @Method:      emailCheck 
  347.    *   @Parameters:   1 
  348.    *   @Param-1:      strAddress - String - Holds an email address 
  349.    *   @Description:   Checks to ensure that the email address is valid 
  350.    * 
  351.    */        
  352.    function emailCheck($strAddress){ 
  353.       if(ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $strAddress)){ 
  354.          return 1; 
  355.       }else{ 
  356.          return 0; 
  357.       } 
  358.    } 
  359.  
  360. ?>