PHP imap_fetchbody 无法获取 HTML a 标签链接 url

标签 php email imap

我正在构建一个电子邮件阅读脚本,它通过 imap 读取电子邮件,当我尝试使用 imap_fetchbody 阅读电子邮件正文时它只显示文本,但电子邮件正文有一个链接似乎没有显示,

这是我的代码

    $hostname = '{mail.test.com/notls}INBOX';
    $username = 'info@test.com';
    $password = 'testopw';

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Email Server: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox,'SEEN FROM "noreply@test.com"');
if($emails) {

    /* put the newest emails on top */
    rsort($emails);
    /* for every email... */
    foreach($emails as $email_number) {
        /* get information specific to this email */                
       echo $message = imap_fetchbody($inbox,$email_number,1);
}
}

邮件内容如下

 We do our utmost to arrive to you on time. There may, however, 
be unexpected circumstances, causing our driver to be delayed. 
You can follow the current status of your assignment here.

当我从邮件客户端文本这里阅读电子邮件时有一个链接,但是当使用 php 阅读时它只显示一个文本,

电子邮件屏幕截图在这里 enter image description here 有人知道这个问题的原因是什么,或者是否需要更改一些参数以获取与 url 的链接。谢谢你

最佳答案

您必须检查电子邮件的多个部分。您实际上可以阅读电子邮件的源代码。这很容易理解,您将识别不同的部分。

imap_fetchbody($inbox,$email_number,$section);

http://php.net/manual/en/function.imap-fetchbody.php

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext

您在评论中提到的部分是 PNG 文件。一些部分可以在 base64 中编码为电子邮件,它是一种基于文本的协议(protocol)。

https://www.base64decode.org/

电子邮件示例:

 From: Nathaniel Borenstein <nsb@bellcore.com> 
 To:  Ned Freed <ned@innosoft.com> 
 Subject: Sample message 
 MIME-Version: 1.0 
 Content-type: multipart/mixed; boundary="simple 
 boundary" 

 This is the preamble.  It is to be ignored, though it 
 is a handy place for mail composers to include an 
 explanatory note to non-MIME compliant readers. 
 --simple boundary 

 This is implicitly typed plain ASCII text. 
 It does NOT end with a linebreak. 
 --simple boundary 
 Content-type: text/plain; charset=us-ascii 

 This is explicitly typed plain ASCII text. 
 It DOES end with a linebreak. 

 --simple boundary-- 
 This is the epilogue.  It is also to be ignored.

https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

关于PHP imap_fetchbody 无法获取 HTML a 标签链接 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901204/

相关文章:

c# - asp.net 格式化电子邮件

java - IMAP 电子邮件 channel 适配器引发异常 "A5 BAD invalid command or parameters;"

java - 当 IMAP 文件夹关闭时,JavaMail API 如何删除邮件

php - PDOException SQLSTATE[HY000] [2002] 本地计算机上的连接超时

php - 如何选择表中多个值的最大年份?

html - 电子邮件模板 Outlook

go - emersion/go-imap - 如何检索和列出看不见的消息

php - 如何用 Twig 渲染字符串/数据库中的内容?

php - 构建和搜索 GPS 坐标的 SQL 数据库/表

php - 如何使用 phpmailer 跟踪通过 smtp 发送的电子邮件?