PHP - Imap Attachments
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.
Similar TutorialsHey 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'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... 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); ?> 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. I want to read an imap message with all the headers, and body, what would be the best way to do that? 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? 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 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 Hi I am creating a simple program to send e-mail. It contains 2 pages, index.php and sendmail.php Here is the code of index.php Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Send Mail #1</title> </head> <body> <p> </p> <p> Mail 1</p> <form id="form1" name="form1" method="post" action="sendmail.php"> <table width="821" height="240" border="0"> <tr> <td width="168">To :</td> <td colspan="2"><label> <input name="to_textbox" type="text" id="to_textbox" size="75" /> </label></td> </tr> <tr> <td>Subject :</td> <td colspan="2"><label> <input name="subject_textbox" type="text" id="subject_textbox" value="" size="75" /> </label></td> </tr> <tr> <td>From :</td> <td colspan="2"><label> <input name="from_textbox" type="text" id="from_textbox" value="" size="75" /> </label></td> </tr> <tr> <td>Content :</td> <td colspan="2"><label> <textarea name="content_textbox" id="content_textbox" cols="75" rows="5"> </textarea> </label></td> </tr> <tr> <td> </td> <td width="153"><label> <input type="submit" name="send_button" id="send_button" value="Send..." /> </label></td> <td width="486"><label> <input type="reset" name="reset_button" id="button" value="Reset" /> </label></td> </tr> </table> <p> </p> <p> </p> <p> </p> </form> <p> </p> </body> </html> Code for sendmail.php Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $to = $_POST["to_textbox"]; $from = $_POST["from_textbox"]; $subject = $_POST["subject_textbox"]; $message = $_POST["content_textbox"]; $headers = "From: " . $from . "\r\n"; $mailsent = mail($to, $subject, $message, $headers); if ($mailsent) { echo " <center> Congrats ! The message has been sent </center>"; echo " <center><b>\n To: $to </b></center>"; } else { echo " There was an error..."; } ?> </body> </html> How can I add attachments ? I want to add few photos (.jpg) I searched on google but found nothing which can relate with mail() Hello, so I have been able to successfully use the imap_mail_compose function to generate a valid and working MIME message string. However, when trying to add an attachment, the MIME message never contains the attachment., What am I doing wrong?
$envelope["from"]=$_SESSION['username']; $envelope["to"]=$_POST['To']; $envelope["cc"]=$_POST['Cc']; $envelope["bcc"]=$_POST['Bcc']; $envelope["subject"]=$_POST['Subject']; $part1["type"] = TYPEMULTIPART; $part1["subtype"] = "mixed"; $part2["type"]=TYPETEXT; $part2["subtype"]="plain"; $part2["contents.data"]=$_POST['Message']; $uploaddir = "C:\\inetpub\\wwwroot\\PHPTesting\\uploads\\"; $uploadfile = $uploaddir.$_FILES['Attachment']['name']; move_uploaded_file($_FILES['Attachment']['tmp_name'], $uploadfile); $fp = fopen($uploadfile, "r"); $contents = fread($fp, filesize($uploadfile)); fclose($fp); $part3["type"] = $_FILES['Attachment']['type']; $part3["encoding"] = ENCBINARY; $part3["description"] = basename($uploadfile); $part3["contents.data"] = $contents; $body[1]=$part1; $body[2]=$part2; $body[3]=$part3; $mime = imap_mail_compose($envelope, $body); Hi all, Does anyone know how to extract attachments from an email that has more than one attachment associated with it? Getting the overall attachment data works fine using Code: [Select] <?php $attachment_data = imap_fetchbody($inbox,$email_number,2); ?> ..but breaking up that data into its component attachments is eluding me, and I can't find anything on the PHP doco that enabled me to do it. All info appreciated, WoolyG Hi, is there a way to generate attachemtns for php's mail() function on the fly, without first saving a file. I have an html page stored in a variable and would like to send it as both the body of the mail and as an attachment in it (well actualy I would like to just send it in the body....). Can I push the variable as an attachement or do I need to generate a temp file from it first? Cheers hi there. this is my first post. i'm sory for my english (i'm portuguese) i have i very bad situation. on my code. i'm sending an email from my aplication and when arrives to the client the Attachments is send with the extension ".dat". in my pc the email is sent correct. but from other pc (server) the extension arrives ".dat". does anyone have the same problem ??? please help me. tnks 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 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); How would I go about creating a new user email account that can be accessed via my cpanel mail scripts (RoundCube, SquirrelMail, etc.), in PHP. 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 mark107Hi I'm very new to PHP. I've been searching high and low for a script that will process a simple form and also allow a file attachment. I've tried loads of different ones and none of them work as I want. I do actually have one working but this doesn't have any validation of the file. Can anyone please point me in the direction of a script that will allow me to do this. Thanks Hi all Im sending a dynamic created pdf file with php. It gets flagged as spam are things that can be done to help prevent this ? didnt really know where to post it. |