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,裏面各路大神爲您保駕護航!