How to send excel in PHP as a message

Source: Internet
Author: User
Tags foreach join mail port number

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.