2015-11-15 21 views
0

我有兩件事。我有phpmailer,他將表單的內容發送到電子郵件地址,這是有效的。我有phpword,他做文件,這也有用。如何在電子郵件中附上Word表單數據

我有一個問題;如果點擊提交按鈕,我如何才能在電子郵件附件的Word(docx)文件中獲取$消息(全名,主題,電話,電子郵件和註釋)形式的內容?使用這段代碼,你不會在瀏覽器中看到任何東西,我該如何'混合'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 = "[email protected]"; // 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("[email protected]", "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

所以基本上你想變$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 = "[email protected]"; // 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("[email protected]", "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」。

問候

編輯: 爲了能夠對Word文件附加與PHPMailer的和閱讀文檔(https://github.com/PHPMailer/PHPMailer/wiki/Tutorial):

的命令附加一個本地文件只是 $ mail-> addAttachment($ path);,其中$ path包含您要發送的 文件的路徑,可以放在$ mail = new PHPMailer之間的任何位置;併發送消息。請注意,您不能使用URL爲 的路徑 - 您只能使用本地文件系統路徑。有關如何使用遠程內容,請參閱下面有關字符串 附件的說明。

翻譯成你的腳本,這意味着你必須添加以下行:

[...] 
$mail->AddAddress("[email protected]", "form from website"); // Where to send it - Recipient 
$mail->addAttachment("Text.docx"); // <-------------------------- 
$result = $mail->Send();  // Send! 
+0

是的,我看到它,isset在頁面的頂部。但我想在電子郵件中有一個附件,我怎樣才能做到這一點:$ mail-> AddAttachment($ message); // 附件??提前致謝。 – maichel

+0

我假設你的意思是添加新創建的Word文件,而不是變量本身。如果是這樣,請看上面的編輯。 –

相關問題