首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >想要生成一个docx文件并使用PHP (PHPMailer)作为附件发送它

想要生成一个docx文件并使用PHP (PHPMailer)作为附件发送它
EN

Stack Overflow用户
提问于 2013-09-16 19:51:13
回答 2查看 3.6K关注 0票数 0

我已经创建了一个病人医疗报告表单,工作是我想要生成一个格式正确、兼容的docx文件(主要由表和段落组成),并将其作为附件发送,而不需要保存任何地方。

为了创建docx文件,我想使用PHPWord插件,我也尝试了使用HTMLtoDOCX生成docx,但是我想在附件中发送动态生成的docx文件。(不要告诉我使用PHPDocx,因为我有它的社区版本和它生成的空白文件或文件,但文本比添加的要少)

我正在使用PHPMailer发送电子邮件。我不想附加文件使用AddAttachment函数,因为它是用于位于某处的永久文件。

目前,我能够使用PHPMailer发送动态生成的文档PHPMailer文件,但是该文件与MS Word 2010不兼容,生成文档文件的代码并将其作为附件发送:

代码语言:javascript
复制
    $separator = md5(time());

    $eol = PHP_EOL;

    $headers  = "MIME-Version: 1.0".$eol;
line-4  // $headers .= "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document".$eol;
line-5  // $headers .= "Content-Type: application/vnd.ms-word.document.macroEnabled".$eol;

    // attachment name
    $filename = $a1 . " Medical Report.doc";

    // Report Document

    $report  = "                    ".$a1 . " Medical Report".$eol.$eol.$eol;
    $report .= "NAME                            |            ".$a1.$eol;
    $report .= "HOSPITAL No.                    |            ".$a2.$eol;
    $report .= "DATE of BIRTH                   |            ".$a3.$eol;
    $report .= "SEX                             |            ".$a4.$eol;
    $report .= "DATE of FOLLOW UP               |            ".$a5.$eol;
    $report .= "DATE of IMPLANT                 |            ".$a6.$eol;
    $report .= "PACEMAKER                       |            ".$a7.$eol;
    $report .= "MODEL                           |            ".$a8.$eol;
    $report .= "PROGRAMMED RATE(bpm)            |            ".$a9.$eol;
    $report .= "MAGNET RATE(bpm)                |            ".$a10.$eol;
    $report .= "EOL/ERT RATE(bpm)               |            ".$a11.$eol;
    $report .= "PROGRAMMED RATE(ms)             |            ".$a12.$eol;
    $report .= "MAGNET RATE(ms)                 |            ".$a13.$eol;
    $report .= "EOL/ERT Rate(ms)                |            ".$a14.$eol;
    $report .= "ATRIAL(amp)                     |            ".$a15.$eol;
    $report .= "RIGHT VENTRICLE(amp)            |            ".$a16.$eol;
    $report .= "LEFT VENTRICLE(amp)             |            ".$a17.$eol;
    $report .= "ATRIAL(pw)                      |            ".$a18.$eol;
    $report .= "RIGHT VENTRICLE(pw)             |            ".$a19.$eol;
    $report .= "LEFT VENTRICLE(pw)              |            ".$a20.$eol;
    $report .= "ATRIAL(mv)                      |            ".$a21.$eol;
    $report .= "RIGHT VENTRICLE(mv)             |            ".$a22.$eol;
    $report .= "LEFT VENTRICLE(mv)              |            ".$a23.$eol;
    $report .= "ATRIAL(ohms)                    |            ".$a24.$eol;
    $report .= "RIGHT VENTRICLE(ohms)           |            ".$a25.$eol;
    $report .= "LEFT VENTRICLE(ohms)            |            ".$a26.$eol;
    $report .= "BATTERY IMPEDANCE               |            ".$a27.$eol;
    $report .= "CALC LONGEVITY                  |            ".$a28.$eol;
    $report .= "BATTERY VOLTAGE                 |            ".$a29.$eol;
    $report .= "ERI INDICATOR                   |            ".$a30.$eol;
    $report .= "CURRENT                         |            ".$a31.$eol;
    $report .= "STABILITY/MYO-POTENTIAL         |            ".$a32.$eol;
    $report .= "VA CONDUCTION                   |            ".$a33.$eol;
    $report .= "WOUND CHECK                     |            ".$a34.$eol;
    $report .= "ECG RHYTHM                      |            ".$a35.$eol;
    $report .= "UNDERLYING RHYTHM               |            ".$a36.$eol;
    $report .= "HISTOGRAMS %AGE PACING          |            ".$a37.$eol;
    $report .= "PATIENT SYMPTOMS                |            ".$a38.$eol;
    $report .= "COMMENTS                        |            ".$a39.$eol;
    $report .= "PROGRAMME CHANGES AND REASONS   |            ".$a40.$eol;  
    $report .= "CARDIAC PHYSIOLOGIST            |            ".$a44.$eol;
    $report .= "NEXT APPOINTMENT                |            ".$a42.$eol;



    // encode data (puts attachment in proper format)
    $attachment = chunk_split(base64_encode($report));

    ///////////HEADERS INFORMATION////////////

    // main header (multipart mandatory) message
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
    $headers .= "Content-Transfer-Encoding: 7bit".$eol;
    $headers .= "This is a MIME encoded message.".$eol.$eol;

    // message
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $headers .= $message1.$eol.$eol;

    // attachment
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $headers .= "Content-Transfer-Encoding: base64".$eol;
    $headers .= "Content-Disposition: attachment".$eol.$eol;
    $headers .= $attachment.$eol.$eol;
    $headers .= "--".$separator."--";



    require("class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP();   // set mailer to use SMTP
    $mail->Host = "localhost";  // specify main and backup server
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->Username = "newuser"; // Make sure to replace this with your shell enabled user
    $mail->Password = "wampp";  // Make sure to use the proper password for your user


    $mail->From = "admin@localhost";
    $mail->FromName = "Admin";
    $mail->AddAddress("recepient@domain.com", "First Last");
        $mail->AddCC("recepient@domain.com");
        $mail->AddBCC("recepient@domain.com");

    $mail->AddReplyTo("admin@localhost", "Admin");

    $mail->WordWrap = 50;  // set word wrap to 50 characters
    $mail->IsHTML(true);   // set email format to HTML

    $mail->Subject = $subject;
    $mail->MsgHTML($message1);
    $mail->AddCustomHeader($headers);
    // $mail->AddAttachment($filename, 'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document;application/vnd.ms.word.document.macroEnabled');

    if(!$mail->Send()){
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }
    else {
echo <<<_END
    <script>
        alert("Email Sent");
        document.location='index.html';
    </script>
_END;
    }
?>

如果我注释掉了docx的第4行和第5行,那么我没有收到HTML,而且文件也被更改为不同的不受支持的word文件。

这个完整问题的结论是,我需要根据我想要的设计或模板正确格式化的Docx文件,并将其作为附件发送,而不需要任何保存或下载对话框。

如果我使用这些标题,则会出现一个对话框,其中包含“打开和保存”选项(我也不希望这样)。

代码语言:javascript
复制
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=example.docx');
header('Content-Transfer-Encoding: binary');

我希望你们都能理解我想要什么。如果有人会给我正确的方向或解决方案,那就提前1000000000000000谢谢,因为我在这个问题上浪费了2天时间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-16 19:59:24

你不能生成一个文字文件..。您正在生成纯文本,然后通过伪造mime头来假装它是单词。

考虑到您使用的是PHPMailer,您应该使用而不是来构建您自己的MIME电子邮件-- PHPMailer已经为您自己做得很好了。

对于不想使用“真实”文件来存储附件数据,可以使用AddStringAttachment()方法:

代码语言:javascript
复制
  $mailer->AddStringAttachment($your_fake_word_file_as_a_string, 'Example.docx')

详见此处:http://phpmailer.worxware.com/index.php?pg=tutorial#3

票数 2
EN

Stack Overflow用户

发布于 2014-08-28 04:50:09

使用PHPWord生成Docx文件并在邮件中发送。很容易添加图像、表格、字体样式和更多的功能,

有关PHPWord及其使用的更多信息,请在这里阅读。

http://www.kvcodes.com/2014/08/create-docx-files-using-php/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18836154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档