duanchi0897 2015-11-15 18:23
浏览 26

如何在电子邮件中附加Word中的表单数据

I have two things. I have phpmailer, he sent the content of the form to a E-mailaddress, this works. And I have phpword, he make word file, this also works.

I have a question; how can I get the content of the form the $message (Full name, subject, phone, email and comments) in a Word(docx) file in a Email attachment if you click on the submit button?

With this code you see nothing in the browser, how can I 'mix' phpmailer and phpword?.

Can someone help me?

thanks in advance.

The form code is:

    <?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>
  • 写回答

1条回答 默认 最新

  • dqq9695 2015-11-15 19:04
    关注

    So basically you are trying to add the content of the variable $message to the word document BEFORE you even declared it.

    Also, you don't define an "action" in your form, so it is just doing nothing when you click the submit button.

    Assuming that the rest of your code works, this should do it:

    <?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>
    

    Remember to replace "url.php" with the real URL of your page.

    Regards

    EDIT: In order to be able to attach the Word file with phpmailer and reading the documentation (https://github.com/PHPMailer/PHPMailer/wiki/Tutorial):

    The command to attach a local file is simply $mail->addAttachment($path);, where $path contains the path to the file you want to send, and can be placed anywhere between $mail = new PHPMailer; and sending the message. Note that you cannot use a URL for the path - you may only use local filesystem path. See notes on string attachments below for how to use remote content.

    Translated to your script, this means you have to add the following line:

    [...]
    $mail->AddAddress("test@gmail.com", "form from website"); // Where to send it - Recipient
    $mail->addAttachment("Text.docx"); // <--------------------------
    $result = $mail->Send();        // Send!
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大