Page 1 of 1

Using mail() to attach image to email??

Posted: Tue Jun 03, 2008 9:40 am
by hairytea
I have a form that asks simply for name and phone number as well as an upload...

I want the end user to be able to upload an image and have that image sent as part of the email body!

I have managed to get the script working as far as taking the uploaded file and depositing it in a directory on the server....but I need to know how to then add the image just uploaded to the email $string.

Thank you in advance :-)

Code: Select all

 
<?php
  $file_dir = $_SERVER['DOCUMENT_ROOT'] . '/terry/uploads/';
  $name = $_REQUEST['name'] ;
  $phone = $_REQUEST['telephone'] ;
  
  foreach($_FILES as $file_name => $file_array) {
 
    if (is_uploaded_file($file_array["tmp_name"])) {
        move_uploaded_file($file_array["tmp_name"], "$file_dir/".$file_array["name"]) or die ("Couldn't copy");
    }
  }
  
  $string = "T-Shirt order form....\n\n\n";
  $string .= "Name:                        ".$name."\n";
  $string .= "Telephone Number:            ".$phone."\n";
  $string .= "T-Shirt Design"."\n\n";
  
 mail("root@thanweb.co.uk", "Website - T-Shirt Order", $string, "From: root@thanweb.co.uk", "-froot@thanweb.co.uk"); 
 header( "Location: http://www.thanweb.co.uk/thankyou.htm" );
?>
 

Re: Using mail() to attach image to email??

Posted: Tue Jun 03, 2008 9:55 am
by hairytea
I've spent 3 hours searching the net for a solution and cannot seem to find one?

If i'm trying to do something not possible could someone please please take me out of my misery and let me know :-)

Thank you

Re: Using mail() to attach image to email??

Posted: Tue Jun 03, 2008 10:35 am
by gosu
Part of the working script for my site, able to send files over the email by request of the user :)
You might find something suitable for you.

Code: Select all

   
 
   $id = intval($_GET['id']);
    if($id)
    {
        $sql_file = $db->query("SELECT `title`, `category`, `screenshot`, `filename`, `description`, `size` FROM `" . PREFIX . "_".$modul_dbtitle."` WHERE `id` = '{$id}'");
        $row = $db->get_row($sql_file);
        $title = $row['filename'];
        $title2 = $row['title'];
        $size = intval($row['size']);
        if ($size < 2097152)
        {
            $description = $row['description'];
            $cat_title=intval($row['category']);
 
            $sql_select = "SELECT parentid, dir FROM " . PREFIX . "_cat_".$modul_dbtitle." WHERE id = '{$cat_title}'";
            $sql_result = $db->query($sql_select);
            $row = $db->get_array($sql_result);
            $dir_cat=$row['dir'];
            if (!empty($title) AND file_exists(ROOT_DIR."/uploads/".$modul_filesdir."/$dir_cat/$title"))
            {
                $file_path = ROOT_DIR."/uploads/".$modul_filesdir."/$dir_cat/$title";
 
                $text = strip_tags("Please don't respond to this E-mail. You requested the file \"{$title}\" from site.com\n\n????????:\n {$description}\n\n Have a nice day! :)");
                $email = $db->super_query("SELECT `email` FROM `" . PREFIX . "_users` WHERE `user_id` = '{$member_id['user_id']}'");
                $email = $email['email'];
                include_once ENGINE_DIR.'/classes/mail.class.php';
                $mail = new dle_mail ($config, false);
                $mail->from = "site.com <admin@site.com>";
                $mail->to = $email;
                // From?
                $headers = "From: {$mail->from}\nTo: {$mail->to}\nSubject: File delivery \"$title2\" from site.com\n";
                $headers .= "Date: " . date('j F Y H:i');
                // Forming boundary string - 
                $semi_rand = md5(time());
                $mime_boundary = "--{$semi_rand}--";
 
                // Type of file transfer
                $headers .= "\nMIME-Version: 1.0\n" .
                "Content-Type: multipart/mixed;\n" .
                " boundary=\"{$mime_boundary}\"";
                $mail->mail_headers = $headers;
 
                $message = "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type: text/plain; charset=\"windows-1251\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" .
                $message . $text."\n\n";
                $file = fopen($file_path,'rb'); //Find stream
                $data = fread($file,filesize($file_path));
 
                fclose($file);  //Close stream
 
                // Copy Base64 file contents
                $data = chunk_split(base64_encode($data));
 
                // Descr of filetype
                $message .= "--{$mime_boundary}\n".
                "Content-Type: application/octet-stream;\n".
                " name=\"{$title}\"\n".
                "Content-Transfer-Encoding: base64\n\n".
                $data."\n\n";
                $message .= "--{$mime_boundary}--\n";
                $mail->message = $message;
 
                $mail->smtp_send();

Re: Using mail() to attach image to email??

Posted: Wed Jun 04, 2008 4:40 am
by hairytea
Is it not possible to extract the file just uploaded and sent to the directory i chose to be added to the string like....

Code: Select all

 
#   $string = "T-Shirt order form....\n\n\n";
#   $string .= "Name:                        ".$name."\n";
#   $string .= "Telephone Number:            ".$phone."\n";
#   $string .= "T-Shirt Design"."\n\n";
 
// NEW LINE
 
# $string .= $file_array["name"] ;
 
 
or something similar? if you see what i'm trying to do?