How to send a PHP exported Excel as a message
Now realize the click to download Excel, and the function of sending text messages, how to combine, the PHP exported Excel as an attachment to send the perfect.
1. Generate Excel:
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=". $filename. ". XLS ");
Header ("Pragma:no-cache");
Header ("expires:0");
if (!empty ($title)) {
foreach ($title as $k => $v) {
$title [$k]=iconv ("UTF-8", "GB2312", $v);
}
$title = implode ("T", $title);
echo "$titlen";
}
if (!empty ($data)) {
foreach ($data as $key => $val) {
foreach ($val as $ck => $CV) {
$data [$key] [$ck]=iconv ("UTF-8", "GB2312", $CV);
}
$data [$key]=implode ("T", $data [$key]);
}
echo Implode ("n", $data);
}
2. Send mail:
Using the Phpmailer class library
$mail = new Phpmailer ();
$mail->charset = ' UTF-8 ';
$mail->issmtp ();
$mail->smtpauth = true;
$mail->smtpsecure = ';
$mail->host = $config [' Smtp_host ']; SMTP Server
$mail->port = $config [' Smtp_port ']; Port number of the SMTP server
$mail->username = $config [' Smtp_user ']; SMTP Server user name
$mail->password = $config [' Smtp_pass ']; SMTP server password
$mail->setfrom ($config [' From_email '], $config [' from_name ']);
$replyEmail = $config [' Reply_email ']? $config [' Reply_email ']: $config [' from_email '];
$replyName = $config [' Reply_name ']? $config [' Reply_name ']: $config [' from_name '];
$mail->addreplyto ($replyEmail, $replyName);
$mail->subject = $subject;
$mail->msghtml ($body);
$mail->addaddress ($to, $name);
if (Is_file ($attachment)) {//Add attachment
$mail->addattachment ($attachment);
}
return $mail->send ()
------Solution--------------------
At line 7th, join
Ob_start ();
Join after Line 23rd
$s = Ob_get_flush ();
File_put_contents ($filename. ") XLS ", $s);
$attachment = $filename. ". XLS ";
Perform a message send
------Solution--------------------
There must be something wrong with you, check it out carefully.
You actually output a text file, you can open it with Notepad.
The function and usage of the OB function are available in the manual
------Solution--------------------
Sweat! How do you send it as an attachment without a path??
Aren't you a myth?
------Solution--------------------
To make a thought, you can refer to the following:
First save Excel on the server, and then get the Excel path, and then email as an attachment, if you do not need this file, and then do the delete operation on the OK