PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How do I attach a Word (docx) file I made using a template processor into an email I'm sending

 July 06, 2022     php, phpmailer, phpword     No comments   

Issue

I'm using this function to send a mail, and it works perfectly:

mail('emailhere','subjecthere','contenthere');

I'm also using this code to create a word file and offer the user to download it:

// I use my templace that's with my files : 
    $templateProcessor = new TemplateProcessor('Template.docx');

// I fill the template values from an sql query : 
    $templateProcessor->setValue('titre', $options['titre']);
    $templateProcessor->setValue('source', $options['source']);
    $templateProcessor->setValue('auteur', $options['auteur']);
    $templateProcessor->setValue('date_pub', $options['date_pub']);
    $templateProcessor->setValue('contenu', $options['contenu']);

 // I give the user the file (I don't fully understand how this works but it does)

    header("Content-Disposition: attachment; filename=$title.docx");
    $templateProcessor->saveAs('php://output');

What I'm trying to do is, I want to create a word file from that template, put it inside that mail and send it

I apologize for the lack of code that I tried, but I really don't know where to start even.

I have been recommended to use this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

But I don't know what the path to the file is?


Solution

Add the following lines of code:

// Create a temporary file to store the template.

$temp_file = tempnam(sys_get_temp_dir(), 'PHPTemplate');

// Save the template

$templateProcessor->saveAs($temp_file);

// Attach the temporary file (template) to the email

$mailer->addAttachment($temp_file, 'nameofile.docx');

// Send Email

return $email->Send();


Answered By - Juan Carlos
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing