dousa1630 2013-09-16 19:51
浏览 86
已采纳

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

I have created a Patient Medical Report form, the work is I want to generate a proper formatted and compatible docx file (mostly consist table and paragraph) with value received by the filled form and send it as attachment without saving anywhere.

To create docx file I want to use PHPWord plugin and I also tried HTMLtoDOCX for docx generation but I want to send the dynamically generated docx file in attachment. (Don't tell me to use PHPDocx because I have its community version and its generating either blank file or file with less text than added)

I am using PHPMailer to send Emails. I don't want to attach file use AddAttachment Function because it is use for permanent file that is located somewhere.

Currently I am able to send a dynamically generated doc file using PHPMailer but the file is not compatible with MS Word 2010 the code for generating doc file and send it as attachment is:

    $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;
    }
?>

If I comment out the line-4 and line-5 for docx then I didn't receive the HTML msg and also the file is also changed to a different unsupported word file.

The conclusion of this complete question is that I need the properly formatted Docx file according the design I want or the template I have and send it as attachment without any save or download dialog box.

If I use these headers then a dialog box come out with open and save option (I don't want this also)

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=example.docx');
header('Content-Transfer-Encoding: binary');

I hope you all understand what I want. If somebody will give me right direction or solution then 1000000000000000 thanks in advance because I have wasted 2 days on this issue only.

  • 写回答

2条回答 默认 最新

  • douyanning3724 2013-09-16 19:59
    关注

    You're not generating a Word file... you're generating plain text and then PRETENDING it's Word by forging the mime headers.

    Given that you're using PHPMailer, you should NOT be trying to build your own MIME email - PHPMailer already will do that for you perfectly well on its own.

    As for not wanting to use a "real" file to store the attachment data, you can use the AddStringAttachment() method:

      $mailer->AddStringAttachment($your_fake_word_file_as_a_string, 'Example.docx')
    

    as detailed here: http://phpmailer.worxware.com/index.php?pg=tutorial#3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大