Created
July 2, 2017 19:38
-
-
Save rogeriopradoj/bcf9e502c085665189636d208af18923 to your computer and use it in GitHub Desktop.
mail.php - swiftmailer + mailhog - send mail via smtp
This file contains hidden or 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
{ | |
"require": { | |
"swiftmailer/swiftmailer": "^6.0" | |
} | |
} |
This file contains hidden or 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 | |
require 'vendor/autoload.php'; | |
// Create the Transport | |
$transport = (new Swift_SmtpTransport('localhost', 1025)); | |
// Create the Mailer using your created Transport | |
$mailer = new Swift_Mailer($transport); | |
// Create the message | |
$message = (new Swift_Message()) | |
->setSubject('Your subject') | |
->setFrom(['[email protected]' => 'John Doe']) | |
->setTo(['[email protected]', '[email protected]' => 'A name']) | |
->setBody('Here is the message itself') | |
->addPart('<q>Here is the message itself</q> <b>in bold!</b>', 'text/html') | |
->attach(Swift_Attachment::fromPath('/tmp/file.tar.gz')) | |
->attach(Swift_Attachment::fromPath('/tmp/image.jpg')->setFilename('new.jpg')) | |
; | |
// Send the message | |
try { | |
$result = $mailer->send($message); | |
echo 'Message has been sent'; | |
} | |
catch (\Swift_TransportException $e) { | |
echo 'Message could not be sent.'; | |
echo 'Mailer Error: ' . $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment