Pipe email to PHP, alter "To:", then send message
I am trying to create a PHP email forwarding script that does the following:
Input: Takes the email from a pipe. (this part I have working)
Process A: Grab a series of recipient email addresses from a database (this part I have working)
Process B: (done for each email address in the series above): Alter the email by changing the "To:" field to one of the email addresses in the series, without messing up the integrity of the message, including any attachments. (this part is getting very messy)
Output: Once the email has been edited to send it to my designated recipient(s), send the message to those recipients.
I understand that the normal PHP mail function isn't this flexible, unless I dissect the whole email into the individual parts and re-assemble them, which I have done, but this messes up some of the emails.
I have tried pulling the message apart using Mail_mimeDecode, and can easily alter the "To:" line as desired.
The problem: Once I have altered the mimeDecode results to include the desired email recipient, how do I reassemble the email and send it without losing any data?
Seems to me, I'd use the IMAP functions to read the mail box, copy the body contents of the e-mail as a whole, parse the headers into an array, change the To: field, and then reassemble the message(s) anew. This cuts out the need to even decode the mime parts.
There is no "mail box" to read with IMAP. The message is created from a PIPE which feeds the email directly to my PHP script. I then want to parse the email, replace the piped email address with the correct recipient address(es), and forward on the message.
I have also now tried to parse the incoming emails with PHP's "mailparse" group of functions, and I can get the recipient email address(es) inserted into the resultant arrays, but I haven't figured out how to re-assemble the email from the parsed arrays, let alone how to send the re-created email.
Maybe a little more explanation would help.
I have a very large family. We (meaning I) maintain a password-protected website the family keeps up-to-date contact information including email addresses.
On the current site, I am using Perl to accept the input of an email pipe (email@example.com), and change the "To:" address to each member of the family (one at a time), and re-sending the email, so each family member receives a copy of the email.
So the script receives one email sent to "firstname.lastname@example.org", and changes the "email@example.com" to "firstname.lastname@example.org" and sends it to that person, and repeats the process for each family member.
So far in PHP, when I capture the pipe input, look through it and edit the "To:" field as desired, then send the email, it goes to the desired person, but then the entire email (headers, html & plain text, attachments and all) are sent through as the message body.
Is there not a function in PHP that takes a preformatted email (headers, attachments and all) and lets me send it without wrapping its own set of headers around it?