ragsr@ix.netcom.com Posted June 25, 2011 Share Posted June 25, 2011 Hopefully this item is pretty simple for a person experienced with MIME email code BACKGROUND: I host a php application on Host Monster and I've added MIME email code to it that, by and large, is working. From a php reference, I have the example, "CODE EXAMPLE", below which is from a reference text. I need to send attachments from a different source than the example which happens to be an attached disk. I want to source the attachment from a network address such as "http://www.abccompany/images/testfile.jpg" instead of a local disk. REQUEST: Please give me an example of how the code example below can be modified to source the image file from a network address vs. from a local disk. NOTE: I've attached a Notepad file with the example cc'd into it. CODE EXAMPLE: <?php //include "mime_mail.inc" ; Not needed on Host Monster $filename = "testfile.jpg" ; $content_type = "image/jpeg" ; #read a JPEG picture from the disk $fd = fopen($filename, "r") ; $data = fread($fd, filesize($filename)) ; fclose($fd) ; #create object instance $mail = new mime_mail ; #set all data slots $mail->from = "your@address.com" ; $mail->to = "recipient@remote.net" ; $mail->subject = "welcome!" ; $mail->body = "Hello" ; #append the attachment $mail->add_attachment ($data, $filename, $content_type) ; #send e-mail $mail->send() ; ////MIME EMAIL CODE - ALLOWS ATTACHEMENTS ///////////////////////////////////////////////// // store as "mime_mail.inc" class mime_mail { var $parts; var $to; var $from; var $headers; var $subject; var $body ; /* * void mime_mail() * class constructor */ function mime_mail () { $this->parts = array() ; $this->to = "" ; $this->from = "" ; $this->subject = "" ; $this->body = "" ; $this->headers = "" ; } /* * void add_attachement (string message, [string name], [string ctype]) * Add an attachment to the mail object */ function add_attachment ($message, $name= "", $ctype = "application/octetstream") { $this->parts[] = array ( "ctype" => $ctype, "message" => $message, "encode" => $encode, "name" => $name, ); } /* * void build_message (array part= * Build message parts of an multipart mail */ function build_message ($part) { $message = $part[ "message"]; $message = chunk_split(base64_encode($message)) ; $encoding = "base64" ; return "Content-Type: ".$part["ctype"]. ($part["name"]?" ; name =\"". $part["name"]. "\"":""). "\nContent-Transfer-Encoding: $encoding\n\n$message\n" ; } /* * void build_multipart() * Build a multipart mail */ function build_multipart() { $boundary = "b".md5(uniqid(time())) ; $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary" ; for ($i = sizeof($this->parts)-1; $i >+ 0; $i--) { $multipart .= "\n".$this->build_message($this->parts[$i])."--$boundary" ; } } /* * string get_mail() * returns the constructed mail */ function get_mail($complete = true) { $mime = "" ; if (!empty($this->from)) $mime .= "From: ".$this->from. "\n" ; if (!empty($this->headers)) $mime .= $this->headers. "\n" ; if ($complete) { if (!empty($this->to)) { $mime .= "To: $this->to\n" ; } if (!empty($this->subject)) { $mime .= "Subject: $this->subject\n" ; } } if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plane"); $mime .= "MIME-Version: 1.0\n".$this->build_multipart(); return $mime; } /* * void send() * Send the mail (last class-function to be called) */ function send () { $mime=$this->get_mail (false) ; mail($this->to, $this->subject, $this->body, $mime) ; } } ; ////END OF MIME CODE ///////////////////////////////////////////////////////////////// ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/240408-email-attachment-mime/ Share on other sites More sharing options...
dreamwest Posted June 26, 2011 Share Posted June 26, 2011 Boundry attachments are always fun http://www.wizecho.com/nav=php&s=email Quote Link to comment https://forums.phpfreaks.com/topic/240408-email-attachment-mime/#findComment-1234917 Share on other sites More sharing options...
ragsr@ix.netcom.com Posted June 26, 2011 Author Share Posted June 26, 2011 dreamwest, Thanks for the reply. I appreciate the effort. What you sent me is another script for sending email with attachment using MIME. I already have a script that is supposed to do the same thing and seems to work but it is an example for which the file is assumed to be on a local disk. My file is on the web and my problem is actually quite simple. What I need help on is figuring out, after getting the attachment using a web address, exactly how to feed the attachment file to the send () script of the example I already have. I can narrow that down further. I've now read that fopen will operating on regular files, http files, and ftp files so I was able to use the fopen function to get the attachment file from the web. The sample code that I have first does a fopen to get the file then it does an fread to get the data. I'm having diffculty with the latter. Those two lines are... $fd=fopen('http://abccompany/testimagefile.jpg', 'r') ; //This works fine $data=fread($fd, filesize($filename)) ; Unfortunately, I get in trouble with the "$filename" part of the second line. If, for $filename, I use "testimagefile.jpg" or http://abcompany/testimagefile.jpg or $fd, the file is not found. I think this is due to the fact that the file is not local and I don't know what form or format the value for the variable $filename should be. I'm not at all experienced in dealing with file system ops so this is difficult for me but it should be dead simple for you. Cheers, Bob G. Quote Link to comment https://forums.phpfreaks.com/topic/240408-email-attachment-mime/#findComment-1235045 Share on other sites More sharing options...
dreamwest Posted June 26, 2011 Share Posted June 26, 2011 $file = "http://www.wizecho.com/images/programming.jpg"; //if you dont get the attachment check to see if the image is still availiable $attachment = chunk_split(base64_encode(file_get_contents($file))); Quote Link to comment https://forums.phpfreaks.com/topic/240408-email-attachment-mime/#findComment-1235131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.