使用 PHPMailer 发邮件
/** * 发邮件 * * @param array $receiver 接收人信息 * @param array $attachment_info 附件信息 * @param string $is_html 是否html内容 默认值不是 * @param string $mail_title 邮件标题 * @param string $mail_body 邮件内容 * @return boolean 发送成功与否 * * * =================== * $receiver = array( * array('mail_address'=>'xxx@qq.com','user_name'=>'张三','type' => 1), * array('mail_address'=>'xxx@qq.com','user_name'=>'李四','type' => 2), * array('mail_address'=>'xxx@qq.com','user_name'=>'张三','type' => 3), * ) * type: 1-密送 2-抄送 3-主送 * =================== * * =================== * $attachment_info = array( * array('file_full_name' => 'xx/xxx/test.jpg', 'attachment_name'=> 'beautiful.jpg'), * array('file_full_name' => 'xx/xxx/test.jpg', 'attachment_name'=> 'beautiful.jpg'), * * ) * file_full_name 文件全路径 attachment_name 邮件里边的附件名称 * =================== */ function sendMail($receiver, $mail_title = "sysinfo", $mail_body = 'test', $attachment_info = array(), $is_html = false){ if (empty($receiver)){ return false; } require_once LIB_PATH . '/PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer(); $mail->isSMTP(); // Set mailer to use SMTP $mail->CharSet='UTF-8'; // 设置邮件的字符编码,这很重要,不然中文乱码 $mail->Host = 'smtp.exmail.qq.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // 开启认证 $mail->WordWrap = 50; //设置每行最大字符数,超过改数后自动换行 $mail->Username = 'aaa@xxx.com'; // SMTP username $mail->Password = 'bbb@xxx'; // SMTP password /*设置信件是否是HTML格式*/ $mail->isHTML($is_html); /*发件人信息*/ $mail->From = 'aaa@xxx.com'; $mail->FromName = '系统'; /*添加收件人*/ if (!empty($receiver)){ foreach ($receiver as $val){ if (empty($val['mail_address'])){ continue; } if ($val['type'] == 1){ $mail->addBCC($val['mail_address'], $val['user_name']); //密送,不出现在收件方列表里边 }elseif($val['type'] == 2){ $mail->addCC($val['mail_address'], $val['user_name']); //抄送 }elseif ($val['type'] == 3){ $mail->addAddress($val['mail_address'], $val['user_name']); //主送 } } } /*添加附件*/ if (!empty($attachment_info)){ foreach ($attachment_info as $val){ if (empty($val['file_full_name'])){ continue; } $mail->addAttachment($val['file_full_name'], $val['download_name']); } } /*标题*/ $mail->Subject = $mail_title; /*内容*/ $mail->Body = $mail_body; /*发信*/ if(!$mail->send()) { //$mail->ErrorInfo; return false; } else { return true; } }