You can use stream called php://temp
Using MPDF
If you are using MPDF (version 7) you probably use the composer to install, so it should look like this:
require_once __DIR__ . '/vendor/autoload.php';
Then an example with mpdf:
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Stack Overflow');
$mpdf->Output('php://temp', \Mpdf\Output\Destination::FILE);
And in Phpmailer use this:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com'; //Configure o seu SMTP
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // usuario
$mail->Password = 'secret'; // senha
$mail->SMTPSecure = 'tls'; // TLS se tiver criptografia
$mail->Port = 587; //Porta do SMTP
//Destinatários
$mail->setFrom('[email protected]');
//ADICIONA O ANEXO AQUI
$mail->addAttachment('php://temp', 'Arquivo.pdf');
$mail->isHTML(true);
$mail->Subject = 'ASSUNTO';
$mail->Body = 'Mensagem';
$mail->AltBody = 'mensagem para clientes de email que não usam HTML';
$mail->send();
echo 'Mensagem enviada';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Using FPDF
If you are not using composer and are using FPDF, first import what you need:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
require 'path/to/fpdf.php';
Then call the FDPF:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B',16);
$pdf->Cell(40, 10, 'Stack Overflow!');
$pdf->Output('F', 'php://temp');
And in phpmailer is the same as the first example, if it keeps like this:
//ADICIONA O ANEXO AQUI
$mail->addAttachment('php://temp', 'Arquivo.pdf');