Forum Moderators: coopster
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);
}
}
$full = imap_fetchheader($conn, $i);
$full .= imap_body($conn, $i);
mailfrom($from, $to, $full);
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?
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
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.
$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);
}
}
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.
function mailfrom($headers) {
$fp = popen("/usr/sbin/sendmail -t", "w");
if ($fp) {
fputs($fp, $headers);
pclose($fp);
}
}
$full = imap_fetchheader($conn, $i);
$full = str_replace($mailHeader->toaddress, $real_email, $full);
$full .= imap_body($conn, $i);
mailfrom($full);
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.
$full = imap_fetchheader($conn, $i);
$full = str_replace($mailHeader->toaddress, $real_email, $full);
$body = imap_body($conn, $i);mail($to, $mailHeader->subject, $body, $full);
[edited by: FiRe at 8:09 am (utc) on July 10, 2006]