My code here is:
<form action="#" method="POST" enctype="multipart/form-data" >
<input type="file" name="csv_file[]" />
<br/>
<input type="file" name="csv_file[]" />
<br/>
<input type="file" name="csv_file[]" />
<br/>
<input type="submit" name="upload" value="Upload" />
<br/>
</form>
<?php
if($_POST) {
for($i=0; $i < count($_FILES['csv_file']['name']); $i++){
$ftype[] = $_FILES['csv_file']['type'][$i];
$fname[] = $_FILES['csv_file']['name'][$i];
}
// array with filenames to be sent as attachment
$files = $fname;
// email fields: to, from, subject, and so on
$to = "example#gmail.com";
$from = "example#gmail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
I got the code from here: https://stackoverflow.com/a/27565430/4357238
the form successfully sends the email and when i get the email, the name of the file(s) show up but the file is an empty file (0 bytes).
For example, i send a file called car.jpg and when it gets to my email, it shows up on my email as car.jpg but when i download the file and open the file, it says the file is broken and it cannot be opened because the file is empty.
what is wrong and how can i fix it?
Related
This is my code, in this case, the code works perfectly and I got the attachment in the mail but the attached file can't be opened. Then I downloaded the file and tried to open it, it shows an error "either not a supported file type or file has been damaged(For example, it was send as an email attachment and wasn't correctly decoded)"
<form action="#" method="POST" enctype="multipart/form-data" >
<input type="file" name="csv_file[]" />
<br/>
<input type="file" name="csv_file[]" />
<br/>
<input type="file" name="csv_file[]" />
<br/>
<input type="submit" name="upload" value="Upload" />
<br/>
</form>
<?php
if($_POST) {
for($i=0; $i < count($_FILES['csv_file']['name']); $i++){
$ftype[] = $_FILES['csv_file']['type'][$i];
$fname[] = $_FILES['csv_file']['name'][$i];
}
// array with filenames to be sent as attachment
$files = $fname;
// email fields: to, from, subject, and so on
$to = "example#gmail.com";
$from = "example#gmail.com";
$subject ="My subject";
$message = "My message1";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n $data \n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// we'll begin by assigning the To address and message subject
$to="example#gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message="This is an example";
$message .= "Name:".$_POST["fromname"]."Message Posted:".$_POST["modlist"];
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1">
<p>Your name: <input type="text" name="fromname"></p>
<p>Your e-mail: <input type="text" name="fromemail"></p>
<p>Mod List: <textarea name="question" maxlength="1000" cols="25" rows="6" name="modlist"></textarea>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p>File: <input type="file" name="file3"></p>
<p>File: <input type="file" name="file4"></p>
<p>File: <input type="file" name="file5"></p>
<p>File: <input type="file" name="file6"></p>
<p>File: <input type="file" name="file7"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>
I am sending attachments by uploading through <input type="file" /> but now I want to know if we can send attachments just using URL of a file?
Like if I add <input type="url" name="file[]" /> and I need to send this as attachment not in body as inline link in message.
For now I am using this code:
HTML:
<form method="post" action="code.php" enctype="multipart/form-data">
<input type="file" name="file[]" multiple="multiiple" />
<button type="submit">Submit</button>
</form>
PHP:
<?php
foreach(array_combine($_FILES['file']['tmp_name'], $_FILES['file']['name']) as $tmp_name => $file_name ) {
$filetmp[] = $tmp_name;
$filename[] = $file_name;
}
// email fields: to, from, subject, and so on
$to = "receiver_email";
$from = "sender_email";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($filetmp);$x++){
$file = fopen($filetmp[$x],"rb");
$data = fread($file,filesize($filetmp[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$filename[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filename[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
mail($to, $subject, $message, $headers);
?>
Thanks
I have created two scripts for this HTML and a corresponding PHP.
HTML markup:
<html>
<body>
<form action="http://senindiaonline.in/admar/non-series-numbers-db.php" method="post" target="_blank" name="non_series_numbers_db" enctype="multipart/form-data">
<label for="emp-id">Employee ID: </label>
<input type="text" name="emp-id" id="emp-id" maxlength="15" style='text-transform:uppercase'/>
<br/>
<label for="no-csv">If you have a mobile number database in CSV file format upload that here: </label>
<input name="no-csv" id="no-csv" type="file" accept=".csv"/>
<br/>
<label for="no-xls">If you have a mobile number database in XLS (excel) file format upload that here: </label>
<input name="no-xls" id="no-xls" type="file" accept=".xls"/>
<br/>
<label for="no-xlsx">If you have a mobile number database in XLSX (excel) file format upload that here: </label>
<input name="no-xlsx" id="no-xlsx" type="file" accept=".xlsx"/>
<br/>
<input name="send" value="Send" type="submit" id="send"/>
</form>
</body>
</html>
PHP script:
<?php
error_reporting(0);
$email_to = 'senindiagroup#gmail.com';
$email_subject = "Non-Seriesed Mobile Number Database from Ad&Marketing Employee Panel of Sen India Online inc.";
$emp_id = $_POST['emp-id'];
$sub="Employee ID: $emp_id";
$bound_text = "jimmyPI23";
$bound = "--" .$bound_text. "\r\n";
$bound_last = "--" .$bound_text. "--\r\n";
$email_message .= "If you can see this MIME than your client doesn't accept MIME types! \r\n" .$bound;
$email_message .= "Content-Type: text/html; Charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n" ."$sub\r\n" .$bound;
//getting temporary file location with name
$a=file_get_contents($_FILES['no-csv']['tmp_name']);
$b=file_get_contents($_FILES['no-xls']['tmp_name']);
$c=file_get_contents($_FILES['no-xlsx']['tmp_name']);
//getting original name of the file you uploaded
$name=$_FILES["no-csv"]["name"];
$name1=$_FILES["no-xls"]["name"];
$name2=$_FILES["no-xlsx"]["name"];
$email_message .= "Content-Type: {\"application/octet-stream\"}; \n" . "name=\"$name\ "\n" . "Content-Transfer-Encoding: base64\r\n" ."Content-Disposition: attachment; \n" . " filename=\"$name\ "\n"."\r\n" .chunk_split(base64_encode($a)).$bound;
mail($email_to, $email_subject, $email_message, $headers);
?>
The error is:
syntax error showing on line 31, attach file with mail command.
And the mail is not send. I solve this error with DreamWeaver suggestion. Then the mail is send, but the attachment is not included in the mail.
This is the example of sending multiple attachments in mail
<?php
for($i=0; $i < count($_FILES['csv_file']['name']); $i++){
$ftype[] = $_FILES['csv_file']['type'][$i];
$fname[] = $_FILES['csv_file']['name'][$i];
}
// array with filenames to be sent as attachment
$files = $fname;
// email fields: to, from, subject, and so on
$to = "example#gmail.com";
$from = "example#gmail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>
if you want simple solution use phpmailer library, that would be easy to use,easy to configure.
Hi everybody I'm searching to solve a problem (looks easy but I don't find a solution).
I have a form HTML and I'm trying to send datas and files to a specific email.
The email arrives correctly but the 2 file attached have a strange name, it look like a temporary name file. It like I lost the name of the file and the extension.
Somebody could help me to understand what I'm doing wrong?
I'm not really an expert on PHP.
Thanks!
HTML file
<form id="form1" name="form1" method="post" action="email2.php" enctype="multipart/form-data">
<fieldset>
<legend>Canditatura</legend>
<label>Nome:</label> <input type="text" placeholder="Inserisci il tuo nome" name="nome" size="30px" required="true"><br>
<label>Cognome:</label> <input type="text" placeholder="Inserisci il tuo cognome" name="cognome" size="30px" required="true"><br>
<label>Email:</label> <input type="email" name="email" placeholder="example#example.it" size="30px" required="true"><br>
<label>Telefono:</label> <input type="tel" name="telefono" placeholder="+39" size="30px"><br>
<label>Allegato 1:</label> <input type="file" id="allegato" name="allegato" required="true"><br>
<label>Allegato 2:</label> <input type="file" id="allegato2" name="allegato2" required="true"><br><br><br>
<label class="lprivacy"><input type="checkbox" required="true">Accetto normativa sulla Privacy</label><input type="submit" value="Invia Canditatura" name="submit">
</fieldset>
</form>
PHP File
<?php
$allegato = $_FILES['allegato']['tmp_name'];
$allegato_type = $_FILES['allegato']['type'];
$allegato_name = $_FILES['allegato']['name'];
$allegato2 = $_FILES['allegato2']['tmp_name'];
$allegato2_type = $_FILES['allegato2']['type'];
$allegato2_name = $_FILES['allegato2']['name'];
$email = $_POST['email'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$telefono = $_POST['telefono'];
mail_attachment("test#test.com","Subject","Nuova canditatuda da <b>$nome $cognome</b>. <br> Telefono: $telefono <br> Email: $email ",array("$allegato","$allegato2"));
function mail_attachment($to, $subject, $message, $files) {
$headers = "From: no-reply#mail.com";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
foreach ($files as $file) {
$filename = end(explode("/",$file));
$data = file_get_contents($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$file\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
echo (#mail($to, $subject, $message, $headers)) ? "<p>Messaggio spedito correttamente a $to!</p>" : "<p>ERRORE! Messaggio non spedito a $to!</p>";
} // mail-attachment ?>
According to your code, your sending temp file but your not sending the original file name, so that's the received file name has some random file name.
Check bellow code
<?php
$email = $_POST['email'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$telefono = $_POST['telefono'];
mail_attachment("test#test.com","Subject","Nuova canditatuda da <b>$nome $cognome</b>. <br> Telefono: $telefono <br> Email: $email ",array("allegato","allegato2"));
function mail_attachment($to, $subject, $message, $files) {
$headers = "From: no-reply#mail.com";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
foreach ($files as $f) {
$file = $_FILES[$f]['tmp_name'];
$filename = $_FILES[$f]['name'];;
$data = file_get_contents($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$file\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
echo (#mail($to, $subject, $message, $headers)) ? "<p>Messaggio spedito correttamente a $to!</p>" : "<p>ERRORE! Messaggio non spedito a $to!</p>";
} // mail-attachment ?>
Instead of passing file uploaded paths, I passing HTML elements to the function, so that it will fetch necessary Information and attach file(s) and it will send mail.
I have been trying to get an email form that will send emails with attachments to email adresses which are stored in a database. I already have a script that will send an email to the email adresses in my database and I have a script that sends 1 attachment to 1 email and both of them work fine. The problem however is that I can't get them combined into 1 script. I have tried to combine the 2 into 1 many times but I can seem to figure it out. Since I'm still a student I am still learning how to do these things.
I will post the code I have below to show you what I have so far. If anyone has any tips or could show me how to do it it would be very helpful.
config.php
<?php
$server="localhost";
$database="NAW";
$db_user="root";
$db_pass="something";
$fromadmin="test#test.com";
$table="Email";
$table_email="Email";
?>
Send mails to email adresses in database:
<?php
include "header.php";
include "config.php";
if( $_POST || $_FILES )
{
$seconds=$_POST['seconds'];
$subject=$_POST['subj'];
$messagesend=$_POST['message'];
mysql_connect($server, $db_user, $db_pass)
or die ("Database CONNECT Error");
$resultquery = mysql_db_query($database, "select * from $table");
while ($query = mysql_fetch_array($resultquery))
{
$emailinfo=$myemail;
$mailto=$query[$table_email];
mail($mailto, $subject, $messagesend , "From:".$fromadmin."\nReply-To:".$fromadmin."\n");
echo 'Mail sent to '.$mailto.'<br>';
sleep($seconds);
}
echo 'Mails sent. Go Back';
}
else
{
?>
<table height="250" cellpadding="1">
<tr><td valign="top">
<h2>Mail Sender</h2><br><form action="massmail.php" method="POST">
<div align="center">
<table cellpadding="0" border="0" align="left">
<tr>
<td>
Subject:
</td>
<td>
<input type="text" align="left" name="subj" size="66">
</td>
</tr>
<tr><td align="left" valign="top">
Message Text:</td><td align="left"> <textarea name="message" rows="15" cols="60" ></textarea></td></tr>
<tr>
<tr><td colspan="2" align="left">
Seconds between messages:<input type="text" size="10" name="seconds" value="0.1"> (seconds)
</td></tr>
<tr><td colspan="2">
<input type="submit" value="Send mass mails" name="submit" >
</Td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<?php
}
include "footer.php";
?>
Send a mail with an attachment:
<?php
if( $_POST || $_FILES )
{
// email fields: to, from, subject, and so on
// Here
$from = "test#test.com";
$to = "test2#test.com";
$subject = "Mail with Attachment";
$message = "This is the message body and to it I will append the attachments.";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
fixFilesArray($_FILES['attachment']);
foreach ($_FILES['attachment'] as $position => $file)
{
// should output array with indices name, type, tmp_name, error, size
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file['tmp_name'],"rb");
$data = #fread($fp,filesize($file['tmp_name']));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return 1; } else { return 0; }
}
//This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
?>
<html>
<body>
<form method="POST" action="bijlagetest.php" enctype="multipart/form-data">
<input type="file" name="attachment[]"><br/>
<input type="submit">
</form>
</body>
</html>
Ok to attach Files the User first needs an Upload-Form
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
to save the File in PHP do something like this
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
So now you have the Files to send as Attachment, rewrite your script to a function that can handle multiple attachments...
something like this should work...
<?php
function sendMail($to, $from, $subject, $message, $attachments){
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($attachments);$x++){
$file = fopen($attachments[$x],"rb");
$data = fread($file,attachmentsize($attachments[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$attachments[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$attachments[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
I have not tested this code but it should push you in the right direction ;-)
Try This,
First create one folder temp.
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
//Database credentials
$mysql_db_hostname = "localhost";
$mysql_db_user = "root";
$mysql_db_password = "";
$mysql_db_database = "reassurance";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$filename='temp/test.csv';
//$filename='temp/'.$test.'.csv';
$fp=fopen($filename,"w");
$sql = mysql_query("SELECT * FROM `contactus`");
$row=mysql_fetch_assoc($sql);
$seperator="";
$comma="";
foreach($row as $name =>$value)
{
$seperator.=$comma.''.str_replace('','""',$name);
$comma=",";
}
$seperator.="\n";
$seperator;
fputs($fp,$seperator);
mysql_data_seek($sql,0);
while($row=mysql_fetch_assoc($sql))
{
$seperator="";
$comma="";
foreach($row as $name =>$value)
{
$seperator.=$comma.''.str_replace('','""',$value);
$comma=",";
}
$seperator.="\n";
fputs($fp,$seperator);
}
fclose($fp);
$my_file ='test.csv';
$path = "temp/";
$from_name = "Hara Prasad Hota";
$from_mail = "haraprasad#lemonpeak.com";
$mailto = "haraprasad#lemonpeak.com";
$subject = "This is a mail with attachment.";
$message = "Hallo,\r\n do you got attachment? I hope it is working.\r\n\r\Hara";
$replyto="haraprasad#lemonpeak.com";
$file = $path.$my_file;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, "", $header)
?>