-1

I've got an email message represented by a string, it already contains all headers, attachments, etc. What I need to do is to forward that email to another address and I don't want to touch that string at all, all contents of it must remain untouched. So, ideally it should look like this:

forward_email('forward_address@example.com', $original_message)

Code samples would be nice, since I'm not a PHP guy, just had to use it for scripting today.

UPDATE

I have to clarify, because maybe that's not what I need at all.

I have a postfix alias, which forwards all incoming mail to a pipe command, which is a php script. In that script I take all STDIN, which is email message, and make a POST request to a web-service, which parses the email message.

I need to forward that email to a backup mailbox in case of unsuccessful web-service call to be able to pull it from my app later. Any suggestions?

Max Al Farakh
  • 4,386
  • 4
  • 29
  • 56
  • 'do not alter that string (headers and all)' vs. 'send to other address' => drop 1 of your requirements, they conflict. – Wrikken Dec 11 '12 at 20:29
  • I've updated my question with some clarification. – Max Al Farakh Dec 11 '12 at 20:36
  • Take a look at this question and see if it helps you: http://stackoverflow.com/questions/4503980/forwarding-emails-with-a-php-script – Felix Guo Dec 11 '12 at 20:46
  • much,much,much easier to have a postfix alias to 2 emailboxes, one of them goes to pipe, one of them is the backup mailbox... let's not involve PHP in this unless we have to. – Wrikken Dec 11 '12 at 20:47
  • @Wrikken, yes, it would be way easier, but I need only non-imported emails to be in that mailbox, not every email. – Max Al Farakh Dec 11 '12 at 20:53
  • @MrXenotype saw that question. Doesn't really help, because the top answer requires me to disassemble a message and then assemble it again, that may lead to changes in the original message and to bugs, that will be a huge pain in the ass to debug. – Max Al Farakh Dec 11 '12 at 20:56
  • Hm, well, in that case, `imap_append` seems the most likely candidate as you have access to the mailbox the message should be in. – Wrikken Dec 11 '12 at 21:01

3 Answers3

2

Your question is not really about forwarding. It's about sending an email when you have the email-message encoded and ready.

Your question should sound like "send raw email from PHP". Here's a similar one:

Given an email as raw text, how can I send it using PHP?

Community
  • 1
  • 1
Serge Shultz
  • 5,888
  • 3
  • 27
  • 17
0

Solution if all emails should go to the backup inbox: create an alias for 2 addresses, one of them being the pipe, one of them the backupbox.

If you only need to store the mail on failure, one of the easier methods to store the email in an emailbox you have access to is imap_append, bypassing any actual delivery which might mess with headers.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
-1

So send it twice?

<?php
// Your regular mail logic here, concluded by something like...
mail('someone@example.com', 'Subject', $message, $headers);
// Then do it again, but to another address
mail('forward_address@example.com', 'Subject', $message, $headers);
?>

As long as you use the same variables for message/headers, they will be re-used for the 2nd recipient. Additionally you can add FW: in front of the subject if you want.

Oldskool
  • 34,211
  • 7
  • 53
  • 66