@@ -15,7 +15,7 @@ final class Mailer
1515
1616 const MAILER_VERSION = '3.0.0-beta ' ;
1717
18- const DEFAULT_CHANNEL = 'default ' ;
18+ const CHANNEL_DEFAULT = 'default ' ;
1919
2020 const TRANSPORT_SENDMAIL = 'sendmail ' ;
2121 const TRANSPORT_SMTP = 'smtp ' ;
@@ -34,7 +34,7 @@ final class Mailer
3434 */
3535 public function __construct ($ transport = self ::TRANSPORT_SENDMAIL , $ options = array ())
3636 {
37- $ this ->setChannel (self ::DEFAULT_CHANNEL , $ transport , $ options );
37+ $ this ->setChannel (self ::CHANNEL_DEFAULT , $ transport , $ options );
3838 }
3939
4040 /**
@@ -65,7 +65,8 @@ public function setChannel($name, $transport, $options = array())
6565 break ;
6666 default :
6767 $ opt = array_key_exists ('options ' , $ options )?(string )$ options ['options ' ]:'' ;
68- $ connect = new Sendmail ($ opt );
68+ $ sender = array_key_exists ('sender ' , $ options )?(string )$ options ['sender ' ]:'' ;
69+ $ connect = new Sendmail ($ sender , $ opt );
6970 }
7071 $ this ->channels [$ name ] = $ connect ;
7172 }
@@ -77,34 +78,49 @@ public function setChannel($name, $transport, $options = array())
7778 public function removeChannel ($ name )
7879 {
7980 $ name = (string )$ name ;
80- if ($ name == self ::DEFAULT_CHANNEL ) {
81+ if ($ name == self ::CHANNEL_DEFAULT ) {
8182 throw new ChannelCantBeRemovedException ($ name );
8283 }
8384 unset($ this ->channels [$ name ]);
8485 }
8586
8687 /**
8788 * @param Message $message
88- * @param Book $addresses
89- * @param bool $personal
89+ * @param string[] $to
9090 * @param string $channel
9191 * @throws ChannelNotExistsException
9292 */
93- public function send (Message $ message , Book $ addresses , $ personal = false , $ channel = self ::DEFAULT_CHANNEL )
93+ public function send (Message $ message , $ to , $ channel = self ::CHANNEL_DEFAULT )
94+ {
95+ foreach ($ to as $ address ) {
96+ $ message
97+ ->removeHeader ('To ' )
98+ ->removeHeader ('CC ' )
99+ ->removeHeader ('BCC ' )
100+ ;
101+ $ this ->mass ($ message , array ($ address ), array (), array (), $ channel );
102+ }
103+ }
104+
105+ /**
106+ * @param Message $message
107+ * @param string[] $to
108+ * @param string[] $cc
109+ * @param string[] $bcc
110+ * @param string $channel
111+ * @throws ChannelNotExistsException
112+ */
113+ public function mass (Message $ message , $ to , $ cc = array (), $ bcc = array (), $ channel = self ::CHANNEL_DEFAULT )
94114 {
95- $ message ->setHeader ('X-Mailer ' , 'ddrv/mailer- ' .self ::MAILER_VERSION .' (https://github.com/ddrv/mailer) ' );
96115 $ channel = (string )$ channel ;
97116 if (!array_key_exists ($ channel , $ this ->channels )) {
98117 throw new ChannelNotExistsException ($ channel );
99118 }
100- if ($ personal ) {
101- foreach ($ addresses as $ address ) {
102- $ recipient = new Book ();
103- $ recipient ->add ($ address );
104- $ this ->channels [$ channel ]->send ($ message , $ recipient );
105- }
106- } else {
107- $ this ->channels [$ channel ]->send ($ message , $ addresses );
108- }
119+ $ message ->setHeader ('X-Mailer ' , 'ddrv/mailer- ' .self ::MAILER_VERSION .' (https://github.com/ddrv/mailer) ' );
120+ $ message ->setHeader ('To ' , implode (', ' , $ to ));
121+ $ message ->setHeader ('CC ' , implode (', ' , $ cc ));
122+ $ message ->setHeader ('BCC ' , implode (', ' , $ bcc ));
123+ $ addresses = array_unique (array_merge ($ to , $ cc , $ bcc ));
124+ $ this ->channels [$ channel ]->send ($ message , $ addresses );
109125 }
110126}
0 commit comments