0

Recupero un PDF de un WS y lo manipulo para descargar en pantalla mediante un botón. Ahora necesito saber cómo poder hacer para enviarlo como archivo adjunto por email.

En el front tengo un modal que te pide una dirección de email y tengo esta función reciclada que te manda el email pero no se como adjuntar el archivo para enviarlo:

public function sendBudget(Request $request)
{
    // Gets sent variables variables
$this->JSdata = $request->all();

$this->toAddress = $this->JSdata["email"];
$this->body = $this->JSdata["body"];
$this->html = $this->JSdata["html"];

//Server settings
$this->mail->SMTPDebug = 0;                                       // Enable verbose debug output
$this->mail->isSMTP();                                            // Set mailer to use SMTP
$this->mail->Host = config('mail.host');               // Specify main and backup SMTP servers
$this->mail->SMTPAuth = true;                                   // Enable SMTP authentication
$this->mail->Username = config('mail.username');           // SMTP username
$this->mail->Password = config('mail.password');           // SMTP password
$this->mail->SMTPSecure = config('mail.encryption');         // Enable TLS encryption, `ssl` also accepted
$this->mail->Port = config('mail.port');               // TCP port to connect to
$this->mail->CharSet = 'UTF-8';

// Content
$this->mail->isHTML(true);                                  // Set email format to HTML

// Fixes connection errors
$this->mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

if (!$this->mail->send()) {
    unlink('coste-seguro.pdf');
    return response()->json(['error' => 'KO', 'customClass'=> $this->modalKOClass, 'title'=> $this->modalKOTitle, 'body'=> $this->modalKOBody, 'button'=> $this->modalKOButton1, 'e' => $this->mail->ErrorInfo]);
} else {
    unlink('coste-seguro.pdf');
    return response()->json(['success' => 'OK', 'customClass'=>  $this->modalOKClass, 'title'=> $this->modalOKTitle, 'body'=> $this->modalOKBody, 'button'=> $this->modalOKButton1]);
}

}

He probado usando este método pero me devuelve error 500:

$this->mail->attach(asset($this->JSdata["budgetURL"]));

Y esto también:

$this->mail->addAttachment($this->JSdata["budgetURL"]);
1
  • Ayudaría saber el mensaje de ese error 500 que obtienes. el 10 nov. 2021 a las 17:02

1 respuesta 1

0

Probando encontré la forma de hacerlo, al final tenía que pasar el parámetro de la siguiente forma: $this->JSdata['attachment'] = $this->JSdata['budgetURL'];

public function sendBudget(Request $request)
    {
        // Gets sent variables variables
        $this->JSdata = $request->all();
        $this->toAddress = $this->JSdata["email"];
        $this->body = $this->JSdata["body"];
        $this->html = $this->JSdata["html"];
        $this->JSdata['attachment'] = $this->JSdata['budgetURL'];
        $this->document();

        //Server settings
        $this->mail->SMTPDebug = 0;                                       // Enable verbose debug output
        $this->mail->isSMTP();                                            // Set mailer to use SMTP
        $this->mail->Host = config('mail.host');               // Specify main and backup SMTP servers
        $this->mail->SMTPAuth = true;                                   // Enable SMTP authentication
        $this->mail->Username = config('mail.username');           // SMTP username
        $this->mail->Password = config('mail.password');           // SMTP password
        $this->mail->SMTPSecure = config('mail.encryption');         // Enable TLS encryption, `ssl` also accepted
        $this->mail->Port = config('mail.port');               // TCP port to connect to
        $this->mail->CharSet = 'UTF-8';

        // Content
        $this->mail->isHTML(true);                                  // Set email format to HTML

        // Fixes connection errors
        $this->mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

        if (!$this->mail->send()) {
            //unlink($this->JSdata['budgetURL']);
            return response()->json(['error' => 'KO', 'customClass'=> $this->modalKOClass, 'title'=> $this->modalKOTitle, 'body'=> $this->modalKOBody, 'button'=> $this->modalKOButton1, 'e' => $this->mail->ErrorInfo]);
        } else {
            //unlink($this->JSdata['budgetURL']);
            return response()->json(['success' => 'OK', 'customClass'=>  $this->modalOKClass, 'title'=> $this->modalOKTitle, 'body'=> $this->modalOKBody, 'button'=> $this->modalOKButton1]);
        }

    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.