Posted By

Zayac on 11/27/12


Tagged

class mail headers send letter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wrximpreza1987


PHP Mail Letter Class


 / Published in: PHP
 

This is a Class that allows you create a letter with correct headers and send it to multi-emails.

  1. class Letter
  2. {
  3. private $subject;
  4. private $message;
  5.  
  6. private $addHeaders;
  7. private $charset;
  8.  
  9. function __construct( $subject, $message, $fromEmail = null, $fromName = null, $charset = 'utf-8')
  10. {
  11. $this->charset = $charset;
  12.  
  13. $this->subject = "=?$charset?b?" . base64_encode($subject) . "?=";
  14.  
  15. if (is_array($message))
  16. {
  17. $messageString = '';
  18. foreach ($message as $value)
  19. $messageString .= "$value\n";
  20. $message = $messageString;
  21. }
  22.  
  23. $this->message = $message;
  24.  
  25. $addHeadersList = array(
  26. 'From' => "=?$charset?b?" .base64_encode($fromName). "?= <$fromEmail>",
  27. 'Content-Type' => 'text/plain; charset="' .$charset. '"',
  28. 'Content-Transfer-Encoding' => '8bit'
  29. );
  30.  
  31. $addHeaders = '';
  32. foreach ($addHeadersList as $key => $value)
  33. $addHeaders .= "$key: $value\r\n";
  34.  
  35. $this->addHeaders = $addHeaders;
  36. }
  37.  
  38. public function send($toEmail, $toName = null)
  39. {
  40. if (is_array($toEmail))
  41. {
  42. $index = 0;
  43. while ($toEmail[$index])
  44. {
  45. $overallResult = false;
  46. $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail[$index]>";
  47. $overallResult |= mail($to, $this->subject, $this->message, $this->addHeaders);
  48. return $overallResult;
  49. }
  50. }
  51. else
  52. {
  53. $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail>";
  54. return mail($to, $this->subject, $this->message, $this->addHeaders);
  55. }
  56.  
  57. }
  58. }

Report this snippet  

You need to login to post a comment.