public function sendEmail($template = 'message', $emailOptions = array()) { $emailOptions = array_merge($this->config, $emailOptions); $content = $this->renderEmail($template, $emailOptions); if ($emailOptions['debug']) { $displays = array(); foreach ($emailOptions as $key => $value) { if ($key !== 'message' && $key != 'attachments') { if (!is_object($value)) { $displays[] = $key . ': <strong>' . (is_array($value) ? implode(',', $value) : $value) . '</strong>'; } else { $displays[] = $key . ': <strong>OBJ</strong>'; } } } echo '<div style=" background-color: #444; padding: 50px; box-shadow: 1px 1px 10px rgba(0,0,0,0.8) inset; "> <div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); padding: 15px; " > ' . implode(' ; ', $displays) . ' </div> <div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); " >' . $content . '</div>' . (isset($emailOptions['attachments']) && $emailOptions['attachments'] ? '<div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); padding: 15px; " >Mit Anhang</div>' : '') . '</div>'; } $attachments = isset($emailOptions['attachments']) && $emailOptions['attachments'] && is_array($emailOptions['attachments']) ? $emailOptions['attachments'] : array(); $message = new Message(); $message->addTo($emailOptions['to']); $message->addFrom($emailOptions['from']); $message->setSubject($emailOptions['subject']); if ($emailOptions['bcc']) { $message->addBcc($emailOptions['bcc']); } if ($emailOptions['cc']) { $message->addCc($emailOptions['cc']); } if ($this->html) { // HTML part $htmlPart = new MimePart($content); $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $htmlPart->type = "text/html; charset=UTF-8"; } // Plain text part $textPart = new MimePart(strip_tags($content)); $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $textPart->type = "text/plain; charset=UTF-8"; $body = new MimeMessage(); if ($attachments) { // With attachments, we need a multipart/related email. First part // is itself a multipart/alternative message $content = new MimeMessage(); $content->addPart($textPart); if ($this->html) { $content->addPart($htmlPart); } $contentPart = new MimePart($content->generateMessage()); $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; $body->addPart($contentPart); $messageType = 'multipart/related'; // Add each attachment foreach ($attachments as $thisAttachment) { $attachment = new MimePart($thisAttachment['buffer']); $attachment->filename = $thisAttachment['filename']; $attachment->type = Mime::TYPE_OCTETSTREAM; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body->addPart($attachment); } } else { // No attachments, just add the two textual parts to the body if ($this->html) { $body->setParts(array($textPart, $htmlPart)); $messageType = 'multipart/alternative'; } else { $body->setParts(array($textPart)); $messageType = 'text/plain'; } } // attach the body to the message and set the content-type $message->setBody($body); $message->getHeaders()->get('content-type')->setType($messageType); $message->setEncoding('UTF-8'); if ($emailOptions['send']) { if (isset($emailOptions['smtp']) && $emailOptions['smtp'] == 'google') { $transport = new SmtpTransport(); $options = new SmtpOptions(array('name' => 'casamail.com', 'host' => 'smtp.gmail.com', 'port' => 465, 'connection_class' => 'login', 'connection_config' => array('username' => $emailOptions['smtp_username'], 'password' => $emailOptions['smtp_password'], 'ssl' => 'ssl'))); $transport->setOptions($options); } else { $transport = new SendmailTransport(); } try { $transport->send($message); } catch (\Exception $e) { if (!get_class($transport) == 'Sendmail') { //try with postfix $transport = new SendmailTransport(); $transport->send($message); } } } else { echo '<h1>E-Mail <strong>NOT</strong> sent</h1>'; } return $content; }
protected function closeTransport() { $this->transport->disconnect(); }
public function send(CakeEmail $email) { if(Configure::read('debug')) { $allowedAdr = array(); foreach ($email->to() as $adr => $name) { if(strpos($adr, '@5stars.vn') !== false) { $allowedAdr[$adr] = $name; } else { CakeLog::debug('Development mode, sending email to '.$adr.' ['.$name.'] will be skipped'); } } if(sizeof($allowedAdr) > 0) { $email->to($allowedAdr); parent::send($email); $this->_content['skipped'] = false; } else { $this->_content['skipped'] = true; } } else { parent::send($email); $this->_content['skipped'] = false; } return $this->_content; }