【发布时间】:2019-09-27 15:44:55
【问题描述】:
我正在从 gmail 帐户检索 XML ADF 格式的电子邮件。我正在使用 imap_body 来检索电子邮件。查看电子邮件帐户,我可以看到电子邮件是通过两种方式接收的。第一个开始是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<?adf version="1.0"?>
<adf>
<prospect status="new">
<id sequence="1" source=...
第二种开头是这样的:
<?xml version="1.0" encoding="UTF-8"?><?adf version="1.0"?><adf><prospect status="new"><id sequence="1" source=...
对于第二种类型,imap_body 返回固定为 75 个字符。第 75 个字符作为等号 (=) 插入。此外,在正常的等号生成之后添加了 3D 字符:
<?xml version=3D"1.0" encoding=3D"UTF-8"?><?adf version=3D"1.0"?><adf><pros=
pect status=3D"new"><id sequence=3D"1" source=3D...
我可以对第 75 个字符使用循环来更正字符串,但如果第 74 个字符是等号,则在未插入的等号之前插入等号和一个空格。
例如。
<name part=3D"full" type=
=3D"business"
这会引发计数。我虽然也想对此进行补偿,但如果可能的话,我想在这里尝试修复实际的 imap_body 请求。
//Retrieve emails, make corrections if needed, convert to array
$inbox = imap_open($cred['host'],$cred['user'],$cred['pass']);
if(!$inbox){
$output = ['success' => false,'error' => "Unable to connect to host. ".imap_last_error()];
}else{
/* Get email list */
$emails = imap_num_msg($inbox);
/* Cycle through each email based on count */
if($emails > 0){
for($key = 1;$key <= $emails;$key++) {
/* Retrieve the email body */
$pull = imap_body($inbox,$key);
$init = str_replace(["\r","\n"],'',stripslashes($pull));
//Clears the breaks having the = character and clears the added characters 3D
if(substr($init,14,2) == "3D" && substr($init,75,1) == "="){
$max = strlen($init);
$cnt = 75;//First = character occurance
while($cnt < $max){
$init = substr($init,0,$cnt).substr($init,$cnt + 1);//remove =
$cnt += 75;//Set for next iteration
}
$init = str_replace("3D",'',$init);//Remove additional
}
$data = simplexml_load_string($init);
$xml[$key] = json_decode(json_encode($data),true);
}
}
}
第二种类型的电子邮件需要进行更正,但由于电子邮件中途存在双重等号/空格问题,将被关闭。由于这些问题,simplexml_load_string 失败。同样,从 imap_body 更正将是理想的,否则我将继续进行更正,希望不会出现更多类型的问题。
【问题讨论】:
-
您要查找的关键字是 'Quoted Printable' 解码:php.net/quoted_printable_decode
-
@Max - 是的,成功了。谢谢!
标签: php simplexml imap gmail-imap