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);
发送生成的文件到指定邮箱php
$rs = sendMail($email, "标题测试", "二当家的,欢迎来到二当家的<a href='http://www.erdangjiade.com'>http://www.erdangjiade.com</a>", $attachments); echo $rs;
记得在sendMail方法里面配置邮件服务器,最好是企业邮箱,好比QQ企业邮箱,会当即收到。163等普通邮箱发送频繁会被冻结,过段时间又能够发送。数组
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.erdangjiade.com'>http://www.erdangjiade.com</a>", $attachments); echo $rs;
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; } 更多php技术交流,可加Q群:884743303,里面各路大神为您保驾护航!