Re: Mail, array of recipients
| From: | DG | Date: | Fri, 01 Oct 2004 20:27:07 +0000 |
| Subject: | Re: Mail, array of recipients | ||
| References: | 1 2 3 4 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-14723@lists.php.net to get a copy of this message | ||
Justin Patrin wrote:
On Fri, 01 Oct 2004 15:34:14 +0200, DG <listsdg@yahoo.fr> wrote:Sure, the second one, maybe, but that was the point. Use the first one.Justin Patrin wrote:This won't make the job !! This foreach will send count($recipients) but always with the same "To" ! Once $myHdrs have been set withOn Fri, 6 Aug 2004 18:08:30 -0400, Sean Brown <sean@sean-brown.com> wrote: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); }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 onerecipient."; } Has anyone run into this? Is there a fix?
$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) {where are you setting $headers?$mime->_headers['To'] = $recipient;//a setTo() method would bebetter
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); } Thanks for the hint. But I don't want that each recipient can know others... I suppose the "to" field in the in received emails will presents all recipients with your implode method ? Is there any disadvantage in making a method setTo($to){$this->_headers['To'] = $to;} in mail_mime ?$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);}
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: