Jump to content

[SOLVED] Email with attachment


bschultz

Recommended Posts

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

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.

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

}

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");
?> 

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>

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");
?> 

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">';
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.