I am trying to redirect the user after they enter their email. I am able to send the email with the attachment but not able to redirect the user to thankyou.html. If I uncomment the hardcoded value and use that the redirect is working fine. Not sure what I am doing wrong. Any help is appreciated.
Here is my php file:
<?php
//form variables
$to = $_POST['userEmail'];
$user_name = $_POST['userName'];
$subject = $_POST['subject'];
$fname = $_POST['filename'];
$mailcopyfile = $_POST['backupfile'];
//$subject = 'Templage from us';
//$fname = 'somefile.xlsx';
//$mailcopyfile = 'somefile.csv';
// common for all landing page emails
$from_name = 'MyCompany LLC';
$from_mail = 'admin#mycompany.com';
$now = date('Y-m-d H:i:s');
$num = md5(time());
$message = "<html><body>";
$message .= "<span>Dear " . $user_name . ",</span>";
$message .= "<table><tr><td>Thank you for your interest in our company.</td></tr>";
$message .= "<tr><td>Here is the template you requested.</td></tr>";
$message .= "<tr><td> </td></tr>";
$message .= "<tr><td>Regards,</td></tr>";
$message .= "<tr><td>Our Team</td></tr>";
$message .= "</table>";
$message .= '<img src="http://mycompany.com/Images/logo.jpg" alt="MyCompany LLC">';
$message .= "</body></html>";
//File open
$fp = fopen($fname, "rb");
$upload_size=filesize($fname);
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
fclose($fp);
$upload_type=filetype($fname);
//Normal headers
$headers = "From: ".$from_name." <".$from_mail.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--" .$num. "\r\n";
// With message
$headers .= "Content-Type: multipart/alternative; boundary=";
$headers .= $num. "\r\n";
$headers .= "--" .$num. "\r\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n";
$headers .= "Thanks for your interest in our company. Here is the template you requested. Thanks Our Team\r\n";
$headers .= "--" .$num. "\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= $message."\r\n\r\n";
$headers .= "--".$num."\r\n";
// Attachment headers
$headers .= "Content-Type:".$upload_type."; ";
$headers .= 'name="'.$fname.'"'. "\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= 'filename="'.basename($fname).'"' . "\r\n";
$headers .= $file."\r\n";
$headers .= "--".$num."--";
//send the email
$mail_sent = #mail( $_POST['userEmail'], $subject, "", $headers );
//$mail_sent = #mail( 'myemail#hotmail.com', $subject, "", $headers );
if($mail_sent){
$fp = fopen($mailcopyfile, "a");
fputs($fp, $now . "\t" . $user_name . "\t" . $_POST['userEmail'] . "\t\n" );
//fputs($fp, $now . "\t" . $user_name . "\t" . 'myemail#hotmail.com' . "\t\n" );
fclose($fp);
header('Location: Thankyou.html', true);
exit;
}
?>
Related
Please anyone help.
I am sending email with attachment in php. email is sent but we do not recieve the attachment as file but it is sent in form of encoded text...any suggestion and help ??
Here is my php code..
<?php
if(isset($_POST['submit']))
{
ob_start();
if(isset($_FILES['upload_cv']))
{
$file_tmp_name = $_FILES['upload_cv']['tmp_name'];
$file_name = $_FILES['upload_cv']['name'];
$file_size = $_FILES['upload_cv']['size'];
$file_type = $_FILES['upload_cv']['type'];
$file_error = $_FILES['upload_cv']['error'];
//$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode(file_get_contents(($content))));
}
$boundary = md5("ajantasoya");
$recipientEmail = "prg6#gmail.com";
//$senderEmail = $_POST['email'];
$senderEmail = "vipinmishra.alld#gmail.com";
$Subject = "Test";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit" . " \r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1 \r\n";
$headers .= "From: ".$senderEmail." \r\n";
$headers .= "Reply-To: ".$senderEmail." \r\n";
$headers .= "Subject:". $subject." \r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$headers .= "--" . $boundary . " \r\n";
$headers .= "Content-Type: text/HTML; charset=\"iso-8859-1\"" ." \r\n";
$headers .= "Content-Transfer-Encoding: 8bit" . " \r\n";
// Main message start
$msg_body .= "--" . $boundary . " \r\n";
$msg_body .= "Content-Type: text/HTML; charset=\"iso-8859-1\"" ." \r\n";
$msg_body .= "Content-Transfer-Encoding: 8bit" . " \r\n";
$msg_body = "Name: ".$_POST['name']. "\r\n";
$msg_body .= "Current Location: ".$_POST['location']. "\r\n";
$msg_body .= "Home town : ".$_POST['home-town']. "\r\n";
$msg_body .= "Marital Status: ".$_POST['mstatus']. "\r\n";
$msg_body .= "Children, if any: ".$_POST['num-ch']. "\r\n";
$msg_body .= "Position applying for : ".$_POST['pos-for']. "\r\n";
$msg_body .= "Date of Birth : ".$_POST['dob']. "\r\n";
$msg_body .= "Strengths: ".$_POST['strenght']. "\r\n";
$msg_body .= "Weaknesses: ".$_POST['weakness']. "\r\n";
//Attachment Part
$msg_body .= "--".$boundary."\r\n";
$msg_body .= "Content-Type: application/octet-stream; name=\"" . $file_name . "\r\n";
$msg_body .= "Content-Transfer-Encoding: base64 \r\n";
$msg_body .= "Content-Disposition: attachment \r\n";
//$msg_body .="Content-Type: ".$file_type. "; Name=".$file_name."\r\n";
//$msg_body .="Content-Type: ".$file_type. "; Name=".$file_name."\r\n";
//$msg_body .="Content-Disposition: Attachment; Filename=".$file_name."\r\n";
//$msg_body .="Content-Transfer-Encoding: base64\r\n";
$msg_body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$msg_body .= $encoded_content;
$message = $msg_body;
$send = #mail($recipientEmail, $Subject , $message, $headers);
if ($send) {
echo "Message sent!";
}
else {
echo "ERROR sending message.";
}
}
ob_end_flush();
?>
In the header, you have:
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\"\r\n";
So the boundary between the message and the attachment should be --PHP-mixed-$boundary. But when you encode the message body, you're sending:
$msg_body .= "--" . $boundary . " \r\n";
You're missing the PHP-mixed- prefix before $boundary. Change those lines to:
$msg_body .= "--PHP-mixed-" . $boundary . " \r\n";
Also, you're not specifying the filename in the correct place in the attachment header. You have it in the Content-type: line, it should be in Content-disposition:.
$msg_body .= "Content-Type: application/octet-stream \r\n";
$msg_body .= "Content-Transfer-Encoding: base64 \r\n";
$msg_body .= "Content-Disposition: attachment; filename=\"$file_name\" \r\n";
<?php
if(isset($_POST['submit']))
{
ob_start();
if(isset($_FILES['upload_cv']))
{
$file_tmp_name = $_FILES['upload_cv']['tmp_name'];
$file_name = $_FILES['upload_cv']['name'];
$file_size = $_FILES['upload_cv']['size'];
$file_type = $_FILES['upload_cv']['type'];
$file_error = $_FILES['upload_cv']['error'];
//$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("ajantasoya");
}
$recipientEmail = "prg6#gmail.com";
//$senderEmail = $_POST['email'];
$senderEmail = "prg5#gmail.com";
$Subject = "Test";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1 \r\n";
$headers .= "From: ".$senderEmail." \r\n";
$headers .= "Reply-To: ".$senderEmail." \r\n";
$headers .= "Subject:". $subject." \r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$msg_body = "Name: ".$_POST['name']. "\r\n";
$msg_body .= "Current Location: ".$_POST['location']. "\r\n";
$msg_body .= "Home town : ".$_POST['home-town']. "\r\n";
$msg_body .= "Marital Status: ".$_POST['mstatus']. "\r\n";
$msg_body .= "Children, if any: ".$_POST['num-ch']. "\r\n";
$msg_body .= "Position applying for : ".$_POST['pos-for']. "\r\n";
$msg_body .= "Date of Birth : ".$_POST['dob']. "\r\n";
$msg_body .= "Qualifications : \r\n \r\n";
$msg_body .= " Stream ";
$msg_body .= "Year of Completion ";
$msg_body .= "Percentage \r\n \r\n";
$msg_body .= "Others ";
$msg_body .= $_POST['other-e']." ";
$msg_body .= $_POST['other-y']." ";
$msg_body .= $_POST['other-p']. "\r\n";
$msg_body .= "Masters ";
$msg_body .= $_POST['master-e']." ";
$msg_body .= $_POST['master-y']." ";
$msg_body .= $_POST['master-p']. "\r\n";
$msg_body .= "Bachelors ";
$msg_body .= $_POST['bachlor-e']." ";
$msg_body .= $_POST['bachlor-y']." ";
$msg_body .= $_POST['bachlor-p']. "\r\n";
$msg_body .= "HSC ";
$msg_body .= $_POST['hse-e']." ";
$msg_body .= $_POST['hse-y']." ";
$msg_body .= $_POST['hse-p']. "\r\n";
$msg_body .= "SSC ";
$msg_body .= $_POST['ssc-e']." ";
$msg_body .= $_POST['ssc-y']." ";
$msg_body .= $_POST['ssc-p']. "\r\n";
$tot_emp_details = sizeof($_POST['cname']);
//echo $tot_emp;
$msg_body .= "Strengths: ".$_POST['strenght']. "\r\n";
$msg_body .= "Weaknesses: ".$_POST['weakness']. "\r\n";
$msg_body .= "Where do you see yourself in 5 years?: ".$_POST['next-5-year']. "\r\n";
$msg_body .= "Where do you see yourself in 10 years?: ".$_POST['next-10-year']. "\r\n";
$msg_body .= "What is your eventual career goal?: ".$_POST['career-goal']. "\r\n";
$msg_body .= "Computer skills: ".$_POST['computer']. "\r\n";
$msg_body .= "Languages: ".$_POST['language']. "\r\n";
$msg_body .= "Others, if any: ".$_POST['other-any']. "\r\n";
//Attachment Part
$msg_body .= "--".$boundary."\r\n";
//$msg_body .="Content-Type: ".$file_type. "; Name=".$file_name."\r\n";
$msg_body .= "Content-Type: application/octet-stream \r\n";
$msg_body .="Content-Disposition: Attachment; filename=".$file_name."\r\n";
$msg_body .="Content-Transfer-Encoding: base64\r\n";
$msg_body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$msg_body .= $encoded_content;
$message = $msg_body;
$send = #mail($recipientEmail, $Subject , $message, $headers);
if ($send) {
echo "Message sent!";
}
else {
echo "ERROR sending message.";
}
}
ob_end_flush();
?>
This is roughly how it should look like. The parts you might be missing **
$eol = "\n";
// main header (multipart mandatory)
$headers = "From: $form". $eol;
$headers .= "MIME-Version: 1.0" . $eol;
**$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";**
// message
$body .= "--$boundary" . $eol;
$body .= "Content-Transfer-Encoding: 7bit" . $eol.$eol;
$body .= "This is a MIME encoded message." . $eol;
$body .= "--".$boundary.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
//attachment
$body .= "--$boundary" . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment". $eol.$eol;
$body .= $content . $eol;
**$body .= "--$boundary--";**
mail('email', 'subject', $body, $headers)
I try to add send email attachment using php,file attachment working fine but attachment file open blank document.How to solve this issue.Below mentioned my code.
$from_email = 'sender_mail#example.com'; //sender email
$recipient_email = 'manosk24#gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
$filename = "file1.pdf";
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$boundary = md5(uniqid(time()));
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $user_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/pdf; name=" . $filename . "\r\n";
$body .="Content-Disposition: attachment; filename=" . $filename . "\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
Sorry for my spelling mistake..
try this code,
$filename = "file1.pdf";
$file = $path . "/" . $filename;
$message ="my message";
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: name <test#test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
$headers .= $message . $eol;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol;
$headers .= $content . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, "", $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
i hope it will be helpful.
Php code used:
$boundary = uniqid("np");
$header = "MIME-Version: 1.0\r\n" ;
$header .= "From: $from\r\n";
$header .= "To: $to\r\n";
if (!is_null($reply_to)) $header .= "Reply-To: $reply_to \r\n";
$header .= "Content-Type: multipart/mixed;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a multi part message in MIME format.\n\n";
$message .= "--" . $boundary . "\n";
$message .= "Content-type: text/html;charset=utf-8\n\n";
$message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
$message .= "<BR>Regards";
$message .= "<BR>Your Name";
$message .= "\n\n--" . $boundary . "\n";
$file_name = "file_01.txt";
$file_content = "Ciao - file 1";
$chunked_content = chunk_split(base64_encode($file_content));
$message .= "Content-Type: application/octet-stream; name=\"$file_name\"\n";
$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n";
$message .= "Content-Transfer-Encoding: base64\n\\n" . $chunked_content . "\n\n";
$message .= "--{$boundary}--";
Results: HTML Mail IS OK, there is the attached file with right filename but it's empty
What's wrong ?
Edit: ! tried to replace every \n with \r\n but the results is an empty mail..
I use the following code for email attachments. Try this
function mailWithAttachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$file = $path.$filename;
$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.">\n";
$header .= "Reply-To: ".$replyto."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/html; charset=iso-8859-1\n";
$header .= $message."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";
if(mail($mailto, $subject, "", $header))
{
return 1;
}
else
{
return 0;
}
}
I am sending an email through php no problem. I want to attach a .csv file along with the HTML email provided. I can't seem to find how to do that. Is there a certain Content-Type I also have to include in the $headers along with a Content-Disposition of attachment?
$to = 'email#email.com';
$subject = 'A Subject Line';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "
<html>
<head>
<title>List of New Price Changes</title>
</head>
<body>";
$message .="HTML table";
$message .="</body></html>";
mail($to, $subject, $message, $headers);
$fileatt_type = "text/csv";
$myfile = "myfile.csv";
$file_size = filesize($myfile);
$handle = fopen($myfile, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$message = "<html>
<head>
<title>List of New Price Changes</title>
</head>
<body><table><tr><td>MAKE</td></tr></table></body></html>";
$uid = md5(uniqid(time()));
#$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/html; 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: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($to, $subject, $message, $header);
Try to modify the code for your situation.
You are confusing HTTP headers with mail.... You probably want to send a multipart messages, but instead of reimplementing it, I'd go for something like PHPMailer.
This code works for me. You can try this.
<?php
$con = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$con)
{
die("error" . mysqli_connect_error());
}
error_reporting(E_ERROR);
$filename = "adhaar_info";
$sql = mysqli_query($con, "SELECT * FROM adhaar_info order by id desc limit 0,10");
$row = mysqli_fetch_assoc($sql);
$filename2='datas/'.$filename.'.csv';
$fp = fopen($filename2, "w");
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $name);$comma = ",";}
$seperator .= "\n";
$seperator;
fputs($fp, $seperator);
mysqli_data_seek($sql, 0);
while ($row = mysqli_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 = $filename2;
$path = "datas/";
$from_name = "solomon";
$from_mail = "pss#gmail.com";
$mailto = "pssworkcse#gmail.com";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\r\n\r\Solomon";
$replyto = "pssworkcse#gmail.com";
$file = $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=\"" . $filename2 . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename2 . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
mail($mailto, $subject, "", $header)
?>
I am sending form data through a .swf file into this PHP page.(see below) When the email is sent, if there is an attachment the body of the message will not appear but if there is not an attachment the body appears just fine. I know that all of the variable names are correct because all of the data has been passed from the swf file. Is there something in the way I have set up the email that makes it work that way? Please let me know.
<?php
$to=$_POST['toEmail'];
$subject=$_POST['subject'];
$body = $_POST['messaggio'];
$nome = $_POST['nome'];
$email = $_POST['email'];
$attach = $_POST['attach'];
$headers = "From: $nome<" . $email . ">\n";
if ($attach == 1) {
$tmp_name = $_FILES['Filedata']['tmp_name'];
$type = $_FILES['Filedata']['type'];
$name = $_FILES['Filedata']['name'];
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
$headers .= "Reply-To: <" . $email . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDARY_main_message\"\n";
$headers .= "X-Sender: $to <" . $to . ">\n";
$headers .= "Return-Path: <" . $email . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDARY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDARY_message_parts\"\n";
$message = "------=MIME_BOUNDARY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= $body . "\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n";
$message .= $data;
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message--\n";
mail($to, $subject, $message, $headers);
}
} else {
if(mail($to, $subject, $body, $headers)) {
echo "ok=1";
}
}
?>
Building MIME messages, especially with attachments, is painful. You'd be better off using something like PHPMailer, which will handle the whole business for you automatically... You just have to provide the content.
Beyond that, you're slurping the attachments into memory. How big are they? Are you exceeding the script's memory_limit?