Re: Mail, array of recipients

From: Date: Fri, 01 Oct 2004 17:46:59 +0000
Subject: Re: Mail, array of recipients
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-14717@lists.php.net to get a copy of this message
On Fri, 01 Oct 2004 15:34:14 +0200, DG <listsdg@yahoo.fr> wrote: > Justin Patrin wrote: > > >On Fri, 6 Aug 2004 18:08:30 -0400, Sean Brown <sean@sean-brown.com> wrote: > > > > > >>In the pear manual examples for the send method, it shows: > >> > >>send (mixed $recipients, string $headers, string $body); > >> > >>When I send out an e-mail from an app, it works, however, it does not seem > >>to properly populate the "To" header based on the recipient array. What I > >>mean by that is if I examine the headers after having received the email, > >>the "To" header is completely missing. If I manually set the "To" > >>header, > >>it works. However, as you can imagine, unless my recipient array has only > >>one person, this method wouldn't work. I'd have to loop through each > >>individual in the recipient array, create a new object, then call the method > >>again and again. > >> > >>Here's a snip from my code: > >> > >>$recipients = > >>array('someone@example.com','someoneelse@example.com'); > >> > >>$smtparray = array(); > >>$smtparray["host"] = 'smtp.example.com'; > >>$smtparray["auth"] = FALSE; > >> > >>$crlf = "\r\n"; > >>$from = "me@example.com (Web Master)"; > >>$hdrs = array(); > >>$hdrs["From"] = $from; > >>$hdrs["Subject"] = $subject; > >>$hdrs["Reply-To"] = $from_address; > >> > >>$mime = new Mail_mime($crlf); > >>$mime->setTXTBody($plaintextmessage); > >>$mime->setHTMLBody($htmlmessage); > >>$body = $mime->get(); > >>$hdrs = $mime->headers($hdrs); > >>$mailObj =& Mail::factory('smtp',$smtparray); > >> > >>if (isset($recipients) && ($recipients)) { > >> $result = $mailObj->send($recipients, $hdrs, $body); > >> if (PEAR::isError($result)) { > >> return $result -> getMessage(); > >> } else { > >> return "Success."; > >> } > >>} else { > >> return "Your e-mail has NOT been sent.<br>Please select at least one > >>recipient."; > >>} > >> > >>Has anyone run into this? Is there a fix? > >> > >> > >> > > > >I would say this is expected bahavior. If you want all of them in the > >To use this: > > > >$hdrs['To'] = implode(', ', $recipients); > > > >$mail->send($recipients, $hdrs, $body); > > > >If you want each to have their own To: > > > >foreach($recipients as $recipient) { > > $myHdrs = $hdrs; > > $myHdrs['To'] = $recipient; > > $mail->send($recipient, $myHdrs, $body); > >} > > > > > > > This won't make the job !! This foreach will send count($recipients) but > always with the same "To" ! > Once $myHdrs have been set with > Sure, the second one, maybe, but that was the point. Use the first one. > $myHdrs = $mime->headers($hdrs); > > You can no more change it. So set the to header with *all* of them right away. > > Once you have set the "To" header, it cannot be changed using the same > instance of mail_mime object. On line 553 the method headers() of > Mail_Mime override the $headers you want to set: > > $this->_headers = array_merge($headers, $this->_headers); The To header is informational only, it has no bearing on who the e-mail goes to. > > So you have to make a new instance of mail_mime for each mail ! And to > call again and again $body = $mail_mime_object->get(); > > This can easily kill your server. > > I think there is a lack of a setTo() public method (setting _headers > private property) as suggested in pear_dev mailing list > (http://marc.theaimsgroup.com/?l=pear-dev&m=107486411117568&w=2). Maybe > I didn't understood the correct usage? I use this workaround: > $mime->_headers['To'] = $headers['To']; > > my code = > > $mime = &new Mail_mime("\r\n"); > $mime->setTXTBody($message); > $mime->setHTMLBody($html_message); > $body = $mime->get(); //85% of script time execution. impossible to put > in the foreach > foreach ($recipients as $recipient) { > $mime->_headers['To'] = $recipient;//a setTo() method would be > better where are you setting $headers? > $hdrs = $mime->headers($headers); //without above workaround, > cannot override internal _headers after first call > $mailObj =& Mail::factory($driver,$options); > $result = $mailObj->send($recipient, $hdrs, $body); > } Here, this will do exactly what you want: $mime = &new Mail_mime("\r\n"); $mime->setTXTBody($message); $mime->setHTMLBody($html_message); $body = $mime->get(); $headers['To'] = implode(', ', $recipients); $hdrs = $mime->headers($headers); $mailObj =& Mail::factory($driver,$options); foreach ($recipients as $recipient) { $result = $mailObj->send($recipient, $hdrs, $body); } > > >I believe it's set up this way so that you can: > >1) Not have a To: > >2) Set everyone in the To: > >3) Set only one person in the To: > > > > > > -- paperCrane --Justin Patrin--

« previous php.pear.general (#14717) next »