Change file name when sending email

Asked

Viewed 119 times

0

I am sending emails with PHP mPDF files, but there is a situation where I recover a URL that contains a boleto, from that URL I take the content and saved in a temporary folder on the server, but I have to generate a random name so it does not repeat itself and when sending the email I need to change this name, I can not send the random, follows the code:

Function sending email with Phpmailer

function sendSimpleMail($to, $subject, $msg, $arrayAnexo) {

require 'mail/Exception.php';
require 'mail/PHPMailer.php';
require 'mail/SMTP.php';

$mail = new PHPMailer(true);

try {
    // $mail->SMTPDebug = 2;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '';
    $mail->Password = '';
    $mail->Port = ;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );

    $mail->setFrom('[email protected]', 'SE Suite');
    $mail->addAddress($to);
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Subject = $subject;
    $mail->Body    = $msg;

    foreach($arrayAnexo as $attach){
        if (array_key_exists("TYPE", $attach) == true)
            $type = $attach["TYPE"];
        else
            $type = "FILE";

        if (array_key_exists("PATH", $attach) == true)
            $path = $attach["PATH"];
        else
            $path = "";

        if (array_key_exists("NAME", $attach) == true)
            $name = $attach["NAME"];
        else
            $name = "";

        if (!empty($path))
        {   
            # possui um caminho
            if ($type == "URL" && !empty($name)){
                $pathArchive = generatePDFfromURL($path);//aqui eu chamo a função que salva o PDF no servidor
                $mail->AddAttachment($pathArchive);//e aqui é onde eu preciso trocar o nome pra enviar o email  
            }
            elseif ($type == "FILE")
            {
                # anexa o arquivo já salvo no servidor
                if (empty($name)){
                    $name = substr($path, strrpos($path, "/"));
                    $mail->AddAttachment($name); 
                }
                echo " <BR> Adiciona na fila o arquivo já no servidor como anexo.";
            }
            echo " ==> Type: " . $type . " Path: " . $path . " Name: " . $name;
        }
    }

    $mail->send();

    echo 'Mensagem enviada!';

} catch (Exception $e) {
    echo 'Error: ', $mail->ErrorInfo;
}

and here the function that sends the contents of the URL as PDF pro server :

function generatePDFfromURL($path){

error_reporting(0);
ini_set('display_errors', 0);

$location = '/usr/local/se/web/wwwroot/temp/';

$url = file_get_contents($path);

$mpdf = new \Mpdf\Mpdf();
$mpdf->allow_charset_conversion = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->writeHTML($url);

ob_clean();

$pathArchive = $location . md5(uniqid()) . '-' . time() . '.pdf';

$mpdf->Output($pathArchive);

return $pathArchive;
exit(); }

SOLUTION

Phpmailer allows sending two parameters:

$pathArchive = generatePDFfromURL($path);
$pathArchive2 = 'olaarquivo.pdf';

$mail->AddAttachment($pathArchive, $pathArchive2);  

1 answer

1


Only pass name as second parameter:

$mail->AddAttachment($pathArchive, "novo_nome.pdf");
  • 1

    yes Leo exactly, I just saw in the documentation that Phpmailer allows this. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.