phpword附件在Word中显示br标记

phpword附件在Word中显示br标记,php,phpword,Php,Phpword,在php脚本中,您可以看到我使用了phpmailer和phpword。你使用表格,他发送到电子邮件地址。Word文件中表单的内容。 但是当我打开Word文件时,他会在字段之间显示br标记(参见with.jpg)。 但我想要这个没有br标签的: 我的问题是:如何在没有带phpWord的br标签的情况下获得单词附件 提前谢谢 我的代码是: <?php if(isset($_POST['submit'])) { //phpmailer require "phpmailer/cla

在php脚本中,您可以看到我使用了phpmailer和phpword。你使用表格,他发送到电子邮件地址。Word文件中表单的内容。 但是当我打开Word文件时,他会在字段之间显示br标记(参见with.jpg)。 但我想要这个没有br标签的:

我的问题是:如何在没有带phpWord的br标签的情况下获得单词附件

提前谢谢

我的代码是:

<?php

if(isset($_POST['submit']))
{
  //phpmailer
     require "phpmailer/class.phpmailer.php"; //include phpmailer class
     //phpword
     require_once 'PHPWord.php';



     $message=
     'Full Name:    '.$_POST['fullname'].'<br />
     Subject:    '.$_POST['subject'].'<br />
     Phone:    '.$_POST['phone'].'<br />
     Email:    '.$_POST['emailid'].'<br />
     Comments:    '.$_POST['comments'].'
     Comments:    '.$_POST['comments2'].'
     ';

     // New Word Document
     $PHPWord = new PHPWord();

     // New portrait section
     $section = $PHPWord->createSection();
     //add text

     $section->addText($message, array('name'=>'Arial'));
     $section->addTextBreak(2);//if this in comment the word file give also br tag

     // Save File
     $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
     $objWriter->save('Text.docx');    

    // Instantiate Class  
    $mail = new PHPMailer();  

    // Set up SMTP  
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 465;  //Gmail SMTP port
    $mail->Encoding = '7bit';

    // Authentication  
    $mail->Username   = "test@gmail.com"; // Your full Gmail address
    $mail->Password   = "secret"; // Your Gmail password


    // Compose
    $mail->SetFrom($_POST['emailid'], $_POST['fullname']);
    $mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
    $mail->Subject = "form from website";      // Subject (which isn't required)  
    $mail->MsgHTML($message);

    // Send To  
    $mail->AddAddress("test@gmail.com", "form from website"); // Where to send it - Recipient
    $mail->AddAttachment("Text.docx");      // attachment
    $result = $mail->Send();        // Send!  
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);

}
?>

最灵活的方法是逐个写入值(请注意,在将文本(尤其是用户直接输入的文本)写入word文档时,确实应该始终使用htmlspecialchars函数):


尝试使用
\r
\n
或两者都使用,可能是因为您没有在标题中设置类型(不确定代码,但我认为它是
文本/HTML
<body>
    <div style="margin: 100px auto 0;width: 300px;">
            <h3>Contact Form</h3>
            <form name="form1" id="form1" action="url.php" method="post">
                    <fieldset>
                      <input type="text" name="fullname" placeholder="Full Name" required/>

                      <input type="text" name="subject" placeholder="Subject" />

                      <input type="text" name="phone" placeholder="Phone" />

                      <input type="text" name="emailid" placeholder="Email"  required/>

                      <textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>

                      <textarea rows="4" cols="20" name="comments2" placeholder="Question/Comments"></textarea>

                      <input type="submit" name="submit" value="Send" />
                    </fieldset>
            </form>
            <p><?php if(!empty($message)) echo $message; ?></p>
        </div>
</body>
 /*
 $message=
 'Full Name:    '.$_POST['fullname'].'<br />
 Subject:    '.$_POST['subject'].'<br />
 Phone:    '.$_POST['phone'].'<br />
 Email:    '.$_POST['emailid'].'<br />
 Comments:    '.$_POST['comments'].'
 Comments:    '.$_POST['comments2'].'
 ';
 */

 // New Word Document
 $PHPWord = new PHPWord();

 // add the font style globally so that it can be accessed with the name
 $PHPWord->addFontStyle('ArialText',  array('name'=>'Arial'));

 // New portrait section
 $section = $PHPWord->createSection();

 //add text
 $section->addText(htmlspecialchars('Full Name:    ' . $_POST['fullname'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Subject:    ' . $_POST['subject'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Phone:    ' . $_POST['phone'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Email:    ' . $_POST['emailid'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Comments:    ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Comments:    ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');
PhpOffice\PhpWord\Shared\Html::addHtml($section, $message);