function sendemail($toname, $toemail, $fromname, $fromemail, $subject, $message, $type = "plain", $cc = "", $bcc = "") { global $settings, $locale; require_once INCLUDES . "class.phpmailer.php"; $mail = new PHPMailer(); if (file_exists(INCLUDES . "language/phpmailer.lang-" . $locale['phpmailer'] . ".php")) { $mail->SetLanguage($locale['phpmailer'], INCLUDES . "language/"); } else { $mail->SetLanguage("en", INCLUDES . "language/"); } if (!$settings['smtp_host']) { $mail->IsMAIL(); } else { $mail->IsSMTP(); $mail->Host = $settings['smtp_host']; $mail->Port = $settings['smtp_port']; $mail->SMTPAuth = $settings['smtp_auth'] ? true : false; $mail->Username = $settings['smtp_username']; $mail->Password = $settings['smtp_password']; } $mail->CharSet = $locale['charset']; $mail->From = $fromemail; $mail->FromName = $fromname; $mail->AddAddress($toemail, $toname); $mail->AddReplyTo($fromemail, $fromname); if ($cc) { $cc = explode(", ", $cc); foreach ($cc as $ccaddress) { $mail->AddCC($ccaddress); } } if ($bcc) { $bcc = explode(", ", $bcc); foreach ($bcc as $bccaddress) { $mail->AddBCC($bccaddress); } } if ($type == "plain") { $mail->IsHTML(false); } else { $mail->IsHTML(true); } $mail->Subject = $subject; $mail->Body = $message; if (!$mail->Send()) { $mail->ErrorInfo; $mail->ClearAllRecipients(); $mail->ClearReplyTos(); return false; } else { $mail->ClearAllRecipients(); $mail->ClearReplyTos(); return true; } }
/** * Send an email using a Transactional email service * or native PHP as a fallback. * * @param array $attributes A list of attributes for sending * @return bool on success */ public static function send($attributes = array()) { /* |-------------------------------------------------------------------------- | Required attributes |-------------------------------------------------------------------------- | | We first need to ensure we have the minimum fields necessary to send | an email. | */ $required = array_intersect_key($attributes, array_flip(self::$required)); if (count($required) >= 3) { /* |-------------------------------------------------------------------------- | Load handler from config |-------------------------------------------------------------------------- | | We check the passed data for a mailer + key first, and then fall back | to the global Statamic config. | */ $email_handler = array_get($attributes, 'email_handler', Config::get('email_handler', null)); $email_handler_key = array_get($attributes, 'email_handler_key', Config::get('email_handler_key', null)); if (in_array($email_handler, self::$email_handlers) && $email_handler_key) { /* |-------------------------------------------------------------------------- | Initialize Stampie |-------------------------------------------------------------------------- | | Stampie provides numerous adapters for popular email handlers, such as | Mandrill, Postmark, and SendGrid. Each is written as an abstract | interface in an Adapter Pattern. | */ $mailer = self::initializeEmailHandler($email_handler, $email_handler_key); /* |-------------------------------------------------------------------------- | Initialize Message class |-------------------------------------------------------------------------- | | The message class is an implementation of the Stampie MessageInterface | */ $email = new Message($attributes['to']); /* |-------------------------------------------------------------------------- | Set email attributes |-------------------------------------------------------------------------- | | I hardly think this requires much explanation. | */ $email->setFrom($attributes['from']); $email->setSubject($attributes['subject']); if (isset($attributes['text'])) { $email->setText($attributes['text']); } if (isset($attributes['html'])) { $email->setHtml($attributes['html']); } if (isset($attributes['cc'])) { $email->setCc($attributes['cc']); } if (isset($attributes['bcc'])) { $email->setBcc($attributes['bcc']); } if (isset($attributes['headers'])) { $email->setHeaders($attributes['headers']); } $mailer->send($email); return true; } else { /* |-------------------------------------------------------------------------- | Native PHP Mail |-------------------------------------------------------------------------- | | We're utilizing the popular PHPMailer class to handle the messy | email headers and do-dads. Emailing from PHP in general isn't the best | idea known to man, so this is really a lackluster fallback. | */ $email = new PHPMailer(true); $email->IsMAIL(); $email->CharSet = 'UTF-8'; $email->AddAddress($attributes['to']); $email->From = $attributes['from']; $email->FromName = $attributes['from']; $email->Subject = $attributes['subject']; if (isset($attributes['text'])) { $email->AltBody = $attributes['text']; } if (isset($attributes['html'])) { $email->Body = $attributes['html']; $email->IsHTML(true); } if (isset($attributes['cc'])) { $email->AddCC($attributes['cc']); } if (isset($attributes['bcc'])) { $email->AddBCC($attributes['bcc']); } if ($email->Send()) { return true; } } } return false; }