Jump to content
Stef's Coding Community

PHP Email with attachment


Guest ConciergeCity

Recommended Posts

Guest ConciergeCity

I am trying to use a form so clients can send me pictures off my site and i cant seem to get the file to open in my email.

 

 

form.php

 

 

enctype="multipart/form-data">

Filename:

 

 

 

 

 

upload_file.php= process script

 

 

$fileatt = ""; //

$fileatt_type = "application/octet-stream"; //

$fileatt_name = ""; //

 

$email_from = ""; //

$email_subject = ""; //

$email_txt = "Hello"; //

 

$email_to = "my_email@domain.com"; //

 

$headers = "From: ".$email_from;

 

$file = fopen($fileatt,'rb');

$data = fread($file,filesize($fileatt));

fclose($file);

 

$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

 

$headers .= "\nMIME-Version: 1.0\n" .

"Content-Type: multipart/mixed;\n" .

" boundary=\"{$mime_boundary}\"";

 

$email_message .= "This is a multi-part message in MIME format.\n\n" .

"--{$mime_boundary}\n" .

"Content-Type:text/html; charset=\"iso-8859-1\"\n" .

"Content-Transfer-Encoding: 7bit\n\n" .

$email_message . "\n\n";

 

$data = chunk_split(base64_encode($data));

 

$email_message .= "--{$mime_boundary}\n" .

"Content-Type: {$fileatt_type};\n" .

" name=\"{$fileatt_name}\"\n" .

//"Content-Disposition: attachment;\n" .

//" filename=\"{$fileatt_name}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n" .

"--{$mime_boundary}--\n";

 

$ok = @mail($email_to, $email_subject, $email_message, $headers);

 

if($ok) {

echo "The file was successfully sent!";

} else {

die("Sorry but the email could not be sent. Please go back and try again!");

}

Link to post
Share on other sites

A couple questions... Are you trying to embed the attachment within the email content itself? Just trying to attach a specific file to the message that gets sent?

 

I'd suggest starting here, and comparing your code to their example of an HTML email with attachment:

http://www.webcheatsheet.com/php/send_email_text_html_attachment.php

Link to post
Share on other sites
  • 3 weeks later...
I too am attempting to produce a form that sends an attachment, with little success. It finds the attachment and attaches the attachment but doesn't send the attachment. Anyone have a script I can peruse to get this problem resolved. Kindest regards, Grucker

 

The page I linked to a couple posts up on this topic has examples (I've adapted them to my needs before, so I know they work): http://www.webcheatsheet.com/php/send_email_text_html_attachment.php

Link to post
Share on other sites

Heres my version you can use

Key-Features:

  • Simple OOP
  • Plain-Text OR Html Formatted
  • Use both Plain-Text and Html to help with unsupported clients
  • Able to add attachments of any sort
  • Based on PHP-Mail
  • Multiple Recipients in one batch
  • Built for "Php Newbies"

 

Available on PhpClasses for download

 

Some Usage Smaples

 

Simple Plain text

<?php

//Include the emailer class
include 'Emailer.class.php';

//Load the Emailer class into a variable
$Emailer = new Emailer;

//Setup where and where from the message is being sent.
$Emailer->set_to("some_email@some_domain.tld");
$Emailer->set_from("admin@localhost");
$Emailer->set_sender("me@domain.com");

//Afdd some message stuff
$Emailer->set_subject("This is a plain-text email");
$Emailer->set_text("Hello World!");

$Emailer->send();
?>

 

HTML formatted with

<?php

//Include the emailer class

include 'Emailer.class.php';

//Load the Emailer class into a variable
$Emailer = new Emailer;

//Setup where and where from the message is being sent.
$Emailer->set_to("some_email@some_domain.tld");
$Emailer->set_from("admin@localhost");
$Emailer->set_sender("me@domain.com");

//Add some message stuff
$Emailer->set_subject("This is a html formatted email");

/*Set the text if the end-user does not have the correct software to view html*/
$Emailer->set_text("Hello World! (Non html version)");
$Emailer->set_html("Hello World! (html Version)");

//html will show if supported otherwise they will be sent a nice plain-text version

$Emailer->send();
?>

 

HTML Formatted with Attachments

<?php

//Include the emailer class

include 'Emailer.class.php';

//Load the Emailer class into a variable
$Emailer = new Emailer;

//Setup where and where from the message is being sent.
$Emailer->set_to("some_email@some_domain.tld");
$Emailer->set_from("admin@localhost");
$Emailer->set_sender("me@domain.com");

//Afdd some message percifics
$Emailer->set_subject("This is a html formatted email with files");

/*Set the text if the end-user does not have the correct software to view html*/
$Emailer->set_html("Here's your files");

//Add some files
$Emailer->add_attachments(
   array(
     "test/account_details.txt",
     "test/your_avater.png",
     "test/some_sample_source.php",
     "test/some_random.ext"
     /*Add as many as you wish but try not overload the message size by sending 100MB download lol*/
   )
);

$Emailer->send();
?>

Edited by cornetofreak
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...