petschko

E-Mail Class

Feb 1st, 2016
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.11 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [[email protected]]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 10.08.2015
  6.  * Time: 12:27
  7.  * Update: 09.04.2016
  8.  * Version: 1.2.0 (Changed Class-Name & Website - Added alias functions for getSender and setSender)
  9.  * 1.1.2 (Reformat Code)
  10.  * 1.1.1 (Add CC, BCC, Max, Line Length & Reply-To Address)
  11.  *
  12.  * Notes: Class for sending easy Mails via PHP
  13.  */
  14.  
  15. /**
  16.  * Class Email
  17.  */
  18. class Email {
  19.     private $charset;
  20.     private $to = false;
  21.     private $cc = false;
  22.     private $bcc = false;
  23.     private $maxLineLength = false;
  24.     private $sender = false;
  25.     private $replyTo = false;
  26.     private $subject = false;
  27.     private $msg = false;
  28.  
  29.     /**
  30.      * Creates a new instance
  31.      *
  32.      * @param string $charset - Encoding of this (Default: utf-8)
  33.      */
  34.     public function __construct($charset = 'utf-8') {
  35.         $this->setCharset($charset);
  36.     }
  37.  
  38.     /**
  39.      * Clears Memory
  40.      */
  41.     public function __destruct() {
  42.         unset($this->charset);
  43.         unset($this->to);
  44.         unset($this->cc);
  45.         unset($this->bcc);
  46.         unset($this->maxLineLength);
  47.         unset($this->sender);
  48.         unset($this->replyTo);
  49.         unset($this->subject);
  50.         unset($this->msg);
  51.     }
  52.  
  53.     /**
  54.      * Get the current Encoding of this
  55.      *
  56.      * @return string - Encoding of this
  57.      */
  58.     public function getCharset() {
  59.         return $this->charset;
  60.     }
  61.  
  62.     /**
  63.      * Set the Encoding of this
  64.      *
  65.      * @param string $charset - New Encoding of this
  66.      */
  67.     private function setCharset($charset) {
  68.         $this->charset = $charset;
  69.     }
  70.  
  71.     /**
  72.      * Get the current receiver
  73.      *
  74.      * @return array|bool - The receiver or false if none is set
  75.      */
  76.     private function getTo() {
  77.         return $this->to;
  78.     }
  79.  
  80.     /**
  81.      * Returns a List of all "To"-E-Mail Addresses as string
  82.      *
  83.      * @return string - E-Mail list of all "To" Receiver
  84.      */
  85.     public function getToList() {
  86.         return $this->createEMailList($this->getTo());
  87.     }
  88.  
  89.     /**
  90.      * Set the Receiver
  91.      *
  92.      * @param array|bool $to - Receiver
  93.      */
  94.     private function setTo($to) {
  95.         $this->to = $to;
  96.     }
  97.  
  98.     /**
  99.      * Get the the current CC(s)
  100.      *
  101.      * @return array|bool - The current Copy-To or false if none is set
  102.      */
  103.     private function getCc() {
  104.         return $this->cc;
  105.     }
  106.  
  107.     /**
  108.      * Returns a List of all "CC"-E-Mail Addresses as string
  109.      *
  110.      * @return string - E-Mail list of all "CC" Receiver
  111.      */
  112.     public function getCcList() {
  113.         return $this->createEMailList($this->getCC());
  114.     }
  115.  
  116.     /**
  117.      * Set CC
  118.      *
  119.      * @param array|bool $cc - CC(s) false if unset
  120.      */
  121.     private function setCc($cc) {
  122.         $this->cc = $cc;
  123.     }
  124.  
  125.     /**
  126.      * Get the current BCC(s)
  127.      *
  128.      * @return array|bool - The current Blind-Copy-To or false if none is set
  129.      */
  130.     private function getBcc() {
  131.         return $this->bcc;
  132.     }
  133.  
  134.     /**
  135.      * Returns a List of all "BCC"-E-Mail Addresses as string
  136.      *
  137.      * @return string - E-Mail list of all "BCC" Receiver
  138.      */
  139.     public function getBccList() {
  140.         return $this->createEMailList($this->getBcc());
  141.     }
  142.  
  143.     /**
  144.      * Set the current BCC(s)
  145.      *
  146.      * @param array|bool $bcc - BCC(s) false if unset
  147.      */
  148.     private function setBcc($bcc) {
  149.         $this->bcc = $bcc;
  150.     }
  151.  
  152.     /**
  153.      * Get the sender
  154.      *
  155.      * @return string - Sender-Address
  156.      */
  157.     public function getSender() {
  158.         return $this->sender;
  159.     }
  160.  
  161.     /**
  162.      * Alias for getSender
  163.      *
  164.      * @return string - Sender-Address
  165.      */
  166.     public function getFrom() {
  167.         return $this->getSender();
  168.     }
  169.  
  170.     /**
  171.      * Set the sender
  172.      *
  173.      * @param string $sender - Sender-Address
  174.      */
  175.     public function setSender($sender) {
  176.         self::checkValidEMail($sender);
  177.  
  178.         $this->sender = $sender;
  179.     }
  180.  
  181.     /**
  182.      * Alias for setSender
  183.      *
  184.      * @param string $from - Sender Address
  185.      */
  186.     public function setFrom($from) {
  187.         $this->setSender($from);
  188.     }
  189.  
  190.     /**
  191.      * Returns the Reply-To Address
  192.      *
  193.      * @return bool|string - Reply-To Address or false if none is set
  194.      */
  195.     public function getReplyTo() {
  196.         return $this->replyTo;
  197.     }
  198.  
  199.     /**
  200.      * Sets the Reply-To Address
  201.      *
  202.      * @param bool|string $replyTo - Reply-To or false if none is set
  203.      */
  204.     public function setReplyTo($replyTo) {
  205.         self::checkValidEMail($replyTo);
  206.  
  207.         $this->replyTo = $replyTo;
  208.     }
  209.  
  210.     /**
  211.      * Get the subject
  212.      *
  213.      * @return string - subject
  214.      */
  215.     public function getSubject() {
  216.         return $this->subject;
  217.     }
  218.  
  219.     /**
  220.      * Set the subject
  221.      *
  222.      * @param string $subject - The subject
  223.      */
  224.     public function setSubject($subject) {
  225.         $this->subject = $subject;
  226.     }
  227.  
  228.     /**
  229.      * Get the Mail-Message
  230.      *
  231.      * @return string - Message
  232.      */
  233.     public function getMsg() {
  234.         return $this->msg;
  235.     }
  236.  
  237.     /**
  238.      * Set the Mail-Message
  239.      *
  240.      * @param string $msg - Message
  241.      */
  242.     public function setMsg($msg) {
  243.         $this->msg = $msg;
  244.     }
  245.  
  246.     /**
  247.      * Returns the max Chars that can be in each line
  248.      *
  249.      * @return bool|int - Characters per line or false if none limit is set
  250.      */
  251.     public function getMaxLineLength() {
  252.         return $this->maxLineLength;
  253.     }
  254.  
  255.     /**
  256.      * Set the max Chars that can be in each line
  257.      *
  258.      * @param bool|int $maxLineLength - Characters per Line or false if no limit
  259.      */
  260.     public function setMaxLineLength($maxLineLength) {
  261.         $this->maxLineLength = $maxLineLength;
  262.     }
  263.  
  264.     /**
  265.      * Adds an E-Mail Address to the receiver
  266.      *
  267.      * @param string $to - The E-Mail Address that you want to add
  268.      */
  269.     public function addTo($to) {
  270.         self::checkValidEMail($to);
  271.  
  272.         $this->to[] = $to;
  273.     }
  274.  
  275.     /**
  276.      * Removes a Receiver from the to array
  277.      *
  278.      * @param string $to - The E-Mail Address that you want to remove
  279.      */
  280.     public function removeTo($to) {
  281.         $this->setTo(self::removeFromArray($to, $this->getTo()));
  282.     }
  283.  
  284.     /**
  285.      * Adds an E-Mail Address to CC
  286.      *
  287.      * @param string $cc - The E-Mail Address that you want to add
  288.      */
  289.     public function addCc($cc) {
  290.         self::checkValidEMail($cc);
  291.  
  292.         $this->cc[] = $cc;
  293.     }
  294.  
  295.     /**
  296.      * Removes an E-Mail Address from the CC array
  297.      *
  298.      * @param string $cc - The E-Mail Address that you want to remove
  299.      */
  300.     public function removeCc($cc) {
  301.         $this->setCc(self::removeFromArray($cc, $this->getCc()));
  302.     }
  303.  
  304.     /**
  305.      * Adds an E-Mail Address to BCC
  306.      *
  307.      * @param string $bcc - The E-Mail Address that you want to add
  308.      */
  309.     public function addBcc($bcc) {
  310.         self::checkValidEMail($bcc);
  311.  
  312.         $this->bcc[] = $bcc;
  313.     }
  314.  
  315.     /**
  316.      * Removes an E-Mail Address from the BCC array
  317.      *
  318.      * @param string $bcc - The E-Mail Address that you want to remove
  319.      */
  320.     public function removeBcc($bcc) {
  321.         $this->setBcc(self::removeFromArray($bcc, $this->getBcc()));
  322.     }
  323.  
  324.     /**
  325.      * Send the Mail as HTML-Mail
  326.      *
  327.      * @return bool - true on success | false on error
  328.      */
  329.     public function sendHTML() {
  330.         if(! $this->getSender() || ! $this->getTo() || ! $this->getSubject() || ! $this->getMsg())
  331.             return false;
  332.  
  333.         // Header
  334.         $header = 'MIME-Version: 1.0' . PHP_EOL;
  335.         $header .= 'Content-type: text/html; charset=' . $this->getCharset() . PHP_EOL;
  336.         $header .= 'From: ' . $this->getSender() . PHP_EOL;
  337.         if($this->getCc())
  338.             $header .= 'Cc: ' . $this->getCcList() . PHP_EOL;
  339.         if($this->getBcc())
  340.             $header .= 'Bcc: ' . $this->getBccList() . PHP_EOL;
  341.         if($this->getReplyTo())
  342.             $header .= 'Reply-To: ' . $this->getReplyTo() . PHP_EOL;
  343.         $header .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
  344.  
  345.         $this->makeLineBreaksHTML();
  346.  
  347.         return mail($this->getToList(), $this->getSubject(), $this->getMsg(), $header);
  348.     }
  349.  
  350.     /**
  351.      * Send the Mail
  352.      *
  353.      * @return bool - true on success | false on error
  354.      */
  355.     public function send() {
  356.         if(! $this->getSender() || ! $this->getTo() || ! $this->getSubject() || ! $this->getMsg())
  357.             return false;
  358.  
  359.         // Header
  360.         $header = 'Content-type: text/plain; charset=' . $this->getCharset() . PHP_EOL;
  361.         $header .= 'From: ' . $this->getSender() . PHP_EOL;
  362.         if($this->getCc())
  363.             $header .= 'Cc: ' . $this->getCcList() . PHP_EOL;
  364.         if($this->getBcc())
  365.             $header .= 'Bcc: ' . $this->getBccList() . PHP_EOL;
  366.         if($this->getReplyTo())
  367.             $header .= 'Reply-To: ' . $this->getReplyTo() . PHP_EOL;
  368.         $header .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
  369.  
  370.         $this->makeLineBreaks();
  371.  
  372.         return mail($this->getToList(), $this->getSubject(), $this->getMsg(), $header);
  373.     }
  374.  
  375.     /**
  376.      * Replaces Line-Breaks with HTML Line-Breaks
  377.      */
  378.     private function makeLineBreaksHTML() {
  379.         $this->setMsg(str_replace(array(PHP_EOL, '\r\n'), '<br />', $this->getMsg()));
  380.  
  381.         if($this->getMaxLineLength())
  382.             $this->setMsg(wordwrap($this->getMsg(), $this->getMaxLineLength(), '<br />' . PHP_EOL));
  383.     }
  384.  
  385.     /**
  386.      * Replaces HTML-Line-Breaks with normal Line-Breaks
  387.      */
  388.     private function makeLineBreaks() {
  389.         if($this->getMaxLineLength())
  390.             $this->setMsg(wordwrap($this->getMsg(), $this->getMaxLineLength(), PHP_EOL));
  391.  
  392.         $this->setMsg(str_replace('<br />', PHP_EOL, str_replace('<br>', PHP_EOL, $this->getMsg())));
  393.     }
  394.  
  395.     /**
  396.      * Creates an String of the E-Mail-Array
  397.      *
  398.      * @param array $emailArray - Array with E-Mail Addresses that you want to convert into a String
  399.      * @return string - E-Mail-String
  400.      */
  401.     private function createEMailList($emailArray) {
  402.         return implode(', ', $emailArray);
  403.     }
  404.  
  405.     /**
  406.      * Removes a value from an array
  407.      *
  408.      * @param mixed $value - The value that you want to remove from the array
  409.      * @param array $array - The array from that the value should be removed
  410.      * @return array - The new array without the searched value
  411.      */
  412.     private static function removeFromArray($value, $array) {
  413.         $tmpNew = array();
  414.         $i = 0;
  415.  
  416.         // Process for each array item. Construct new array without the searched value
  417.         foreach($array as $itemValue) {
  418.             if($value != $itemValue) {
  419.                 $tmpNew[$i] = $itemValue;
  420.                 $i++;
  421.             }
  422.         }
  423.  
  424.         return $tmpNew;
  425.     }
  426.  
  427.     /**
  428.      * Check if the E-Mail Address is valid
  429.      *
  430.      * @param string $email - E-Mail Address to check
  431.      */
  432.     private static function checkValidEMail($email) {
  433.         try {
  434.             // Check E-Mail pattern
  435.             $atPos = mb_strpos($email, '@');
  436.             $lastPointPos = mb_strrpos($email, '.');
  437.  
  438.             if(! $atPos || ! $lastPointPos)
  439.                 throw new Exception('E-Mail-Address "' . $email . '" is invalid!');
  440.  
  441.             if(! ($atPos > 0 && $lastPointPos > ($atPos + 1) && mb_strlen($email) > ($lastPointPos + 1)))
  442.                 throw new Exception('E-Mail-Address "' . $email . '" is invalid!');
  443.         } catch(Exception $e) {
  444.             echo $e->getMessage();
  445.         }
  446.     }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment