我有两件事。我有phpmailer,他把表单的内容发送到E-mailaddress,这个工作。我有phpword,他制作word文件,这也有效。
我有一个问题;如果单击“提交”按钮,如何在电子邮件附件的Word(docx)文件中获取表单内容$ message(全名,主题,电话,电子邮件和评论)?
使用此代码,您在浏览器中看不到任何内容,我如何'混合'phpmailer和phpword?。
有人可以帮助我吗?
提前感谢。
表单代码为:
<?php
//phpword
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
$section->addText($message, array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
//phpmailer
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// 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
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" required/>
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" required/>
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
答案 0 :(得分:1)
所以基本上你试图在你声明它之前将变量$message
的内容添加到word文档中。
此外,您没有在表单中定义“操作”,因此当您单击“提交”按钮时它什么都不做。
假设您的其余代码正常工作,应该这样做:
<?php
if(isset($_POST['submit']))
{
//phpword
require_once '../PHPWord.php';
//phpmailer
require "phpmailer/class.phpmailer.php"; //include phpmailer class
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
$section->addText($message, array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
// 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
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<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/>
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" required/>
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
请记住将“url.php”替换为您网页的真实网址。
此致
修改强> 为了能够使用phpmailer附加Word文件并阅读文档(https://github.com/PHPMailer/PHPMailer/wiki/Tutorial):
附加本地文件的命令很简单 $ mail-&gt; addAttachment($ path);,其中$ path包含路径 您要发送的文件,可以放在$ mail = new之间的任何位置 PHPMailer的;并发送消息。请注意,您无法使用URL 路径 - 您可能只使用本地文件系统路径。请参阅字符串说明 以下附件,了解如何使用远程内容。
翻译到您的脚本,这意味着您必须添加以下行:
[...]
$mail->AddAddress("test@gmail.com", "form from website"); // Where to send it - Recipient
$mail->addAttachment("Text.docx"); // <--------------------------
$result = $mail->Send(); // Send!