I'm working on some sort of application where I can get emails from my inbox via a cron job, where i just insert mail-from, mail-subject and mail-body into a MySQL database.
The problem i'm having is with the different mailtypes like HTML mails, plain text mails,..
Is there a way to get just the text? I've tried with fetchbody and HTML entitites, printable_decode but that results in sometimes an empty body insert or one with all special characters or sometimes perfect.
//$body = mysqli_real_escape_string($db, htmlentities(quoted_printable_decode(imap_fetchbody($stream,$email_id, 1.2)), ENT_QUOTES));
$body = mysqli_real_escape_string($db, quoted_printable_decode(imap_fetchbody($stream,$email_id, 1.2)));
//$body = quoted_printable_decode(imap_fetchbody($stream,$email_id,2));
$bodyText = imap_fetchbody($stream,$email_id,1.2);
if(!strlen($bodyText)>0){
$bodyText = "<html><body>".imap_fetchbody($stream,$email_id,1)."</body></html>";
}
Then i've tried with full html body, but that gives problems with inserting into my database and if I escape string, the HTML gets messy. I temporarly fixed it by getting the full HTML body and writing that to a .html file, but that's not really an option.
The reason why i store them into a MySQL database is so I can assign users, colors,.. to different mails (Which isn't an option with iMAP mails in Outlook for ex.)
My ultimate idea is to save the emails, just like it shows in outlook and then showing them exactly like that on a .php page, but it isn't always that easy.
Maybe i'm tackling this issue completely the wrong way, but maybe you guys have some tips, ideas or something?
Thanks in advance, Bert
If emails are sent properly there should be plain text and HTML but it is an ideal world.
So I suggest you to.
When showing to user
You can also use eml parser and save eml files in DB.
Check:
Wanted to add a possible alternative option here if you don't mind!
You could use Parserr to extract the body of the email and then use either Microsoft Flow or Zapier to push that data into your MySql database? It also then gives you options to extract the details of the email as you need (such as subject, cc etc).
Parserr can pass it in a form which is already escaped so that you don't need to worry about it's formatting when it hits your database. In addition it already has the option to add the plain text (automatically extracted for you) as well as a stripped formatting version which uses HTML markdown.