note 119479 added to function.mail

From: Date: Thu, 16 Jun 2016 20:10:20 +0000
Subject: note 119479 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-205736@lists.php.net to get a copy of this message
A simple class for sending email <?php class Email { private $to = array(); private $cc = array(); private $bCc = array(); private $from = null; private $subject = null; private $body = null; private $contentType = 'html'; public $charSet = 'UTF-8'; public function isPlain() { $this->contentType= 'plain'; } public function __construct() { ; } public function setFrom($email, $name = null) { if ($name !== null) { $stFrom = trim($email) . ' <' . trim($email) . '>'; } else { $stFrom = $email; } $this->from = $stFrom; } public function setSubject($subject) { $this->subject = trim($subject); } public function setBody($body) { $this->body = $body; } private function addAddress($email, $destType, $name = null) { if ($name !== null) { $stTo = trim($name) . ' <' . trim($email) . '>'; } else { $stTo = $email; } $this->{$destType}[] = $stTo; } public function addTo($email, $name = null) { $this->addAddress($email, 'to', $name); } public function addCC($email, $name = null) { $this->addAddress($email, 'cc', $name); } public function addBCC($email, $name = null) { $this->addAddress($email, 'bCc', $name); } public function send() { $stErros = ''; if ($this->from === null) { $stErros .= '<li>Informe o remetente da mensagem.</li>'; } if (count($this->to) === 0) { $stErros .= '<li>Informe ao menos um destinatário.</li>'; } if ($this->subject === null) { $stErros .= '<li>Informe o assunto da mensagem.</li>'; } if ($this->body === null) { $stErros .= '<li>Informe o texto da mensagem.</li>'; } if ($stErros !== '') { throw new Exception('Email erro(s): <ul>' . $stErros . '</ul>'); } $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-type: text/{$this->contentType}; charset={$this->charSet}"; $headers[] = "From: {$this->from}"; if (count($this->cc) > 0) { foreach ($this->cc as $bCc) { $headers[] = 'Cc: ' . $bCc; } } if (count($this->bCc) > 0) { foreach ($this->bCc as $bCc) { $headers[] = 'Bcc: ' . $bCc; } } $stTo = implode(", ", $this->to); $stHeaders = implode("\r\n", $headers); if ($this->contentType === 'html') { $body = '<html><head><title></title><meta http-equiv=Content-Type content=text/html; charset=UTF-8>'; $body .= '</head><body><table width="800" border="0"><tr><td><p align="justify" style="color:#000000;">'; $body .= nl2br($this->body); $body .= '</p></td></tr></table></body></html>'; } else { $body = $this->body; } $boSend = @mail($stTo, $this->subject, $body, $stHeaders); if (!$boSend) { throw new Exception('Email fail'); } } public function clearAllRecipients() { $this->to = array(); $this->cc = array(); $this->bCc = array(); } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 200.198.198.76 ---- Manual Page -- http://php.net/manual/en/function.mail.php Edit -- https://master.php.net/note/edit/119479 Del: integrated -- https://master.php.net/note/delete/119479/integrated Del: useless -- https://master.php.net/note/delete/119479/useless Del: bad code -- https://master.php.net/note/delete/119479/bad+code Del: spam -- https://master.php.net/note/delete/119479/spam Del: non-english -- https://master.php.net/note/delete/119479/non-english Del: in docs -- https://master.php.net/note/delete/119479/in+docs Del: other reasons-- https://master.php.net/note/delete/119479 Reject -- https://master.php.net/note/reject/119479 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205736) next »