php – 无法使用IMAP获取纯文本邮件内容
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 无法使用IMAP获取纯文本邮件内容,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2724字,纯文字阅读大概需要4分钟。
内容图文
![php – 无法使用IMAP获取纯文本邮件内容](/upload/InfoBanner/zyjiaocheng/799/06cbb367203f42f9b86d04abd3499a75.jpg)
我正在使用IMAP功能从特定邮件ID中读取邮件.但我无法读取纯文本邮件的邮件内容.它适用于HTML Mails.运行代码后,所有纯文本邮件仍然是未读的,html邮件标记为已读,其他内容如发件人邮件ID和主题我可以阅读.只有阅读内容的问题.这是我尝试过的代码
include('imap.php');
$hostname = '{xxx.org:143/novalidate-cert}INBOX';
$username = 'xxx-xxx@xx.org';
$password = 'xxxxx';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
$emails = imap_search($inbox,'UNSEEN');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$structure = imap_fetchstructure($inbox, $email_number);
$savedir = dirname(__FILE__).'/uploads/';
$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
$savepath = $savedir . $attachments[$i]['filename'];
file_put_contents($savepath, $attachments[$i]['attachment']);
}
}
}
$name = $structure->parts[1]->dparameters[0]->value;
$overview = imap_fetch_overview($inbox,$email_number,0);
$msg = imap_fetchbody($inbox,$email_number,1.2);
$message='';
if($msg=='')
{
$message = imap_fetchbody($inbox,$email_number,2.0);
}else{
$message=$msg;
}
$sub=$overview[0]->subject;
$from=$overview[0]->from;
$arr = explode('<', $from);
$from_mail = $arr[1];
if($from_mail!='')
{
$from=str_replace('>','',$from_mail);
}
$randstr='';
srand((double)microtime()*1000000);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
while(strlen($randstr)<5) {
$randstr.=substr($chars,(rand()%(strlen($chars))),1);
}
请任何人帮助我……提前致谢
解决方法:
你的问题是这一行:
$msg = imap_fetchbody($inbox,$email_number,1.2);
1.2是TEXT / HTML电子邮件部分,用于html电子邮件正文.
1.1是TEXT / PLAIN – 纯文本邮件正文 – 对于纯文本邮件,您需要使用此邮件.
内容总结
以上是互联网集市为您收集整理的php – 无法使用IMAP获取纯文本邮件内容全部内容,希望文章能够帮你解决php – 无法使用IMAP获取纯文本邮件内容所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。