homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

send form mail with attachments

10+ Year Member

Msg#: 3956665 posted 4:36 pm on Jul 21, 2009 (gmt 0)

Hi, I have a simple mail form that sends mail from a website. Too easy. Now I need to add an image-attachment to the mail and I keep getting a "Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent." error when I try to send. I've never done this before so I haven't a clue where I'm going wrong.

Can anyone look over this and tell me why I'm getting that error or better yet, how to fix it. Thanks in advance.


function createForm() { // grab variables from posted form, if any exist
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$comments = $_POST['comments'];

// build the form, populate the fields if needed
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST" enctype="multipart/form-data">
<p>FULL NAME:<br />
<input name="name" type="text" id="name" size="40" value="' . $name . '"></p>
<p>TELEPHONE:<br />
<input name="phone" type="text" id="phone" size="40" value="' . $phone . '"></p>
<input name="email" type="text" id="email" size="40" value="' . $email . '"></p>
<p>IMAGE (*optional):<br />
<input type="file" name="attachment"></p>
<textarea name="comments" id="comments" rows="5" style="width:100%;">' . $comments . '</textarea></p>
<p><input type="submit" name="submit" value="Submit">&nbsp;<input type="reset" name="reset" value="Clear"></p>
} // end function createForm()

// if submit has been pressed
if(isset($_POST['submit'])) {
echo '<ul>';

// make sure all required fields are filled in and display errors if empty
if(empty($_POST['name'])) {
$field1 = "0";
echo '<li class="red strong">Name is required.</li>';
} else {
$field1 = "1"; }

if(empty($_POST['phone'])) {
$field2 = "0";
echo '<li class="red strong">Phone Number is required.</li>';
} else {
$field2 = "1"; }

if(empty($_POST['email'])) {
$field3 = "0";
echo '<li class="red strong">Email Address is required.</li>';
} else {
$field3 = "1";}

if(empty($_POST['comments'])) {
$field4 = "0";
echo '<li class="red strong">Comments are required.</li>';
} else {
$field4 = "1"; }

echo '</ul>';

// if any required fields are missing
if(($field1=="0") ($field2=="0") ($field3=="0") ($field4=="0")) {

// if all required fields are there
if(($field1=="1") && ($field2=="1") && ($field3=="1") && ($field4=="1")) {

// recipients email address
$recipient = 'me@site.com';

// All form fields are automatically passed to the PHP script through the array $_POST
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$attachment = $_POST['attachment'];

// format the message for mail
$comments = stripslashes($comments);
$now = date('l F dS, Y g:i:s a');
$message = "The following is a message from the website sent on $now.\n\nFrom: $name\nEmail: $email\nPhone Number: $phone\n\n\n$comments";

// add email subject and headers
$subject = "Email from website";

//Gather file data and other things
$tmp = $_FILES['attachment']['tmp_name'];
$sep = md5(time());
$filename = $_FILES['attachment']['name'];

$filedata = file_get_contents($tmp); //Get file contents
$fdata = chunk_split(base64_encode($filedata)); //Encode data into text form;

//Determine mime type
$ext = explode('.', $filename);
$ext = $ext[1];

if($ext == "JPG" $ext == "jpg" $ext == "JPEG" $ext == "jpeg") {
$mime_type = "image/jpeg";
elseif($ext == "gif" $ext == "GIF") {
$mime_type = "image/gif";
else {
exit("Error: Wrong file type!");

//Begin the headers
$headers = "From: \"".$name."\" <{".$email."}>
MIME-Version: 1.0
Content-Type: Multipart/Mixed;

Content-Transfer-Encoding: 7bit

Content-Type: ".$mime_type.";
Content-Transfer-Encoding: base64
Content-Disposition: attachment;


// sends the mail

echo "<p class=\"strong bigger red\">Your email has been sent!</p>";
echo "<p>Thank you for contacting us. We will do our best to reply to you as soon as is possible. For the quickest service, feel free to give us a phone call.</p>";
echo "<p><a href=\"contact.php\">Send another message &#187;</a></p>";
} else {



5+ Year Member

Msg#: 3956665 posted 2:49 pm on Jul 22, 2009 (gmt 0)

get rid of your headers and message variables, and your file upload method and replace with the code below -

The 'meesage' is sent as part of the email headers as $body
The file attachment is opened, read and encoded by php.


$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, "rb"); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed

//Let's start our headers
$headers = "From: $name<" . $_POST['email'] . ">\n";
$headers .= "Reply-To: <" . $recipient . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $name<" . $_POST['email'] . ">\n";
$headers .= "X-Mailer: PHP Mail\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";

$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";

// send the message
mail($recipient, $subject, $message, $headers);


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved