Jump to content

[SOLVED] sending a file attachment


shadiadiph

Recommended Posts

Does anyone know what I need to do to this in order to add $data to this email??

 

<? 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$subject		= ($_POST['subject']);
$firstname 		= ($_POST["firstname"]);
$lastname 		= ($_POST["lastname"]);
$email		= ($_POST['email']);
$telephone		= ($_POST["telephone"]);
$country		= ($_POST["country"]);
$position		= ($_POST["position"]);
$message		= ($_POST['message']);
$random		= ($_POST['random']);
$validate		= ($_POST['validate']);
      $data             = (fread(fopen($form_data, "r"), filesize($form_data))); 



if ($random==$validate)

{

$to = "[email protected]";
$subject = "$subject";
$MsgHeader = "From: APA<[email protected]>\n";
$MsgHeader .= "MIME-Version: 1.0\n";
$MsgHeader .= "Content-type: text/html; charset=iso-8859-1";
$MsgBody = "
<html>
<head>
<title>HTML message</title>
</head>
<body>
<table>
<tr><td align='left'><img src='http://www.website.com/pix/logo.gif'></td></tr>
</table>
<table style='padding-left:20px'>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>FIRSTNAME: $firstname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>LASTNAME: $lastname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>EMAIL: $email</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>TELEPHONE: $telephone</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>COUNTRY: $country</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>POSITION: $position</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>REASON: $message</font></font></td></tr>
<tr><td> </td></tr>
</table>
</body>
</html>";
mail($to, $subject, $MsgBody, $MsgHeader);



	header("Location:thankyou.php?message=careers");
	exit;
}

else

{

	header("Location:thankyou.php?message=invalid");
	exit;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/
Share on other sites

First of all, you are setting header after you have output data. This wont work.

 

And you could use phpmailer script. It is very cool. :) Supports attachments and lots of other cool stuf..

 

Or maybe this can help you

 

http://webcheatsheet.com/php/send_email_text_html_attachment.php

The section at that link called "Sending Email with Attachment" is how you send a file attachment. You must create a multipart/mixed mime message with mime boundaries and then output the correct headers followed by the chunk_split/base64_encode contents of the file in its' section of the message.

 

The phpmailer/swiftmailer classes takes care of doing all that for you when you use their attachment methods.

mm ok I have it working 95% can anyone see what my problem is. The message sends is received ok has the correct subject file attachment logo and html format message but it keeps cutting off the htm message part after the first </table> tag the bottom disclaimer doesn't come through which starts with This message, and any attachments,?

 

<? 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$subject		= ($_POST['subject']);
$firstname 		= ($_POST["firstname"]);
$lastname 		= ($_POST["lastname"]);
$email		= ($_POST['email']);
$telephone		= ($_POST["telephone"]);
$country		= ($_POST["country"]);
$position		= ($_POST["position"]);
$message		= ($_POST['message']);
$random		= ($_POST['random']);
$validate		= ($_POST['validate']);
      $fileatt      = $_FILES['fileatt']['tmp_name'];
      $fileatt_type = $_FILES['fileatt']['type'];
      $fileatt_name = $_FILES['fileatt']['name'];


if ($random==$validate)

{
  
header("Location:thankyou.php?message=careers");




if (is_uploaded_file($fileatt)) {
   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$to = "[email protected]";
$headers = "From: [email protected]";  
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";

$message .= "\n\n";
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message .= "
<html>
<head>
<title>HTML message</title>
</head>
<body>
<table>
<tr><td align='left'><img src='http://www.website.com/pix/logo.gif'></td></tr>
</table>
<table style='padding-left:20px'>
<tr><td> </td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>FIRSTNAME: $firstname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>LASTNAME: $lastname</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>EMAIL: $email</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>TELEPHONE: $telephone</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>COUNTRY: $country</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>POSITION: $position</font></td></tr>
<tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>REASON: $message</font></font></td></tr>
<tr><td> </td></tr>
</table>
<table>
<tr><td><font style='font-size: 10px' style='font-family: Tahoma, Arial'>This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. 
If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. 
If you have received this communication in error, please destroy this message. 
Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity. 
</font></td></tr>
</table>
</body>
</html>";


$data = chunk_split(base64_encode($data));


$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: {$fileatt_type};\n";
$message .= " name=\"{$fileatt_name}\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"{$fileatt_name}\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";


}


mail($to, $subject, $message, $headers);



	exit;
}

else

{


	header("Location:thankyou.php?message=invalid");
	exit;
}
?>

got it working now with or without attachments but is there anything i can add to this to disallow .exe files being attached?

 

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$country = $_POST['country'];
$position = $_POST['position'];
$textmessage = $_POST['message'];
$ipaddress = $_POST['ipaddress'];
$random = $_POST['random'];
$validate = $_POST['validate'];
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

if($random == $validate)
{

        $to = '[email protected]';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers = "From: [email protected]\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "<html>
<head>
<title>HTML message</title>
</head>
<body>
<table>
  <tr><td><img src=\"http://www.website.com/pix/logo.gif\"></td></tr>
</table>
<table style=\"padding-left: 20px;\">
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">FIRSTNAME: $firstname</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">LASTNAME: $lastname</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">EMAIL: $email</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">TELEPHONE: $telephone</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">COUNTRY: $country</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">POSITION: $position</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">REASON: $textmessage</td></tr>
  <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">IP ADDRESS: $ipaddress</td></tr>
</table>
<table>
  <tr><td style=\"font-size: 10px; font-family: tahoma, san-serif;\">This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. 
If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. 
If you have received this communication in error, please destroy this message. 
Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity.</td></tr>
</table>
</body>
</html>
";

    if(is_uploaded_file($fileatt))
    {
        // attachment
        $fp = fopen($fileatt, 'rb');
        $data = fread($fp, filesize($fileatt));
        fclose($fp);

        $data = chunk_split(base64_encode($data));
        $message .= "--{$boundary}\n";
        $message .= "Content-Type: $fileatt_type;\n name=\"$fileatt_name\"\n";
        //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name\"\n";
        $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n";
        $message .= "--{$boundary}--\n";
     }
        mail($to, $subject, $message, $headers);
        header("Location: thankyou.php?message=careersyes");
        exit;
    
}
else
{
    header("Location: thankyou.php?message=careersno");
    exit;
}
?> 

This is how I did it on my site:

 

$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n";

// Additional headers
$headers .= 'To: Billy Bob <'.$_POST['to'].'>' . "\r\n";
$headers .= 'From: Jimmy John <[email protected]>' . "\r\n";

// File Info
$message = $_POST['message']."\n\n".
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n"; 
$fileList = '';
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"][$key];
$name = $_FILES["file"]["name"][$key];
$fName = "../attach/".md5(uniqid(rand(), false)).$name;
move_uploaded_file($tmp_name, $fName);

$fileatt = $fName; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = $name; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file); 

$data = chunk_split(base64_encode($data));

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
}
mail($_POST['to'],$_POST['subject'],$message,$headers);
header('Location: /mail?action=inbox');
exit;

Now it checks (it is the second if statement):

 

Edit (forgot a parenthesis):

 

$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n";

// Additional headers
$headers .= 'To: Billy Bob <'.$_POST['to'].'>' . "\r\n";
$headers .= 'From: Jimmy John <[email protected]>' . "\r\n";

// File Info
$message = $_POST['message']."\n\n".
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n"; 
$fileList = '';
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
	$tmp_name = $_FILES["file"]["tmp_name"][$key];
	$name = $_FILES["file"]["name"][$key];
	$fName = "../attach/".md5(uniqid(rand(), false)).$name;
	move_uploaded_file($tmp_name, $fName);
	$path_parts = pathinfo($fName);
	if($path_parts['extension'] != 'exe'){   // Check for extension type

		$fileatt = $fName; // Path to the file
		$fileatt_type = "application/octet-stream"; // File Type
		$fileatt_name = $name; // Filename that will be used for the file as the attachment

		$file = fopen($fileatt,'rb');
		$data = fread($file,filesize($fileatt));
		fclose($file); 

		$data = chunk_split(base64_encode($data));

		$message .= "--{$mime_boundary}\n" .
		"Content-Type: {$fileatt_type};\n" .
		" name=\"{$fileatt_name}\"\n" .
		//"Content-Disposition: attachment;\n" .
		//" filename=\"{$fileatt_name}\"\n" .
		"Content-Transfer-Encoding: base64\n\n" .
		$data . "\n\n" .
		"--{$mime_boundary}\n";
		unset($data);
		unset($file);
		unset($fileatt);
		unset($fileatt_type);
		unset($fileatt_name);
	}else{
		unlink($fName);  // Remove file if it is an exe
	}
}
}
mail($_POST['to'],$_POST['subject'],$message,$headers);
header('Location: /mail?action=inbox');
exit;

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.