0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Does this answer your question? [Send attachments with PHP Mail()?](https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail) – Wais Kamal Aug 03 '21 at 15:08
  • @WaisKamal I dont think it does, Im not sending an attachement from my own PC, Im making the attachement and sending it , so I dont know what I should be putting in the 'file path' part they're saying – media watch Aug 03 '21 at 15:20
  • Isn't the attachment you want to send a file? – Wais Kamal Aug 03 '21 at 15:20
  • @WaisKamal , Yes , What is the path to it ? Considering the code I offered up above ? If u can help me with that I will be very grateful – media watch Aug 03 '21 at 15:23
  • I will make an edit to show where I need help exactly – media watch Aug 03 '21 at 15:26
  • Sorry but I misunderstood you at the beginning. I am not familiar with `templateProcessor`, but I think you can find the answer [here](https://stackoverflow.com/questions/51800292/what-is-the-default-directory-of-template-processor-in-phpword). – Wais Kamal Aug 03 '21 at 15:27
  • `$templateProcessor->saveAs('php://output');` write directly to the output buffer - so you can simply grab that buffer content (https://stackoverflow.com/q/14671961/1427878), and then use `addStringAttachment` instead of `AddAttachment`. – CBroe Aug 10 '21 at 10:42

1 Answers1

0

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();
Dharman
  • 30,962
  • 25
  • 85
  • 135