Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Email Attachments

the mail() function

Moderators: macek, egami, gesf

Email Attachments

Postby 2nejunkie » Sun Aug 10, 2008 11:30 am

Ok Ima n00b at php but have had success with copy and paste.
I am trying to make an Email form that allows picture attachments. I get the emails to send but the attachments keep showing up in text.
Code: Select all
<?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<p>Recipient Name: <input name="to_name" size="50" /><br />
Recipient Email: <input name="to_email" size="50" /><br />
From Name: <input name="from_name" size="50" /><br />
From Email: <input name="from_email" size="50" /><br />
Subject: <input name="subject" size="50" /><br />
Message: <textarea name="body" rows="10" cols="50"></textarea><br />
Attachment: <input type="file" name="attachment" size="50" /><br />
<input type="submit" value="Send Email" /></p>

<?php
}

function sendMail() {
if (!isset ($_POST['2ne@2nejunkie.com'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = stripslashes($_POST['to_name']);
$from_name = stripslashes($_POST['from_name']);
$subject = stripslashes($_POST['subject']);
$body = stripslashes($_POST['body']);
$to_email = $_POST['to_email'];
$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
fclose($fp);
}
//Let's start our headers
$headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['from_email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['from_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("$to_name<$to_email>", $subject, $message, $headers);
print "Mail sent. Thank you for using the MyNewName5333 Mailer.";
}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style="css" type="text/css">
<!--
body {
margin: 0px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
a {color: #0000ff}
-->
</style>
</head>
<body>

<?php
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>

</body>
</html>


I would like for it to 1st not ask for Recipient email or Name.
2nd I would like for it to not ask for subject.
This is not a script I made. I found it on the web after Hours of searching.
Please help. maybe even rewrite the code.
User avatar
2nejunkie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Aug 10, 2008 11:18 am
Location: New York, Central New York

Re: Email Attachments

Postby gesf » Sun Aug 10, 2008 11:47 am

Hey 2nejunkie,

I think this is happening because you're not sending the file's mime type.
Try this out:

Code: Select all
// Add this next line where you're "getting" the file data:
$attachment_type = $_FILES['attachment']['type'];

Code: Select all
// Replace
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";

// With:
$message .= "Content-Type: " . $attachment_type . "; name=\"" . $attachment_name . "\"\n";


Hope it helps ;)
User avatar
gesf
Moderator
Moderator
 
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Re: Email Attachments

Postby 2nejunkie » Sun Aug 10, 2008 12:04 pm

Yes and thank you very much.
One stump I have is
Code: Select all
// Add this next line where you're "getting" the file data:
$attachment_type = $_FILES['attachment']['type'];

Does this go in with $message or in this section
Code: Select all
{
$to_name = stripslashes($_POST['to_name']);
$from_name = stripslashes($_POST['from_name']);
$subject = stripslashes($_POST['subject']);
$body = stripslashes($_POST['body']);
$to_email = $_POST['to_email'];
$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
fclose($fp);
}
User avatar
2nejunkie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Aug 10, 2008 11:18 am
Location: New York, Central New York

Re: Email Attachments

Postby gesf » Sun Aug 10, 2008 12:54 pm

Yes, it should be in that part of the code.
Example:
Code: Select all
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
$attachment_type = $_FILES['attachment']['type'];
User avatar
gesf
Moderator
Moderator
 
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.