/** * Save the email log history * * @param integer $customerId * @param array|string $fromname * @param string $recipient * @param string $subject * @param array|string $cc * @param array|string $bcc * @param string $htmlbody * @param string $textbody * @param string $date */ public static function saveIt($customerId, $from, $recipient, $subject, $cc, $bcc, $htmlbody, $textbody, $date = null) { $EmailsTemplatesSends = new EmailsTemplatesSends(); if (is_numeric($customerId)) { $EmailsTemplatesSends->customer_id = $customerId; $EmailsTemplatesSends->fromname = is_array($from) && isset($from['name']) ? $from['name'] : ''; $EmailsTemplatesSends->fromemail = is_array($from) && isset($from['email']) ? $from['email'] : $from; $EmailsTemplatesSends->subject = $subject; $EmailsTemplatesSends->recipient = $recipient; $EmailsTemplatesSends->cc = is_array($cc) ? trim(implode(',', $cc), ',') : $cc; $EmailsTemplatesSends->bcc = is_array($bcc) ? trim(implode(',', $bcc), ',') : $bcc; $EmailsTemplatesSends->html = $htmlbody; $EmailsTemplatesSends->text = $textbody; $EmailsTemplatesSends->date = !empty($date) ? $date : date('Y-m-d H:i:s'); if ($EmailsTemplatesSends->trySave()) { return true; } } return false; }
/** * Show the content of the email */ public function emailviewAction() { $this->getHelper('layout')->setLayout('blank'); $id = $this->getRequest()->getParam('id'); if (is_numeric($id)) { $email = EmailsTemplatesSends::getById($id); $this->view->email = $email; } return $this->render('emailpreview'); }
/** * SendEmail * Smtp Configuration. * If you would like to use the smtp authentication, you have to add * the parameters in the Setting Module of the Control Panel * * @param string $from * @param string or array $to * @param string $bcc * @param string $subject * @param string $body * @param string $html * @param string $inreplyto * @param string/array $attachments * @return boolean|multitype:unknown NULL */ public static function SendEmail($from, $to, $bcc = NULL, $subject, $body, $html = false, $inreplyto = NULL, $attachments = NULL, $replyto = NULL, $cc = null) { $transport = null; $config = array(); $host = Settings::findbyParam('smtp_host'); if (!empty($host)) { $username = Settings::findbyParam('smtp_user'); $password = Settings::findbyParam('smtp_password'); $port = Settings::findbyParam('smtp_port'); $port = !empty($port) ? $port : 25; if (!empty($username) && !empty($password)) { $config = array('auth' => 'login', 'username' => $username, 'password' => $password, 'port' => $port); } $transport = new Zend_Mail_Transport_Smtp($host, $config); } $mail = new Zend_Mail('UTF-8'); $mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64); if (!empty($attachments)) { if (is_array($attachments)) { foreach ($attachments as $attachment) { if (file_exists($attachment)) { $filename = basename($attachment); // Get the content of the file $content = file_get_contents($attachment); // Create the attachment $zend_attachment = new Zend_Mime_Part($content); $zend_attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $zend_attachment->encoding = Zend_Mime::ENCODING_BASE64; $zend_attachment->filename = $filename; $mail->addAttachment($zend_attachment); } } } else { if (file_exists($attachments)) { $filename = basename($attachments); // Get the content of the file $content = file_get_contents($attachments); // Create the attachment $zend_attachment = new Zend_Mime_Part($content); $zend_attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $zend_attachment->encoding = Zend_Mime::ENCODING_BASE64; $zend_attachment->filename = $filename; $mail->addAttachment($zend_attachment); } } } if (!empty($inreplyto)) { $mail->addHeader("In-Reply-To", $inreplyto); } if (!empty($replyto)) { $mail->setReplyTo($replyto); } // If the body of the message contains the HTML tags // we have to override the $html variable in order to send the html message by email if (self::isHtml($body)) { $html = true; } if ($html) { $mail->setBodyHtml($body, null, Zend_Mime::ENCODING_8BIT); } else { $mail->setBodyText($body); } if (is_array($from)) { $mail->setFrom($from['email'], $from['name']); } else { $mail->setFrom($from); } // If the $to is a group of emails addresses if (is_array($to)) { foreach ($to as $recipient) { $mail->addTo($recipient); } } else { $mail->addTo($to); } if (!empty($bcc)) { if (is_array($bcc) && count($bcc) > 0) { foreach ($bcc as $b) { $mail->addBcc($b); } } else { $mail->addBcc($bcc); } } if (!empty($cc)) { if (is_array($cc) && count($cc) > 0) { foreach ($cc as $c) { $mail->addCc($c); } } else { $mail->addCc($cc); } } $mail->setSubject($subject); try { $mail->send($transport); // All good, log to DB if (is_array($to)) { foreach ($to as $recipient) { // get customer_id $Customers = Customers::findbyemail($recipient); if (is_object($Customers) && is_object($Customers->{0}) && isset($Customers->{0}->customer_id)) { $customerId = $Customers->{0}->customer_id; } if (EmailsTemplatesSends::saveIt($customerId, $from, $recipient, $subject, $cc, $bcc, $html, $body)) { Shineisp_Commons_Utilities::log("An email has been sent to {$recipient}", 'notice.log'); // log the data } } } else { // get customer_id $Customers = Customers::findbyemail($to); if (is_object($Customers) && is_object($Customers->{0}) && isset($Customers->{0}->customer_id)) { $customerId = $Customers->{0}->customer_id; } if (EmailsTemplatesSends::saveIt($customerId, $from, $to, $subject, $cc, $bcc, $html, $body)) { Shineisp_Commons_Utilities::log("An email has been sent to {$to}", 'notice.log'); // log the data } } return true; } catch (Exception $e) { // log the data Shineisp_Commons_Utilities::log($e->getMessage()); return array('email' => $to, 'message' => $e->getMessage()); } return false; }