`

H5页面生成word文件及发送邮件

阅读更多

最近工作中有用到生成word文档及发送该附件的邮件。先说说我的思路和流程吧;

1.在后台用PHP将数据组合拼接成HTML格式;

2.生成带数据(HTML)的word文档(生成到项目对应的目录下);

3.配置发送邮件的信息(如邮件的host/port/username等);

 

第一步:我们先将数据组装拼接成HTML格式  代码如下:

    header("Cache-Control: no-cache, must-revalidate");

    header("Pragma: no-cache");

    //有了这些,可以把带html标签的html源码导入到word里,并且可以保持html的样式。

    $wordStr = '<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:w="urn:schemas-microsoftcom:office:word" xmlns="http://www.w3.org/TR/REC-html40">

    <head>

    </head><body>';

     

    $wordStr .= '<div class="print-container"><div class="print-header"><h1>家访记录汇总</h1></div><div class="print-content">';

   

    for($i=1;$i<=14;$i++){

         $wordStr .= $this->getWordContent($i,$roomid); //拼接数据

    }

   

    $wordStr .= '</div></div>';

    $wordStr .= '</body></html>';

 

第2步.生成word文件到项目对应的目录下;

    $file = rand(10000000,99999999).'.docx';

    //保存文件到目录

    $savefile = $file;

    $attachDir = C("resourceFilesUpload");//上传文件保存路径,结尾不要带/

    $attachDir_show = C("resourceFilesView");

   

    $attachSubDir = 'visitdoc_'.date('ymd');

    $attachDir = $attachDir.'/'.$attachSubDir;

    if(!is_dir($attachDir))

    {

        @mkdir($attachDir, 0777);

    }

    $tempPath = $attachDir.'/'.$savefile;

    file_put_contents($tempPath,file_get_contents("php://input"));

   

    $myfile = fopen($tempPath, "a");

    fwrite($myfile, $wordStr);//写入内容

    fclose($myfile);//关闭该操作

 

3.配置发送邮件的信息(如邮件的host/port/username等);

     //获取保存文件后的地址

    $fileurl = $_SERVER["DOCUMENT_ROOT"]."/Public/upload/".$attachSubDir.'/'.$savefile;

    //注意这里不能用网络地址 如www.baidu.com/image/xxx.jpg

   

    //邮件title

    $emaildesc = "您好:<br/>";

    $emaildesc .= "<b>家访记录汇总, 请打开附件查看</b><br/>";

    $emaildesc .= "此致,晓黑板";

   

    //引用邮件发送类

    require THINK_PATH.'Extend/Emailsend/MySendMailNew.php';

   

    $mail = new MySendMailNew();

    $mail->setServer($this->emailhost, $this->username, $this->password); //你的邮件配置

    $mail->setFrom($this->fromemail);

    $mail->setReceiver($email);

    $mail->setMailInfo($fileName, $emaildesc, $fileurl);

 

    $mail->sendMail();

    //$this->success("邮件发送成功,请及时打开邮箱查看");

    return true;

 

效果如下:

1.生成的word文件

 

 

2.邮件发送后效果:



 

邮寄发送类的代码见下面附件

  • 大小: 38.3 KB
  • 大小: 36.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics