Forum Moderators: coopster

Message Too Old, No Replies

IMAP Forward Mail Problem

         

FiRe

5:43 pm on Jul 5, 2006 (gmt 0)

10+ Year Member



I have this function:
function mailfrom($fromaddress, $toaddress, $headers) {
$fp = popen("/usr/sbin/sendmail -f$fromaddress $toaddress", "w");
if ($fp) {
fputs($fp, $headers);
fputs($fp, "\n.\n");
pclose($fp);
}
}

I am trying to use it to forward on any emails received to another address. Here is how I forward it:
$full = imap_fetchheader($conn, $i);
$full .= imap_body($conn, $i);
mailfrom($from, $to, $full);

However I just get the contents of $full in the body of the email when I receive it. Any help?

IanKelley

10:45 pm on Jul 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The PHP IMAP situation is a bit more complex than your example. I've found that what works on one server will not work on another so you have to play around a bit to get it to work.

As a result I can't help with any examples, just keep trialing and erroring :-)

FiRe

10:43 am on Jul 6, 2006 (gmt 0)

10+ Year Member



am I on the right track though?

IanKelley

11:14 am on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah you seem to be on the right track, but it's hard to say with the snippet you posted.

The imap_open is the stickiest bit. Have you verified you're getting a connection?

Or have you isolated the problem to the mailfrom function? If so, why are you putting $fromaddress and $toaddress in the sendmail call line?

FiRe

12:16 pm on Jul 6, 2006 (gmt 0)

10+ Year Member



It does work because you see the contents of $full in the email it sends to.

If you call sendmail directly, and pipe the entire email to it, it should take all the headers and attachments.
/usr/lib/sendmail -fFromAddress@domain.com Destination@domain.com

[webmasterworld.com...]

IanKelley

11:02 pm on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahhh... I somehow misread the bit about the contents of $full :-)

So what that appears to mean is that sendmail is assuming you're done with headers when you give it the from and to addresses in the call line.

What I would do is assign the header and body of your original email to different variables and then run the header through a loop to replace the TO and FROM addresses with your new ones.

Once that change is made you should be able to just dump everything to sendmail (with no addys in the call line this time) and it should work.

FiRe

8:29 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Ok well I tried this:


$full = imap_fetchheader($conn, $i);
$full = str_replace($to, $real_email, $full);
$full = str_replace($from, "postmaster@site.com", $full);
$full .= imap_body($conn, $i);

mailfrom($full);

And then this function:

function mailfrom($headers) {
$fp = popen("/usr/sbin/sendmail", "w");
if ($fp) {
fputs($fp, $headers);
fputs($fp, "\n.\n");
pclose($fp);
}
}

But I havent received the email so one or the other is wrong :-P

IanKelley

10:08 pm on Jul 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What are the values of the $to and $from variables? Probably they're blank, in which case the script isn't going to replace anything. Instead you'll basically just get the original email resent.

What you want to do is run through the headers line by line (foreach loop on an array created by exploding on line breaks). If the line contains From:, replace that line with your new From: email@addy.com. Also if the line contains To:.

Then implode it back into a string, tack on the body, and give it to sendmail. You may also want to add -t to the sendmail call.

FiRe

10:54 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



$from = $mailHeader->fromaddress;
$to = $mailHeader->toaddress;

So the str_replace should work. Also I dont want to change the $from field, it has to appear from the original sender.

So what would be the final function?

Thanks for your help so far :-)

IanKelley

12:08 am on Jul 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm, yes it should work. Stick a print statement in your code to make sure that the $to variable is being populated.

Sounds like you can drop everything related to From since you're not changing it. Makes sure to put -t after the sendmail call.

FiRe

10:32 am on Jul 8, 2006 (gmt 0)

10+ Year Member



Ok I receive the email but still the contents of $full. Here is the function:
function mailfrom($headers) {
$fp = popen("/usr/sbin/sendmail -t", "w");
if ($fp) {
fputs($fp, $headers);
pclose($fp);
}
}

And the code is:
$full = imap_fetchheader($conn, $i);
$full = str_replace($mailHeader->toaddress, $real_email, $full);
$full .= imap_body($conn, $i);
mailfrom($full);

IanKelley

11:28 am on Jul 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It sounds like sendmail is parsing until it finds a valid To address but is not interpreting that there is a header section.

Try putting the following right before:

$full .= imap_body($conn, $i);

$full .= "\r\n\r\n";

FiRe

1:54 pm on Jul 8, 2006 (gmt 0)

10+ Year Member



Same problem :-( the sendmail seems to work because I receive the email at the new address so its defiently reading the headers...

IanKelley

1:34 am on Jul 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just tried a test of this script and it works so I'm not sure what else to tell you, sorry.

You might try running it without the To address replacement to see if that's where the problem is.

FiRe

11:04 am on Jul 9, 2006 (gmt 0)

10+ Year Member



It might be because of all the extra headers, like "Scanned-By" and stuff?!

IanKelley

10:29 pm on Jul 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Probably not, I tested with a few different emails, some of which had all the extra garbage headers.

It could be some quirk about the way your server/sendmail is setup. One thing you could try is using the PHP mail function (which is actually a sendmail wrapper) instead of using a system call to sendmail. It will accept headers.

FiRe

8:05 am on Jul 10, 2006 (gmt 0)

10+ Year Member



Yeh I tried this:

$full = imap_fetchheader($conn, $i);
$full = str_replace($mailHeader->toaddress, $real_email, $full);
$body = imap_body($conn, $i);

mail($to, $mailHeader->subject, $body, $full);


And I get the exact same problem, so maybe it is my server :-S

[edited by: FiRe at 8:09 am (utc) on July 10, 2006]

FiRe

8:13 am on Jul 10, 2006 (gmt 0)

10+ Year Member



now this is odd, you can see the headers and stuff if sent to a google mail account, but in hotmail it works fine! obviously 1 system filters it out and 1 doesnt. hrmmm any ideas?