永发信息网

如何把php导出的Excel作为邮件发送

答案:2  悬赏:80  手机版
解决时间 2021-04-12 19:56
  • 提问者网友:你独家记忆
  • 2021-04-12 02:05
如何把php导出的Excel作为邮件发送
最佳答案
  • 五星知识达人网友:封刀令
  • 2021-04-12 02:53
1 .生成excel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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 "$title\n";
}
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 . 发送邮件:
用了phpmailer类库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = '';
$mail->Host = $config['SMTP_HOST']; // SMTP 服务器
$mail->Port = $config['SMTP_PORT']; // SMTP服务器的端口号
$mail->Username = $config['SMTP_USER']; // SMTP服务器用户名
$mail->Password = $config['SMTP_PASS']; // SMTP服务器密码
$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)){ // 添加附件
$mail->AddAttachment($attachment);
}
return $mail->Send()
全部回答
  • 1楼网友:平生事
  • 2021-04-12 03:13
发你邮箱731468999@qq.com,例子而已。 确保outlook已登陆。在工资表中,你选中某人,点一下按钮,就发出去了。 主要代码如下: sub test() dim i as integer i = selection.row   '记录你选中人的所在行数 workbooks.add   '新建工作表 thisworkbook.activesheet.range("a1:i1").copy range("a1")     '复制表头,请自己改a1:i1 thisworkbook.activesheet.range("a" & i & ":i" & i).copy range("a2")     '复制明细,请自己改a,i activeworkbook.sendmail thisworkbook.activesheet.range("j" & i), "工资明细" '发邮件,请自己改j activeworkbook.close 0 end sub 由于发邮件的过程比较慢,所以没有用循环。感兴趣的话,可以试试如下代码(我没试过): sub test() dim i as integer for i = 2 to 10 '从第2行到第10行,循环发,你可以先把2到10行的邮件地址都改为自己的试试 workbooks.add   '新建工作表 thisworkbook.activesheet.range("a1:i1").copy range("a1")     '复制表头,请自己改a1:i1 thisworkbook.activesheet.range("a" & i & ":i" & i).copy range("a2")     '复制明细,请自己改a,i activeworkbook.sendmail thisworkbook.activesheet.range("j" & i), "工资明细" '发邮件,请自己改j activeworkbook.close 0 next i end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯