PHP - Imap Character Encoding
I am terribly confused... I am trying to display the html body of a message pulled through imap.
I thought that if I did this: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> in the html head, that everything would be pretty much figured out for me, but... When I look at the original message in gmail (prior to being pulled with imap), I get an ellipses(...), but once I display it in my browser, I get a black diamond question mark thingy... Help! I've tried all manners of conversion, but suspect I'm completely missing the boat here. Thanks Similar TutorialsHi, does anybody know why I get a question mark inside a diamond shape where a pound sign should be when i retrieve data from a mysql database? I have tried using latin1_general_ci and utf8_general_ci and they both do the same, don't know whether that is anything to do with it or not? Any help would be greatly appreciated. So I've recently started looking into Character Encoding, after never really bothering with it, and I've got a few questions. After checking out my site it seems I've been using UTF-8 without really realising it, and I'm going to stick with UTF-8. However, when I jump into phpMyAdmin to check out my MySQL tables I run into a few problems. Originally, for my database, the collation was set to latin1_swedish_ci. I have since changed this to utf8_general_ci. This however only affects newly created tables, not the 150 or so tables I currently have which are still latin1_swedish_ci. Do I need to convert the collation of these already existing tables to UTF-8, and if so what's the best method? This takes me to my text editor, NetBeans 7. Do I need to alter anything here, I've not been successful with finding information relating to this? Cheers guys. Hi A really basic question, with regards to best practice, but I can't find an answer anywhere. Should the charset in my website be set to match the charset used by my database? E.g. Database is utf-8, so website should be set to utf-8? I submit text to my MySQL database using a form on my site. When I look at the text in my database, sometimes, there are strange characters. The strange characters are caused by quotation marks, em dashes, apostrophes and foreign letters of the alphabet. I think that this only happens when the source of the text is a Windows program. I understand that this is a character encoding issue, but I don't fully understand the subject. I've spent the last few hours researching it, but it's only confused me.
My site uses UTF-8 encoding:
<meta http-equiv="content-type" content="text/xml; charset=utf-8" />The collation of my database is utf8_general_ci. My form looks like this: <form action="" method="post"> </form>As you can see, an accept-charset="utf-8" attribute has not been specified. Questions 1) I am guessing that my problem is that the Windows characters are being misinterpreted by my UTF-8 setup. Is that correct? 2) If so, is there a way that I can safely convert the Windows characters to UTF-8 during the submission process? 3) Should I also specify an accept-charset="utf-8" attribute on the form? 4) When I paste the Windows text directly into my database without using the form, the characters save without turning into the strange characters. But they don't render properly on my site. Can't browsers identify Windows characters? How do u work with .jpg file attachments using the IMAP mail functions... i already know how to read the body text from an e-mail with IMAP but I haven't worked with attachments and don't know where to start... i basically want to get the .jpg attachment and save it to a folder on my server. The imap code below displays the date like this: Sat, 18 Feb 2012 19:22:02 +0100 I want the date to be displayed like this instead: 2012-02-18 Any idea how I can achieve this? Thanks in advance Code: [Select] <?php date_default_timezone_set ("Europe/Stockholm"); /* connect to mail */ $hostname = '{xxx.xxxx.com:143/notls}INBOX'; $username = 'xxxxx@xxxxx.com'; $password = 'xxxxxx'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Anslutning till mail misslyckades: ' . imap_last_error()); /* grab emails */ $emails = imap_search($inbox,'ALL'); /* if emails are returned, cycle through each... */ if($emails) { /* begin output var */ $output = ''; /* put the newest emails on top */ rsort($emails); /* for every email... */ foreach($emails as $email_number) { /* get information specific to this email */ $overview = imap_fetch_overview($inbox,$email_number,0); $message = imap_fetchbody($inbox,$email_number,2); $date = date('d m Y'); $swedate = $overview[0]->date; /* output the email information */ $output.= '<li>'; $output.= '<a href="#"><strong>'.$swedate.'</strong> '; $output.= ''.$overview[0]->subject.'<br> '; $output.= '<small> Från: '.$overview[0]->from.'</small></a>'; $output.= '</li>'; } echo $output; } /* close the connection */ imap_close($inbox); ?> I'm using imap_open and when I use imap_headerinfo and output the info, I want to be able to have an array of words to match the fromaddress to.... case-insensitive. I tried ton of different ways... array_search array_filter stripos stristr And tried adding strtolower in a few Tried setting the object as a var and comparing it that way... but it either chokes on the object or the array... Or ignores it completely For example setting an array like this: Code: [Select] $blacklist_array = array("wl_partners@cupid.com", "Replica Shop", "CJ Adams", "eharmony", "Dr.Maxman", "Credit Check"); And the fromaddress has this in it: Credit Check <check@iamaspammer.com> I can't get it to work to match the Credit Check only part. I got it to work if I do a foreach through the messages and then a foreach through the array then process a good or bad result, but it takes too long if there are a ton of messages to process... The code is a mess right now with all the trial and errors. Any ideas? I'm kinda brain dead at this point and have been up way to long... Thanks... Hey guys, Been developing a bespoke "helpdesk" system for work and, while it works well on the whole, I am having major problems with base64 encoded email bodies. Admittedly, this might be due to my misunderstanding of imap body parts. It seems in my current setup, the system thinks that all mails are either "type" 0 or 4. As a result, base64 emails don't get decoded and display as an unreadable long string. Here is what my code for the body decoding looks like just now - can provide more on request. $from=$overview[0]->from; $subject=$overview[0]->subject; $subdb=mysql_real_escape_string($subject); $lookingfor=explode("~#",$body); $result=$from; $find=strrpos($ir, $lookingfor[1]); $findreplystream=mysql_query("SELECT * FROM tickets WHERE ticket_md5='$lookingfor[1]'") or die(''.mysql_error().''); $stream=mysql_fetch_array($findreplystream); $now=time(); if($structure->encoding == 0){ $arr1=array("=A3", "=80"); $arr2=array("£", "€"); $newcontent=$explodeunder[0]; define("CHARSET", "ISO-8859-1"); $dbcontenta=str_replace($arr1, $arr2, $newcontent); $r=mb_detect_encoding($dbcontenta); if($r=="UTF-8"){ $dbcontents=mb_convert_encoding($dbcontenta, "ISO-8859-1", "UTF-8"); $dbcontent1=addslashes($dbcontents); $dbcontent2=strip_tags($dbcontent1); } elseif($r=="ASCII"){ $dbcontentb=mb_convert_encoding($dbcontenta, "UTF-8", "ASCII"); $dbcontents=mb_convert_encoding($dbcontentb, "ISO-8859-1", "UTF-8"); $dbcontent1=addslashes($dbcontents); $dbcontentf=strip_tags($dbcontent1); $dbcontent2=quoted_printable_decode($dbcontentf); } } elseif($structure->encoding == 4) { $dbcontent=$explodeunder[0]; $dbcontentf=quoted_printable_decode($dbcontent); $dbcontent2=mb_convert_encoding($dbcontentf, "ISO-8859-1", "UTF-8"); } elseif($structure->encoding == 3) { $dbcontent2=imap_base64($explodeunder[0]); } elseif($structure->encoding == 1){ $dbcontent3=imap_8bit($explodeunder[0]); $dbcontent2=quoted_printable_decode($dbcontent3); } Can anyone advise me with this? I'd be greatful for any assistance at all, as it's causing me nightmares! All the best I've got a snippet of code that accesses an email account using the the imap function. In my class file I can use 'sender' => $headerArr->sender[0]->mailbox . "@" . $headerArr->sender[0]->host, and it gives me the senders email address. However when I try to use 'reply_to' => $headerArr->reply_to[0]->mailbox. "@" .$headers->reply_to[0]->host, it does not give me the reply to address (which can be different when receiving e-mails from groups, forums, etc). Any help would be appreciated. Jess I want to read an imap message with all the headers, and body, what would be the best way to do that? Hello, Here's little script that should get all email's subject via IMAP: Code: [Select] <?php $imap = imap_open("{imap.gmail.com:993/imap/ssl}", "user@gmail.com", "password"); $message_count = imap_num_msg($imap); for ($i = 1; $i <= $message_count; ++$i) { $header = imap_fetchheader($imap,$i,0); $first=strstr($header, "Subject: "); $second=strstr($header, "From: "); $fc=strlen($first); $sc=strlen($second); $le=$fc-$sc-9; $subject=substr($first,8,$le); echo $subject."\n"; } imap_close($imap); ?> Well it works just fine on inbox with few emails, else, if you have lots of mail with attachments, it will give 30 sec timeout warning. How the the would i bypass downloading the whole message+body, but just a header (subject). Thank's in advance! Howdy. I'm trying to get UTF8 Encoded text out of an email from an imap server. I can read the mail, but the encoding seems to be jacked up. Code: [Select] Hi I'm trying to get Japanese text out of an email from an imap server. PHP is somehow mangling it though. for ( $i = 1; $i <= $mails; $i++ ) { $struct = imap_fetchstructure( $imap, $i ); // $struct->encoding is 0 // The text is UTF8 $body1 = imap_utf8( imap_body( $imap, $i ) ); $body2 = imap_body( $imap, $i ); } In the above code, $body1 and $body2 are the same, and not any Japanese encoding. They're just garbage. Any ideas? My server is fastmail if that helps. Hello Everyone... I'm having an issue with my looping and parsing of email via imap... Here is my code: Code: [Select] <? require "connection.inc"; $mailbox = imap_open("{serverinformation}INBOX", "email@emailaddress.com", "thepassword"); if ($mailbox == false) { echo "<p>Error: Can't open mailbox!</p>"; echo imap_last_error(); } else { $num = imap_num_msg($mailbox); for ($i=0; $i < $num; $i++) { $content = imap_body($mailbox, $num); $siteid = stripos($content,"MOU"); $gwon = stripos($content, "MOSLXX-"); $TempSiteID = substr($content, $siteid, 7); $TempGraniteWorksOrderNumber = substr($content, $gwon, 13); echo '<p>Message #: '.$i.' - Site ID: '.$TempSiteID.' - Granite Works Order #: '.$TempGraniteWorksOrderNumber.'</p>'; } } imap_close($mailbox); ?> The result I get is unexpected... The output gives me each mail message number as expected, however, the information that I parsed for is always that of the last email message... Here's an example of the output: Message #: 0 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 1 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 2 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 3 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 4 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 5 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Message #: 6 - Site ID: MOU3963 - Granite Works Order #: MOSLXX-247119 Anybody tell me what I'm doing wrong here? Hi all, I am working on PHP to create a email client using imap. I have got a problem with using imap because it will get very slow so I want to find a way to make it to go faster. I have been researching that I have seen alot of people are using mysql database to store their emails and get access to their emails pretty quick than using imap. And I have also seen that roundcube are using mysql database to store contacts, emails...etc. I am not really sure what to do but I need your advice and I need your opinion. I have got questions for you. Do you think if I am better off to use imap or use mysql database to get access to my emails pretty quicker? If you think I am better off to use mysql database, then I want to know how I could store my emails in mysql and do I need to delete the emails in imap after I store the emails in mysql? Please let me know your opinion so I will decide what to do next. Thank you. Edited September 15, 2019 by mark107How would I go about creating a new user email account that can be accessed via my cpanel mail scripts (RoundCube, SquirrelMail, etc.), in PHP. I seem to be having a difficult time saving the attachments to a folder on the server, all the examples I see show it to the browser, what I need to do is copy the attachment to a separate folder and do some processing on it. So far everything I get, then ftp to my desktop appears corrupt. It works I just can't seem to unzip the zipped attachments, any ideas? My Code: (Not mine, I used examples from the web and put it into a function) function get_attachments($message_number){ global $connection; $structure = imap_fetchstructure($connection, $message_number); $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($connection, $message_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']); } } } } return $attachments; } Calling Code: $a = get_attachments(6); $fp = fopen($a[1]['filename'], 'w'); fwrite($fp, $a); fclose($fp); The junk I've found on phpclasses is well junk and not to mention really old. I started writing my own, but I frankly don't like the plumbing involved. Anyone have a great IMAP class? Something that just does the work for you? I have spent nearly all day scouring Google for help on this and after trying numerous scripts unsuccessfully and reading up on imap functions but still getting nowhere as well as frying my brain l have had to accept defeat unless anyone of the many gifted coders here can help me out with a script they might have written and used. many thanks Callum |