在php脚本中你看到我使用phpmailer和phpword。你使用表格,他发送到电子邮件地址。 Word文件中表单的内容。 但是当我打开Word文件时,他会在字段之间显示br标记(请参阅with.jpg with br tag)。 但我希望没有br标签:without br tag in Word
我的问题是:如何使用phpWord获取没有br标签的Word附件?
提前感谢。
我的代码是:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{944946CD-39B2-4A16-A8A8-9F70F0450506}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>nunitlib</RootNamespace>
<AssemblyName>nunit-lib</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Test.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
形式:
<?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);
}
?>
答案 0 :(得分:0)
嗯,最灵活的方法是逐个编写值(请注意,在编写文本时,你应该始终使用htmlspecialchars函数 - 特别是当用户输入时直接写入word文档):< / p>
/*
$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');
如果您希望将值组合为最初完成的单个消息变量,您还可以使用PhpWord的addHtml函数(只需注意,使用此选项无法定义字体样式):
PhpOffice\PhpWord\Shared\Html::addHtml($section, $message);