2011-10-25 60 views
1

我试图将电子邮件的正文转换为字符串以进行一些处理,下面的脚本获取电子邮件,但无法将内容转换为字符串。有什么想法吗? 干杯, d在php中将电子邮件内容转换为字符串

/* 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) { 
/* begin output var */ 
$output = ''; 
/* 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);  
/* output the email header information */  
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
$output.= '<span class="subject">subject'.$overview[0]->subject.'</span> '; 
$output.= '<span class="from">'.$overview[0]->from.'</span>'; 
$output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
$output.= '</div>';  
/* output the email body */ 
$output.= '<div class="body">'.$message.'</div>'; 
$pos = strpos("FIND_THIS", $message); 
if ($pos !== false) { 
    print "found<br/>"; 
} 
else { 
    print " not found <br/>"; 
} 
} 
echo $output; 
} 
/* close the connection */ 
imap_close($inbox); 
+1

那么'$ output'是什么? *不是* $ message的内容?正如我对'imap_fetchbody'的理解一样,'$ message'包含了主体.. –

+0

输出是一切串联起来的,欢呼声。 – dale

回答

1

是否有特定的电子邮件中的 '部分2'?如果是明文电子邮件,则只有0(标题)和1(正文)。内容部分在这里详细说明:http://ca3.php.net/manual/en/function.imap-fetchbody.php#89002

+0

这就是纠正它应该是一个,试图各种让它工作。我弄糊涂的是发现字符串是否存在 - 有针 - >干草堆而不是干草堆 - >针($ pos = strpos(“FIND_THIS”,$ message)应该是$ pos = strpos($ message,“FIND_THIS” ))。学校男孩错误!欢呼家伙 – dale

相关问题