php send html email with .csv attachment - php

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

Related

Send email attachment received blank document

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.

email shows html tags it`s not appear in html format

I am sending email using editor but it`s also send the html tags and i want to show only html view and This is my email attachment code --
$file = 'uploads/email_attachments/'.$row['email_image'];
$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()));
$from_name = "Tino Cesar";
$from_mail = "info#tino.co.in";
$replyto = $row['email'];
$message = $row['message'];
$header = "From: ".$name." <".$email.">\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";
$headers .= "Content-type:text/html;charset=UTF-8" . "\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."--";
$mailto = $row['email'];
$subject = $row['subject'];
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
change this
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
to
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
remove this line
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";

Creating excel from mysql and sending this as attachment email in php

I need to send an email with attachment, and that attachment file contain some data fetched from mysql database at the same time.
That problem is already asked and described here but there is no any working answer.
Can anyone have solution, than please answer.
while( $row = mysql_fetch_row( $sqlQuery ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
$cho = "$header\n$data";
echo $cho;
$headers = "From:abcdf.k#gmail.com(PFC Web Admin) \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: application/vnd.ms-excel";
$headers .= 'Content-Disposition: attachment; filename=Test.xls' . "\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "".$cho."\r\n";
$subject = 'Record November';
$mail = mail( 'abc.k#gmail.com', $subject, $msg, $headers );
if( $mail == true )
{
echo "Message successfully sent";
}
else
{
echo "Message could not be sent";
}
Excel is creating and have proper data but I need to mail this rather than download.
Try this,it's working
<?php
include_once('inc/dbConnect.inc.php');
error_reporting(E_ERROR);
$sql = mysql_query("SELECT * FROM tablename");
$row=mysql_fetch_assoc($sql);
$filename='temp/'.$filename.'.csv';
$fp=fopen($filename,"w");
$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 = $filename.'.csv';
$path = "temp/";
$from_name = "hhhhh";
$from_mail = "abc#gmail.com";
$mailto = "abc#gmail.com";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\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)
?>
I don't see any attempt in your code to make a Excel file or an email with an attachment. I see those as two seperate questions.
When it comes to email attachments, it's best to use something that already exists, like:
http://swiftmailer.org
or
https://github.com/Synchro/PHPMailer
(order does not suggest anything)
For Excel the same thing applies, because with the CSV or XML versions of Excel files I always have problems. I have no suggestions though. I did find this more general site:
http://webdeveloperplus.com/php/5-libraries-to-generate-excel-reports-in-php/

Unable to send html mail with an attachment

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;
}
}

redirect after form submission PHP

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;
}
?>

Categories