Permalink
Browse files

Adding support for multiple to, cc, and bcc

  • Loading branch information...
1 parent cd18fba commit 5962f1e9c551ee04dfc93196924c4ee1e15b7f58 @reines reines committed Apr 18, 2011
Showing with 51 additions and 45 deletions.
  1. +26 −20 email.php
  2. +1 −1 mailer.php
  3. +12 −6 transports/mail.php
  4. +12 −18 transports/smtp.php
View
@@ -25,10 +25,12 @@ public static function sanitize_header($value)
private $message;
private $headers;
+ private $from;
private $mailer;
public function __construct($from, $mailer)
{
+ $this->from = $from;
$this->mailer = $mailer;
$this->message = '';
@@ -37,21 +39,19 @@ public function __construct($from, $mailer)
'Content-type' => 'text/plain; charset=utf-8',
'X-Mailer' => self::MAILER_TAG,
);
-
- $this->headers['From'] = $from;
}
public function set_reply_to($reply_to)
{
- $this->headers['Reply-To'] = $reply_to;
+ $this->headers['Reply-To'] = self::encode_utf8($reply_to);
// Allow chaining
return $this;
}
public function set_subject($subject)
{
- $this->headers['Subject'] = $subject;
+ $this->headers['Subject'] = self::encode_utf8($subject);
// Allow chaining
return $this;
@@ -65,7 +65,7 @@ public function set_body($message)
return $this;
}
- public function get_message()
+ private function get_message()
{
// Change \n and \r linefeeds into \r\n
$message = preg_replace(array('%(?<!\r)\n%', '%\r(?!\n)%'), "\r\n", $this->message);
@@ -79,31 +79,37 @@ public function get_message()
return $message;
}
- public function get_headers()
+ private function get_headers()
{
- $headers = array_merge($this->headers, array(
+ return array_merge($this->headers, array(
'Date' => gmdate('r'),
));
+ }
+
+ public function send($to, $cc = array(), $bcc = array())
+ {
+ // Make to, cc, and bcc into arrays if they aren't already
+ if (!is_array($to)) $to = array($to);
+ if (!is_array($cc)) $cc = array($cc);
+ if (!is_array($bcc)) $bcc = array($bcc);
- // Encode the subject as UTF8 if required
- if (!empty($headers['Subject']))
- $headers['Subject'] = self::encode_utf8($headers['Subject']);
+ // Create a list of all recipients
+ $recipients = array_merge($to, $cc, $bcc);
- // Encode the reply-to as UTF8 if required
- if (!empty($headers['Reply-To']))
- $headers['Reply-To'] = self::encode_utf8($headers['Reply-To']);
+ $message = $this->get_message();
+ $headers = $this->get_headers();
// Encode the from as UTF8 if required
- $headers['From'] = self::encode_utf8($headers['From']);
+ $headers['From'] = self::encode_utf8($this->from);
+
+ // Add the to, cc, and bcc headers - don't encode them since they must be a plain email
+ $headers['To'] = implode(', ', $to);
+ $headers['Cc'] = implode(', ', $cc);
+ $headers['Bcc'] = implode(', ', $bcc);
// Sanitize the headers (values only, keys are assumed to be legitimate!)
$headers = array_map(array('Email', 'sanitize_header'), $headers);
- return $headers;
- }
-
- public function send($to)
- {
- return $this->mailer->send($this, $to);
+ return $this->mailer->send($this->from, $recipients, $message, $headers);
}
}
View
@@ -42,5 +42,5 @@ public function new_email($subject = null, $message = null)
return $email;
}
- public abstract function send($email, $to);
+ public abstract function send($from, $recipients, $message, $headers);
}
View
@@ -17,20 +17,26 @@ public function __construct($config)
{
}
- public function send($email, $to)
+ public function send($from, $recipients, $message, $headers)
{
- $message = $email->get_message();
- $headers = $email->get_headers();
+ // $recipients is ignored - we use the contents of the to, cc, and bcc headers instead
// Extract the subject since PHP mail() wants it explicitly
- if (empty($headers['Subject']))
- $subject = '';
- else
+ $subject = '';
+ if (isset($headers['Subject']))
{
$subject = $headers['Subject'];
unset ($headers['Subject']);
}
+ // Extract the to header since mail() adds this itself
+ $to = null;
+ if (isset($headers['To']))
+ {
+ $to = $headers['To'];
+ unset ($headers['To']);
+ }
+
// Start with a blank message
$header_str = '';
View
@@ -192,30 +192,24 @@ private function authPlain($username, $password)
return $this->connection->read_response();
}
- public function send($email, $to)
+ public function send($from, $recipients, $message, $headers)
{
- $message = $email->get_message();
- $headers = $email->get_headers();
-
- // TODO: Sanitize $to
-
- // Extract the from since SMTP wants it explicitly
- $from = $headers['From'];
-
- // TODO: Handle UTF8-decoding? TODO: Sanitize?
-
- // Add the to header
- $headers['To'] = Email::encode_utf8($to);
-
$this->connection->write('MAIL FROM: <'.$from.'>');
$result = $this->connection->read_response();
if ($result['code'] != SMTPConnection::OKAY)
throw new Exception('Invalid response to mail attempt: '.$result['code']);
- $this->connection->write('RCPT TO: <'.$to.'>');
- $result = $this->connection->read_response();
- if ($result['code'] != 250 && $result['code'] != 251)
- throw new Exception('Invalid response to mail attempt: '.$result['code']);
+ // Add all the recipients
+ foreach ($recipients as $recipient)
+ {
+ $this->connection->write('RCPT TO: <'.$recipient.'>');
+ $result = $this->connection->read_response();
+ if ($result['code'] != 250 && $result['code'] != 251)
+ throw new Exception('Invalid response to mail attempt: '.$result['code']);
+ }
+
+ // If we have a Bcc header, unset it so that it isn't sent!
+ unset ($headers['Bcc']);
// Start with a blank message
$data = '';

0 comments on commit 5962f1e

Please sign in to comment.