Kasak Posted January 12, 2008 Share Posted January 12, 2008 Hi All - I am having problem with this code can some help me and guide me how can i have multiple attachments as well .One problem with this code is it is working only for jpg file and not doc/pdf. <?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" /> Recipient Email: <input name="to_email" size="50" /> From Name: <input name="from_name" size="50" /> From Email: <input name="from_email" size="50" /> Subject: <input name="subject" size="50" /> Message: <textarea name="body" rows="10" cols="50"></textarea> Attachment: <input type="file" name="attachment" size="50" /> <input type="submit" value="Send Email" /></p> <?php } function sendMail() { if (!isset ($_POST['to_email'])) { //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."; } } ?> <!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> <meta 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> Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/ Share on other sites More sharing options...
Kasak Posted January 12, 2008 Author Share Posted January 12, 2008 I have figured the email attachments but one thing that is creating problem is :- when I am trying to email the details of the form it is coming in one line even if I am adding "\n" in $message ? <?php /* Mailer with Attachments */ global $action; $action = $_REQUEST['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> <p>Name: <input name="to_name" size="50" /></p> <p>E00: <input name="to_emichid" size="50" /></p> <p>Email@emich.edu: <input name="to_email" size="50" /></p> <p>Mailing Address: <input name="to_mailingaddress" size="80" /></p> <p>Title of Work: <input name="title" size="80" /></p> <p>Medium (poetry, prose, sculpture, photo, etc.):<input name="medium" size="80" /></p> <p>Phone: <input name="to_number" size="80" /></p> <p>Attachment: <input type="file" name="attachment" size="50" /></p> <input type="submit" value="Send Email" /></p> <?php } function sendMail() { if (!isset ($_POST['to_email'])) { die ("<p>You forgot to fill out the email address! Click on the back arrow to go back</p>"); } else { $from_name ='RealBeginnings'; $from_email ='welcome@emich.edu'; $subject ='Real Beginning Submission'; $body ='Please verify the details you have provided'; $to_name = stripslashes($_POST['to_name']); $to_emichid = stripslashes($_POST['to_emichid']); $to_mailingaddress = stripslashes($_POST['to_mailingaddress']); $title = stripslashes($_POST['title']); $medium = stripslashes($_POST['medium']); $to_number = stripslashes($_POST['to_number']); $to_email = $_POST['to_email']; $attachment = $_FILES['attachment']['tmp_name']; $attachment_name = $_FILES['attachment']['name']; if (is_uploaded_file($attachment)) { $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<" .$from_email . ">\n"; $headers .= "Reply-To: <" . $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<" . $from_email . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 1\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $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/html; 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 .= "Name : " . "$to_name\n"; $message .= "E00 : " . "$to_emichid\n"; $message .= "Email@emich.edu : " . "$to_email\n"; $message .= "Mailing Address : " . "$to_mailingaddress\n"; $message .= "Title of Work: " . "$title\n"; $message .= "Medium (poetry, prose, sculpture, photo, etc.): " . "$medium\n"; $message .= "Phone: " . "$to_number\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."; } } ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <?php include("../Scripts/insert_styles.php"); ?> </head> <body> <div id="container"> <?php include("../Scripts/menu.php"); insert_header("nsp"); ?> <?php switch ($action) { case "send": sendMail(); showForm(); break; default: showForm(); } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/#findComment-437492 Share on other sites More sharing options...
Kasak Posted January 13, 2008 Author Share Posted January 13, 2008 Please answer to me how can I now work with multiple attachments Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/#findComment-437959 Share on other sites More sharing options...
monkeytooth Posted January 13, 2008 Share Posted January 13, 2008 I'd say get the files to .zip with the use of zend (correct me if im wrong) then take the .zip and attach it to the email to be sent via the script.. I think it would work better this was as some email providers only allow for one attachment per email still Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/#findComment-437961 Share on other sites More sharing options...
Kasak Posted January 13, 2008 Author Share Posted January 13, 2008 Can you tell me how to get the multiple attachments work ? Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/#findComment-437965 Share on other sites More sharing options...
esport Posted March 19, 2008 Share Posted March 19, 2008 I used to code and works great, but would be nice to know how to send multiple files. Quote Link to comment https://forums.phpfreaks.com/topic/85692-email-with-attachments/#findComment-495571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.