PHP电子邮件转发

时间:2013-05-16 18:50:02

标签: email imap php email-forwarding

我正在尝试编写一个php电子邮件转发脚本。 我写了一个简单的代码,从imap读取电子邮件并将它们发送到特定的电子邮件地址。

但问题是它无法运行:)它发送的电子邮件没有电子邮件正文。

/* connect to imap */
$hostname = '{mail.myserver.com:143/notls}INBOX';
$username = 'email@myserver.com';
$password = 'password';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) 
{


/* put the newest emails on top */
rsort($emails);

/* for every email... */
foreach($emails as $email_number) {

/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);


$full = imap_fetchheader($inbox, $email_number);
$body = imap_body($inbox, $email_number);


imap_mail("targetaddress@hotmail.com","title", $body,$full); 

break;
}
} 

/* close the connection */
imap_close($inbox);

1 个答案:

答案 0 :(得分:0)

试试这个..

虽然有效,但不会转发任何附件。

          $fp = popen('/usr/sbin/sendmail -f'.$fromaddress.' '.$toaddress,"w"); 
                    $subject = 'Title';
                    $msgNo = imap_msgno($conn, $msgUID);
                    $header = imap_header($conn, $msgNo);
                    if($body = imap_fetchbody($conn, $msgNo, 1.1)) {    } 
                    else { $body = imap_fetchbody($openmail, $msgNo, 1); }
                    if(!$fp) return false; 
                    fputs($fp, "To: $toaddress\n"); 
                    fputs($fp, "Subject: $subject\n"); 
                    fputs($fp, $headers."\n\n"); 
                    fputs($fp, $body); 
                    fputs($fp, "\n"); 
                    pclose($fp);