/** * Send an email using an SMTP server * * @throws EmailGatewayException * @throws EmailValidationException * @throws Exception * @return boolean */ public function send() { $this->validate(); $settings = array(); $settings['helo_hostname'] = $this->_helo_hostname; if ($this->_auth) { $settings['username'] = $this->_user; $settings['password'] = $this->_pass; } $settings['secure'] = $this->_secure; try { if (!is_a($this->_SMTP, 'SMTP')) { $this->_SMTP = new SMTP($this->_host, $this->_port, $settings); } // Encode recipient names (but not any numeric array indexes) $recipients = array(); foreach ($this->_recipients as $name => $email) { // Support Bcc header if (isset($this->_header_fields['Bcc']) && $this->_header_fields['Bcc'] == $email) { continue; } // if the key is not numeric, qEncode the key. $name = General::intval($name) > -1 ? General::intval($name) : EmailHelper::qEncode($name); $recipients[$name] = $email; } // Combine keys and values into a recipient list (name <email>, name <email>). $recipient_list = EmailHelper::arrayToList($recipients); // Encode the subject $subject = EmailHelper::qEncode((string) $this->_subject); // Build the 'From' header field body $from = empty($this->_sender_name) ? $this->_sender_email_address : EmailHelper::qEncode($this->_sender_name) . ' <' . $this->_sender_email_address . '>'; // Build the 'Reply-To' header field body if (!empty($this->_reply_to_email_address)) { $reply_to = empty($this->_reply_to_name) ? $this->_reply_to_email_address : EmailHelper::qEncode($this->_reply_to_name) . ' <' . $this->_reply_to_email_address . '>'; } if (!empty($reply_to)) { $this->_header_fields = array_merge($this->_header_fields, array('Reply-To' => $reply_to)); } // Build the body text using attachments, html-text and plain-text. $this->prepareMessageBody(); // Build the header fields $this->_header_fields = array_merge($this->_header_fields, array('Message-ID' => sprintf('<%s@%s>', md5(uniqid()), HTTP_HOST), 'Date' => date('r'), 'From' => $from, 'Subject' => $subject, 'To' => $recipient_list, 'X-Mailer' => 'Symphony Email Module', 'MIME-Version' => '1.0')); // Set header fields and fold header field bodies foreach ($this->_header_fields as $name => $body) { $this->_SMTP->setHeader($name, EmailHelper::fold($body)); } // Send the email command. If the envelope from variable is set, use that for the MAIL command. This improves bounce handling. $this->_SMTP->sendMail(is_null($this->_envelope_from) ? $this->_sender_email_address : $this->_envelope_from, $this->_recipients, $this->_body); if ($this->_keepalive === false) { $this->closeConnection(); } $this->reset(); } catch (SMTPException $e) { throw new EmailGatewayException($e->getMessage()); } return true; }
/** * Send an email using the PHP mail() function * * Please note that 'encoded-words' should be used according to * RFC2047. Basically this means that the subject should be * encoded if necessary, as well as (real) names in 'From', 'To' * or 'Reply-To' header field bodies. For details see RFC2047. * * The parts of a message body should be encoded (quoted-printable * or base64) to make non-US-ASCII text work with the widest range * of email transports and clients. * * @throws EmailGatewayException * @throws EmailValidationException * @return bool */ public function send() { $this->validate(); try { // Encode recipient names (but not any numeric array indexes) $recipients = array(); foreach ($this->_recipients as $name => $email) { // Support Bcc header if (isset($this->_header_fields['Bcc']) && $this->_header_fields['Bcc'] == $email) { continue; } // if the key is not numeric, qEncode the key. $name = General::intval($name) > -1 ? General::intval($name) : EmailHelper::qEncode($name); $recipients[$name] = $email; } // Combine keys and values into a recipient list (name <email>, name <email>). $recipient_list = EmailHelper::arrayToList($recipients); // Encode the subject $subject = EmailHelper::qEncode((string) $this->_subject); // Build the 'From' header field body $from = empty($this->_sender_name) ? $this->_sender_email_address : EmailHelper::qEncode($this->_sender_name) . ' <' . $this->_sender_email_address . '>'; // Build the 'Reply-To' header field body if (!empty($this->_reply_to_email_address)) { $reply_to = empty($this->_reply_to_name) ? $this->_reply_to_email_address : EmailHelper::qEncode($this->_reply_to_name) . ' <' . $this->_reply_to_email_address . '>'; } if (!empty($reply_to)) { $this->_header_fields = array_merge($this->_header_fields, array('Reply-To' => $reply_to)); } // Build the message from the attachments, the html-text and the plain-text. $this->prepareMessageBody(); // Build the header fields $this->_header_fields = array_merge($this->_header_fields, array('Message-ID' => sprintf('<%s@%s>', md5(uniqid()), HTTP_HOST), 'Date' => date('r'), 'From' => $from, 'X-Mailer' => 'Symphony Email Module', 'MIME-Version' => '1.0')); // Format header fields $header_fields = array(); foreach ($this->_header_fields as $name => $body) { $header_fields[] = sprintf('%s: %s', $name, $body); } /** * Make things nice for mail(). * - Replace CRLF in the message body by LF as required by mail(). * - Implode the header fields as required by mail(). */ $this->_body = str_replace("\r\n", "\n", $this->_body); $header_fields = implode("\r\n", $header_fields); // Send the email mail($recipient_list, $subject, $this->_body, $header_fields, "-f{$this->_sender_email_address}"); } catch (Exception $e) { throw new EmailGatewayException($e->getMessage()); } return true; }
/** * Send an email using an SMTP server * * @return bool */ public function send() { $this->validate(); $settings = array(); if ($this->_auth == true) { $settings['username'] = $this->_user; $settings['password'] = $this->_pass; } $settings['secure'] = $this->_secure; try { $this->_SMTP = new SMTP($this->_host, $this->_port, $settings); // Encode recipient names (but not any numeric array indexes) foreach ($this->_recipients as $name => $email) { $name = is_numeric($name) ? $name : EmailHelper::qEncode($name); $recipients[$name] = $email; } // Combine keys and values into a recipient list (name <email>, name <email>). $recipient_list = EmailHelper::arrayToList($recipients); // Encode the subject $this->_subject = EmailHelper::qEncode($this->_subject); // Encode the sender name if it's not empty $this->_sender_name = empty($this->_sender_name) ? NULL : EmailHelper::qEncode($this->_sender_name); // Build the 'From' header field body $from = empty($this->_sender_name) ? $this->_sender_email_address : $this->_sender_name . ' <' . $this->_sender_email_address . '>'; // Build the 'Reply-To' header field body if (!empty($this->_reply_to_email_address)) { if (!empty($this->_reply_to_name)) { $reply_to = EmailHelper::qEncode($this->_reply_to_name) . ' <' . $this->_reply_to_email_address . '>'; } else { $reply_to = $this->_reply_to_email_address; } } if (!empty($reply_to)) { $this->_header_fields = array_merge(array('Reply-To' => $reply_to), $this->_header_fields); } // Build the body text using attachments, html-text and plain-text. $this->prepareMessageBody(); // Build the header fields $this->_header_fields = array_merge(array('Message-ID' => sprintf('<%s@%s>', md5(uniqid()), HTTP_HOST), 'Date' => date('r'), 'From' => $from, 'Subject' => $this->_subject, 'To' => $recipient_list, 'X-Mailer' => 'Symphony Email Module', 'MIME-Version' => '1.0'), $this->_header_fields); // Set header fields and fold header field bodies foreach ($this->_header_fields as $name => $body) { $this->_SMTP->setHeader($name, EmailHelper::fold($body)); } // Send the email $this->_SMTP->sendMail($this->_sender_email_address, $this->_recipients, $this->_subject, $this->_body); } catch (SMTPException $e) { throw new EmailGatewayException($e->getMessage()); } return true; }