PHP 把二维数组导入到excel,返回参数是生成的文件数组
include_once 'function.php';
$email = $_POST['email'];
$titles = array("id", "时间", "名称"); //excel 列
$datas = array(
0 => array(
"id" => "1",
"date" => date("Y-m-d", strtotime("-1 day")),
"name" => "二当家的"
),
1 => array(
"id" => "2",
"date" => date("Y-m-d"),
"name" => "分享微博送30积分"
),
);
$file_name = date("Y-m-d") . "二当家的excel发送";
$attachments = sendExcel($file_name, $titles, $datas);
发送生成的文件到指定邮箱
$rs = sendMail($email, "标题测试", "这里内容", $attachments);
echo $rs;
记得在sendMail方法里面配置邮件服务器,最好是企业邮箱,比如QQ企业邮箱,会立即收到。163等普通邮箱发送频繁会被冻结,过段时间又可以发送。
function sendMail($to, $subject, $body = '', $attachment = null) { //$to 收件者 $subject主题 $body 内容 $attachment附件
$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
if (!preg_match($pattern, $to)) {
return "email_error";
}
//邮件服务器配置
$detail = array(
"smpt" => "smtp.qq.com",
"account" => "",
"pwd" => "",
);
$title = getGb2312("素材火发送excel到邮箱");
include_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer(); //PHPMailer对象
$mail->CharSet = 'GB2312'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->Encoding = "base64";
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // 关闭SMTP调试功能
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = ''; // 使用安全协议
$mail->Host = $detail['smpt']; // SMTP 服务器
$mail->Port = "25"; // SMTP服务器的端口号
$mail->Username = $detail['account']; // SMTP服务器用户名
$mail->Password = $detail['pwd']; // SMTP服务器密码
$mail->Subject = getGb2312($subject); //邮件标题
$mail->SetFrom($detail['account'], $title);
$mail->MsgHTML(getGb2312($body));
$mail->AddAddress(getGb2312($to), $title);
if (is_array($attachment)) { // 添加附件
foreach ($attachment as $file) {
is_file($file) && $mail->AddAttachment($file);
}
}
$rs = $mail->Send() ? true : $mail->ErrorInfo;
return $rs;
}
下载地址