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('[email protected]', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( '[email protected]' );

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

7
  • 2
    Does this answer your question? Send attachments with PHP Mail()?
    – Wais Kamal
    Commented Aug 3, 2021 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 Commented Aug 3, 2021 at 15:20
  • Isn't the attachment you want to send a file?
    – Wais Kamal
    Commented Aug 3, 2021 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 Commented Aug 3, 2021 at 15:23
  • I will make an edit to show where I need help exactly Commented Aug 3, 2021 at 15:26

1 Answer 1

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.