I have a script generating .pdf file. This script return this file as string and then I can save it with file_put_contents()
$output = $dompdf->output();
file_put_contents("myfile.pdf", $output);
I want to add this file as attachment to my email using PHPMailer. If I have a file on disk I just write path to it and new name:
$mail->addAttachment('/path/to/file.pdf', 'newname.pdf');
But can I add attachment without saving myfile.pdf to disk? Something like that:
$mail->addAttachment($output, 'myfile.pdf');
This string returns an error:
PHP Fatal error: Call to a member function addAttachment() on a non-object
How can I transform string type to file type without saving?
UPD: Full code
$output = $dompdf->output();
$name = 'title.pdf';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.***.org'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '***@***.orgg'; // SMTP username
$mail->Password = '******************'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = '[email protected]';
$mail->FromName = 'John';
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addStringAttachment($output, $name);
// Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}