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 = "info@website.com";
$subject = "$subject";
$MsgHeader = "From: APA<noreply@website.com>\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
Share on other sites

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.

Link to comment
Share on other sites

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 = "info@website.com";
$headers = "From: noreply@website.com";  
$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;
}
?>

Link to comment
Share on other sites

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 = 'info@website.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers = "From: noreply@website.com\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;
}
?> 

Link to comment
Share on other sites

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 <jimmyjohn@site.com>' . "\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;

Link to comment
Share on other sites

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 <jimmyjohn@site.com>' . "\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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.