Topic: PHP Email with attachment

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

<!--------------------------------->

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>


<!--------------------------------->

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 "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}

Vote up Vote down

Re: PHP Email with attachment

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_e … chment.php

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: PHP Email with attachment

theres a script called opencart.... and in there libary they have a PHP5 class for sending emails and its not built into the open cart system.. meaning you can just take the file and use it in other scripts

that has attachment support

Vote up Vote down

Re: PHP Email with attachment

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

Vote up Vote down

Re: PHP Email with attachment

grucker wrote:

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_e … chment.php

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: PHP Email with attachment

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("<strong>Hello World!</strong> (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();
?>

Last edited by cornetofreak (November 29, 2009 4:22 am)

Vote up Vote down

Re: PHP Email with attachment

I would like to thank everyone for their advice. Every thing is working perfectly and as many attachments as anyone could want. One problem solved.
Regards
David

Vote up Vote down

Re: PHP Email with attachment

I just finished my Ajax Submit Tut. It's not the best, but it's one way to do it for a person with limited knowledge of PHP/JS (like me).

Vote up Vote down

Re: PHP Email with attachment

Eric wrote:

I just finished my Ajax Submit Tut. It's not the best, but it's one way to do it for a person with limited knowledge of PHP/JS (like me).

Nice work, Eric.
Works fine on FF3.5.5 Ubuntu 9.10 with or without js.

My signature goes here --> X

Vote up Vote down

Re: PHP Email with attachment

Thanks Jim!

Vote up Vote down