PHP - Imap Looping And Parsing
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? Similar TutorialsI'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 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 want to read an imap message with all the headers, and body, what would be the best way to do that? 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 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, 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! 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 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); 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. 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? Hi, I have a script that creates an email through cpanel, that part works. But i am getting error reading the email. Code: [Select] Warning: imap_open() [function.imap-open]: Couldn't open stream {mail.spamarchon.com:143} in /home/spamarch/public_html/index.php on line 71here is the code: Code: [Select] $mbox = imap_open("{mail.$edomain:143}", "$euser@$edomain", "$epass"); any idea Thanks I have a script that fetch emails and its work file when I call it through SSH with PHP at the beginning on the path when I added a cronjob it was working fine too... the problem start when I disable the cronjob email response, the script dose not functioning with Imap What can I do in order to bypass this problem, I will try to call it with include but I have no other options.. Please help I'm building a PHP application that's connect to gmail and download the messages. I have an account that contains more than two millions messages and the PHP cannot connect to that account. What can I do? The error is: Mailbox has more messages (2004032) exist than maximum (1000000) Hi guys, I have written a script that opens up my gmail messages via IMAP stores them in an array. However, I have thousands of emails, so this is taking forever. Is there a faster way of doing this? Here's my code Code: [Select] <?php //lets get those emails /* connect to gmail */ $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'info@***********.com'; $password = '******'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); /* grab emails */ $emails = imap_search($inbox,'ALL'); /* if emails are returned, cycle through each... */ if($emails) { /* for every email... */ foreach($emails as $email_number) { $message = imap_fetchbody($inbox,$email_number,2); } } /* close the connection */ imap_close($inbox); ?> 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 Hi All, I want to create a simple WEBMAIL application from scratch and I've already started with the help of IMAP functions. Listing & Viewing (of Inbox Messages) modules almost completed. Now I want to create an option for composing & sending emails. I've tried imap_mail() function, but there were some problems. So can anybody help me on this, if it there any available resources for it maybe classes or functions?? Thanks in advance Hello. After much searching I dont seem to be able to find anyone who has had this issue. I am writing a bounce handler. To get the bounced email of the original recipient I need to download the TEXT part of the bounced message. The original message contained INLINE Images. Many of the bounced messages are returned what appears to be the INLINE image embedded as text in the TEXT part of the email. This means I am having to download TEXT parts which are many MB in size when all i need is about the first 1Kb to get the email address and status code. Does anyone know if there is an IMAP parameter/command that I can tell it to only download the first NNN BYTES of the TEXT Part. I have seen this done using FOPEN to read messages. |