Skip to content

Instantly share code, notes, and snippets.

@rogeriopradoj
Created July 2, 2017 19:38
Show Gist options
  • Save rogeriopradoj/bcf9e502c085665189636d208af18923 to your computer and use it in GitHub Desktop.
Save rogeriopradoj/bcf9e502c085665189636d208af18923 to your computer and use it in GitHub Desktop.
mail.php - swiftmailer + mailhog - send mail via smtp
{
"require": {
"swiftmailer/swiftmailer": "^6.0"
}
}
<?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(['john@doe.com' => 'John Doe'])
->setTo(['receiver@domain.org', 'other@domain.org' => '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