Last active
May 10, 2023 16:39
-
-
Save parzibyte/0b18675d359f30508fc9045df30c3e5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once "vendor/autoload.php"; | |
use PHPMailer\PHPMailer\PHPMailer; | |
$phpMailer = new PHPMailer; | |
# Puede ser ruta relativa o absoluta | |
$nombreDelDocumento = "factura.pdf"; | |
if (!file_exists($nombreDelDocumento)) { | |
exit("El archivo $nombreDelDocumento no existe"); | |
} | |
try { | |
$phpMailer->setFrom("AQUÍ VA EL CORREO DEL REMITENTE", "AQUÍ VA EL REMITENTE"); # Correo y nombre del remitente | |
$phpMailer->addAddress("AQUÍ VA EL CORREO DEL DESTINATARIO"); # El destinatario | |
$phpMailer->Subject = "Archivo adjunto"; # Asunto | |
$phpMailer->Body = "Hola, amigo. Estamos probando los archivos adjuntos."; # Cuerpo en texto plano | |
// Aquí la magia: | |
$phpMailer->addAttachment($nombreDelDocumento); | |
if (!$phpMailer->send()) { | |
echo "Error enviando correo: " . $phpMailer->ErrorInfo; | |
} | |
# Opcionalmente podrías eliminar el archivo después de enviarlo, si quieres | |
// if (file_exists($nombreDelDocumento)) { | |
// unlink($nombreDelDocumento); | |
// } | |
echo "Enviado correctamente"; | |
} catch (Exception $e) { | |
echo "Excepción: " . $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment