Created
June 2, 2018 01:10
-
-
Save luscas/4688e2d90100126a619e8399cd55bd9d to your computer and use it in GitHub Desktop.
Send mail with attach
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 | |
// Import PHPMailer classes into the global namespace | |
// These must be at the top of your script, not inside a function | |
use PHPMailer\PHPMailer\PHPMailer; | |
use PHPMailer\PHPMailer\Exception; | |
//Load Composer's autoloader | |
require 'vendor/autoload.php'; | |
$mail = new PHPMailer(true); // Passing `true` enables exceptions | |
try { | |
//Server settings | |
$mail->SMTPDebug = 2; // Enable verbose debug output | |
$mail->isSMTP(); // Set mailer to use SMTP | |
$mail->Host = '...'; // Specify main and backup SMTP servers | |
$mail->SMTPAuth = true; // Enable SMTP authentication | |
$mail->Username = '...'; // SMTP username | |
$mail->Password = '...'; // SMTP password | |
$mail->SMTPSecure = '...'; // Enable TLS encryption, `ssl` also accepted | |
$mail->Port = ...; // TCP port to connect to | |
[...] corpo do e-mail [...] | |
//Attachments [Arquivo] | |
$mail->addAttachment('/var/tmp/file.tar.gz'); # Adiciona arquivo | |
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); # nome opcional | |
$mail->send(); | |
echo 'Message has been sent'; | |
} catch (Exception $e) { | |
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment