bschultz Posted June 8, 2009 Share Posted June 8, 2009 I have written / edited / downloaded a script that will take an attachment from a form and attach it to an email. It works just fine...but if no attachment is selected in the html form...I get an "ATT00019.dat" attachment in the email. How would I change this code to NOT include an attachment if none is selected? Thanks. <?php //information processed from form submitted. $msg = $_REQUEST['message']; $email = $_REQUEST['emailaddr']; $name = $_REQUEST['name']; $subject = $_REQUEST['subject'] ; $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: $email\n"; $headers .= "Reply-To: $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: $name<" . $_POST['email'] . ">\n"; $headers .= "X-Mailer: PHP4\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 .= "$msg\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"; //final code to send the message $dbc = mysql_pconnect('localhost','username','password'); mysql_select_db('DB TO SELECT',$dbc); $sql = "SELECT login FROM TABLENAME"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$message", "$headers" ); } header("Location: email.php"); ?> Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/ Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 Take these lines $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 and put an if statement around them if ($attachment) { $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 } This basically then says if $attachment has a value to include these lines in your message which are the applicable lines for including the attachment in your email. Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851820 Share on other sites More sharing options...
bschultz Posted June 8, 2009 Author Share Posted June 8, 2009 thanks for the help...still got a ATT00019.dat attachment that's about 50 bytes in size Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851830 Share on other sites More sharing options...
947740 Posted June 8, 2009 Share Posted June 8, 2009 Throw in a if(isset($_FILES['attachment'])) {} Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851847 Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 Indeed, that means $attachment is getting defined, try using the aforementioned isset to see if that works Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851849 Share on other sites More sharing options...
bschultz Posted June 8, 2009 Author Share Posted June 8, 2009 where? Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851851 Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 Instead of using what I gave you use this.. if(isset($_FILES['attachment'])) { $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 } Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851856 Share on other sites More sharing options...
bschultz Posted June 8, 2009 Author Share Posted June 8, 2009 still getting a .dat attachment... <?php //information processed from form submitted. $msg = $_REQUEST['message']; $email = $_REQUEST['emailaddr']; $name = $_REQUEST['name']; $subject = $_REQUEST['subject'] ; $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: $email\n"; $headers .= "Reply-To: $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: $name<" . $_POST['email'] . ">\n"; $headers .= "X-Mailer: PHP4\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 .= "$msg\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_message_parts--\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message\n"; if(isset($_FILES['attachment'])) { $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"; //final code to send the message $dbc = mysql_pconnect('xxx','xxx','xxx'); mysql_select_db('xxx',$dbc); $sql = "SELECT login FROM xxx"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$message", "$headers" ); } header("Location: email.php"); ?> Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851868 Share on other sites More sharing options...
bschultz Posted June 8, 2009 Author Share Posted June 8, 2009 I echoed out $attachment and $attachment_name...and with an attachment in the html form, it echoed out just fine...with no attachment in the html form, it didn't echo anything. Here's my html form...if that helps <form method="post" enctype="multipart/form-data" name="send" action="formprocess.php"> <table border="0" bgcolor="#ececec" cellspacing="5"> <tr><td>Senders Name</td><td><input type="text" size="30" name="name"></td></tr> <tr><td>Senders Email address</td><td><input type="text" size="30" name="emailaddr"></td></tr> <tr><td>Email Subject</td><td><input type="text" size="30" name="subject"></td></tr> <tr><td>Message</td><td><textarea name="message" rows="6" cols="30"></textarea></td></tr> <tr><td>Attachment</td><td><input type="file" name="attachment" size="50" /></td></tr> <tr><td> </td><td><input type="submit" value="Send"></td></tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-851869 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 Now, I'm getting a text file in the attachment...even though I haven't selected anything to attach. <?php //information processed from form submitted. $msg = $_REQUEST['message']; $email = $_REQUEST['emailaddr']; $name = $_REQUEST['name']; $subject = $_REQUEST['subject'] ; $attachment = $_FILES['attachment']['tmp_name']; $attachment_name = $_FILES['attachment']['name']; if (is_uploaded_file($attachment)) { //Do we have a file attached? $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: $email\n"; $headers .= "Reply-To: $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: $name<" . $_POST['name'] . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $_POST['name'] . ">\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 .= "$msg\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"; //final code to send the message $dbc = mysql_pconnect('localhost','xxx','xxx'); mysql_select_db('xxx',$dbc); $sql = "SELECT login FROM members_test"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$message", "$headers" ); } } else { $headers = "From: $email\n"; $headers .= "Reply-To: $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: $name<" . $_POST['name'] . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $_POST['name'] . ">\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 .= "$msg\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_message_parts--\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; //final code to send the message $dbc = mysql_pconnect('localhost','xxx','xxx'); mysql_select_db('xxx',$dbc); $sql = "SELECT login FROM members_test"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$message", "$headers" ); }} echo "Emails have been sent"; header("Location: email.php"); ?> Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-852064 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 I got it...get rid of all the headers in the second part of the "else" and it works... <?php //information processed from form submitted. $msg = $_REQUEST['message']; $email = $_REQUEST['emailaddr']; $name = $_REQUEST['name']; $subject = $_REQUEST['subject'] ; $attachment = $_FILES['attachment']['tmp_name']; $attachment_name = $_FILES['attachment']['name']; if (is_uploaded_file($attachment)) { //Do we have a file attached? $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: $email\n"; $headers .= "Reply-To: $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: $name<" . $_POST['name'] . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $_POST['name'] . ">\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 .= "$msg\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"; //final code to send the message $dbc = mysql_pconnect('localhost','xxx','xxx'); mysql_select_db('xxx',$dbc); $sql = "SELECT login FROM members"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$message", "$headers" ); } } else { $headers = "From: $email\n"; $headers .= "Reply-To: $email\n"; $dbc = mysql_pconnect('localhost','xxx','xxx'); mysql_select_db('xxx',$dbc); $sql = "SELECT login FROM members"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; mail ( "$row[login]" , "$subject", "$msg", "$headers" ); }} echo 'Your emails have been sent...<meta http-equiv=Refresh content=5;url="sendemail.php">'; ?> Link to comment https://forums.phpfreaks.com/topic/161418-solved-email-with-attachment/#findComment-852070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.