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/
Related
I have this problem with PHP mail function:
I'm trying to sent an html email with a PDF file in attachment, the file is stored in a folder of mywebsite and created from me with mpdf, but when I send it the mail received has an attachment with size 0b.
this is the code:
<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
// File Exists
$size = filesize($attachment);
if( $size > 0 ){
//Alternative 1
$file = fopen($attachment,'rb');
$content = fread($file, $size);
fclose($file);
$content = chunk_split(base64_encode($content));
//Alternative 1
//$content = chunk_split(base64_encode(file_get_contents($attachment)));
$mailto = "example#maito.com";
$from_name = "MyDomainName";
$from_mail = "example#mydomain.com";
$replyto = "example#mydomain.com";
$uid = md5(uniqid(time()));
$subject = "e-mail subject here";
$message = "HTML MESSAGE HERE" ;
$filename = "file.pdf";
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "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 .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; \r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--PHP-alt-".$uid."--";
if( #mail($mailto, $subject, "", $header) ){
echo "Mail SENT";
}else{
echo "ERROR - Mail error";
}
}else{
echo "ERROR- File size = 0";
}
}else{
echo "ERROR - File doesn't exist";
}
?>
The mail is sent correctly, so the file exist and its size is greater than 0b.
But when I receive the email in my emailbox it's all correct instead of the attachment that is present but empty.
I tryed both the 2 alternative of extracting the file content inserted in the code, but the result is the same.
Someone could help me?
I changed some headers configs. Try this:
<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
// File Exists
$size = filesize($attachment);
if( $size > 0 ){
//Alternative 1
$file = fopen($attachment,'rb');
$content = fread($file, $size);
fclose($file);
$content = chunk_split(base64_encode($content));
//Alternative 1
//$content = chunk_split(base64_encode(file_get_contents($attachment)));
$mailto = "example#maito.com";
$from_name = "MyDomainName";
$from_mail = "example#mydomain.com";
$replyto = "example#mydomain.com";
$uid = md5(uniqid(time()));
$subject = "e-mail subject here";
$message = "HTML MESSAGE HERE" ;
$filename = "file.pdf";
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "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 .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--PHP-alt-".$uid."--";
if( #mail($mailto, $subject, "", $header) ){
echo "Mail SENT";
}else{
echo "ERROR - Mail error";
}
}else{
echo "ERROR- File size = 0";
}
}else{
echo "ERROR - File doesn't exist";
}
?>
Seen that I didn't receive answers, I only found one way to do that without problems, this way is to use PHPMailar class.
Thanx to the discussion:
Send attachments with PHP Mail()?
I am using following code to send form data to email along with attachment but it only send attachment not data filled in form.
I also want to set the upload limit to 4mb
Any idea which could help me?
<?php
if(isset($_POST['submit']))
{
$name1=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mob'];
$applyfor= $_POST['applyfor'];
$address= $_POST['Address'];
if(is_uploaded_file($_FILES['resume']['tmp_name']))
{
$path = $_FILES['resume']['tmp_name'];
$filename = $_FILES['resume']['name'];
$file = $path;
$file_size = $_FILES['resume']['size'];
$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: $name1<$email>"."\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."--";
$message = $name1."\n" .$email."\n" .$mobile."\n" .$applyfor."\n" .$address;
$mailto = "uttamking#gmail.com";
$subject = "New resume Receive";
if (mail($mailto, $subject, $message, '', $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
}
?>
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 writte this code to send multiple attachments:
$tablica_plikow=$_FILES["file"]; //array of files
if(!empty($tablica_plikow['name'])){///if attachment
$uid = md5(uniqid(time()));
$header = "From: od\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-2\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .=win2iso( $_POST['tresc'])."\r\n\r\n\r\n\r\n\r\n\r\n";///message
$header .= "--".$uid."\r\n";
for($i=0; $i<count($tablica_plikow['name']); $i++){
if ($tablica_plikow["error"][$i] > 0)
{ $komunikat = "<img src=\"img_panel/bttn_error.gif\">"."Return Code: " . $tablica_plikow["error"][$i] ;
}
if (file_exists("zalacznik/" . $tablica_plikow["name"][$i]))
{
$komunikat = "<img src=\"img_panel/bttn_error.gif\">"."Return Code: " . $tablica_plikow["name"][$i]. " already exists. " ;
}
else
{
if(is_uploaded_file($tablica_plikow["tmp_name"][$i])) {
move_uploaded_file($tablica_plikow["tmp_name"][$i],
"zalacznik/" . $tablica_plikow["name"][$i]);
$komunikat = "<img src=\"img_panel/bttn_info.gif\">" . "zalacznik/" . $tablica_plikow["name"][$i];
$target_path="zalacznik/" . $tablica_plikow["name"][$i];
$file = "zalacznik/".$tablica_plikow["name"][$i];
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$name = basename($file);
$header .= "Content-Type: ".$tablica_plikow["type"][$i]." name=\"".$tablica_plikow["name"][$i]."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\n\r".$content."\r\n\r\n";;
$header .= "Content-Disposition: attachment; filename=\"".$tablica_plikow["name"][$i]."\"\r\n\r\n";
// $header .= $content."\r\n\r\n";
$header .= "--".$uid."-- \r\n";
}
}
}
if (mail("mail#moj.com", $_POST['tytul'], "", $header)) {
$komunikat = "<img src=\"img_panel/bttn_info.gif\">mail send";
} else {
$komunikat = "<img src=\"img_panel/bttn_error.gif\">error";
}
But when I send an email with two or more attachments, receive only one file.This file is a concatenation of all attachments. But the first part of the file is the first attachment, and other parts are just hashmap other files.
I would use a library eg.
Zend_Mail
Pear Mail_Mime
I'd like to advise you to use PHP Mailer.
I advice you use standard and tested mailing lib such as phpMail
It has been discussed here before Please see regarding email with attachment in php
Thanks
:)