Saturday, December 30, 2017

PHP imap_fetchbody can't get HTML a tag link url

Leave a Comment

I'm building a email read script, it read emails via imap, when i try to read email body using imap_fetchbody it only show texts, but email body have one link it seems not show,

This is my code

    $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); } } 

The email contents is like below

 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. 

When i read the email from mail client text here has a link, but when read that using php it only shows a text,

Email screen shot here enter image description here someone know what is the reason for this issue or is there need to change some parameters for get the link with url. Thank You

1 Answers

Answers 1

You have to check the multiple sections of the email. You can actually read the source code of an email. It is quite easy to follow and you will identify the different sections.

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 

The section you mention on the comments it is a PNG file. Some sections can be encoded in base64 as the email it is a text based protocol.

https://www.base64decode.org/

Example of an email:

 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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment