PHP Mail Letter Class


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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
  34. ";
  35.  
  36. $this->addHeaders = $addHeaders;
  37. }
  38.  
  39. public function send($toEmail, $toName = null)
  40. {
  41. if (is_array($toEmail))
  42. {
  43. $index = 0;
  44. while ($toEmail[$index])
  45. {
  46. $overallResult = false;
  47. $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail[$index]>";
  48. $overallResult |= mail($to, $this->subject, $this->message, $this->addHeaders);
  49. return $overallResult;
  50. }
  51. }
  52. else
  53. {
  54. $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail>";
  55. return mail($to, $this->subject, $this->message, $this->addHeaders);
  56. }
  57.  
  58. }
  59. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.