PHP在线生成excel并发送邮件【原创

来源:https://www.sucaihuo.com/php/871.html 2016-06-11 15:35浏览(5477) 收藏

本文实现PHP发送带excel的邮件大致思路如下:首先生成二维数组,并导入到excel文件当中。然后用phpmailer把生成的文件发送到邮件当中。
PHP在线生成excel并发送邮件
分类:表单代码 > 输入框 难易:中级
下载资源 下载积分: 380 积分

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, "标题测试", "素材火内容测试,欢迎来到素材火<a href='http://www.sucaihuo.com'>http://www.sucaihuo.com</a>", $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;
}
声明:本文为原创文章,如需转载,请注明来源sucaihuo.com并保留原文链接:https://www.sucaihuo.com/php/871.html
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2