Mail attachments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
disco stu
Forum Newbie
Posts: 1
Joined: Tue Jun 03, 2003 8:55 am

Mail attachments

Post by disco stu »

I put together an html form on my site. Its controlled by php and sends the forms content to me as an email. I want teh user to be able to upload files to be mailed as attachments. How might I incorporate this. I found a script to do this however i know jack <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> about php and kdont know how to modify it for my needs.

<?php
$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_from = ""; // Who the email is from
$email_subject = ""; // The Subject of the email
$email_message = ""; // Message that the email has in it

$email_to = ""; // Who the email is too

$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!");
}



and lets just say for current purposes I have a form with fields named name, email, phone no, attachment, attachment2.

TY
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The first thing would be to have a look at tutorials on PHP:
http://www.phpcomplete.com
http://www.devshed.com/Server_Side/PHP
http://www.phpbuilder.com

Then uploading is explained here:
http://php.net/manual/features.file-upload.php
and an explanation of the mail() function here:
http://php.net/manual/function.mail.php

Mac
Okanogan
Forum Newbie
Posts: 9
Joined: Sun Aug 10, 2003 2:59 pm

Request file

Post by Okanogan »

I check the links you had there and have search for a few hours on attaching files to emails using forms.

Form.php
<form action="contactresponse.php" method="post" name="contact" id="contact">

<input name="userName" type="text" id="userName" size="30">
<input name="userEmail" type="text" id="userEmail" size="30">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="thefile" />

<input type="submit" name="Submit" value="Submit">


I would like to know how you get the file on the form processing page and attach it to the email.

contactresponse.php
<?php
$recipient = "name@domain.com";
$subject = "Email with attachment";
$userName = $_REQUEST['userName'];
$userEmail = $_REQUEST['userEmail'];
"request the file here"

$msg = "blah \n";
$msg.= "blah \n";

mail ($recipient, $subject, $msg, "From: $sender");

?>

I saw alot of information on how to upload the files to the server but not much on attachmets to email.
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

Okanogan
Forum Newbie
Posts: 9
Joined: Sun Aug 10, 2003 2:59 pm

Post by Okanogan »

Thank you for the information, I will take a look at it. I am pretty new to the php world.
Post Reply