PHP——php脚本如何自动发送邮件

核心思想就是利用了php 的mail函数。支持脚本化自动发送邮件,并且可以带附件。

Mail 简介
函数允许您从脚本中直接发送电子邮件。要使邮件函数可用,PHP 需要已安装且正在运行的邮件系统。要使用的程序是由 php.ini 文件中的配置设置定义的。

1 首先封装一个Mail class类。

 
//邮件发送类
class Mail
{
    //发送指定文件列表到邮箱列表
    //邮件接收人列表,类型为数组
    //附件数据文件为全路径,数组
    //attachmentType, 为附件添加的文件类型后缀
    public function sendData($toList, $from, $subject, $message, $attachmentFileList, $attachmentType)
    {
        $to = '';
        foreach ($toList as $key => $email)
        {
            $to = empty($to) ? $email : $to.','.$email;
        }

        if (empty($to))
        {
            return false;
        }

        $attachments = array();
        foreach($attachmentFileList as $key => $fileFullPath)
        {
            $attachments[$fileFullPath] = file_get_contents($fileFullPath);
        }

        $this->doMail($to, $from, $subject, $message, $attachments, $attachmentType);
    }

    private function doMail($to, $from, $subject, $message, $attachments, $attachmentType)
    {
        // 建立在邮件中分隔不同部分的分界线。
        // 基本上,分界线可以是任意的字符串。
        // 但是重要的一点是确定一个写邮件的人
        // 这会随意写出的字符串,所以我们用
        // uniqid 函数来产生一个随机的字符串。
        $boundary = "||||||||||||||||||||||";
        // 现在我们要建立邮件头。不要忘了插入
        // Content-type头来说明这个邮件包含一个或更多的附件。
        $headers = "From: $from
                    Accept-Language: zh-CN, en-US
                    Content-Language: zh-CN
                    Content-type: multipart/mixed; boundary=\"$boundary\"
                    MIME-Version: 1.0";
        //消息内容
        $emailBody =  $message;
        $attachmentContent = '';
        foreach ($attachments as $fileFullPath => $attachment)
        {
            $pos = strrpos($fileFullPath, '/');
            $fileName = substr($fileFullPath, $pos + 1, strlen($fileFullPath) - $pos - 1).".".$attachmentType;
            $attachment = base64_encode($attachment);
            $attachmentContent .= "--$boundary
                                    Content-Type: application/vnd.ms-excel;
                                        name=\"$fileName\"
                                    Content-Description: $fileName
                                    Content-Disposition: attachment;
                                        filename=\"$fileName\";
                                    Content-Transfer-Encoding: base64
                                    $attachment     
";
         }

        // 好,现在我们已经有了邮件的所有内容。
        // 下一件事是修改邮件的主体。
        $emailBody =  "--$boundary
Content-Type: multipart/alternative;
    boundary=\"_000_396C66BA033B7149911C4430D9E0A547037C3Fm1mailmb16interna_\"

--_000_396C66BA033B7149911C4430D9E0A547037C3Fm1mailmb16interna_
Content-Type: text/plain; charset=\"gb2312\"
Content-Transfer-Encoding: base64

DQo=

--_000_396C66BA033B7149911C4430D9E0A547037C3Fm1mailmb16interna_
Content-Type: text/html; charset=\"gb2312\"
Content-Transfer-Encoding: quoted-printable



$emailBody



--_000_396C66BA033B7149911C4430D9E0A547037C3Fm1mailmb16interna_

$attachmentContent
--$boundary--";

        mail( $to,  $subject, $emailBody, $headers);
    }
}
?>

2 然后就可以把上面的类require_once 进来,进行自动化发送邮件了。

("./Mail.class.php");
define("FILEPATH", "*****");
// $td = date("Ymd",strtotime('-0 day'));
$temp=date('md',time());
$toList =array('[email protected]');//收件人列表
$from = "[email protected]";//发件人
$subject="邮件标题";
$subject = "=?gb2312?B?".base64_encode($subject)."?=";
$cn1=$argv[1];//脚本传入参数
$message = "Hi,这里是邮件内容。$cn1 这是脚本传进来的参数
这是换行符    这是两个空格符 "
; //如果不发送附件文件,则以下两个字段置空"" $attachmentFileList=array(FILEPATH."sendfile1.txt",FILEPATH."sendfile2.txt"); $attachmentType=""; $mailMan = new Mail(); $mailMan->sendData($toList, $from, $subject, $message, $attachmentFileList, $attachmentType); ?>

3 最后随便写个shell脚本,加入到crontab 中自动化运行就可。就可以实现定时发送邮件的功能啦。

#!/bin/bash
cn1=`date +%m%d`
/home/.../php/bin/php ./sendcomplainmail.php  $cn1 

你可能感兴趣的:(php)