Jump to content

Email attachment - MIME


Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.