Skip to content

Instantly share code, notes, and snippets.

@chrisvoo
Created December 2, 2015 10:04
Show Gist options
  • Save chrisvoo/52a785b1265a08372877 to your computer and use it in GitHub Desktop.
Save chrisvoo/52a785b1265a08372877 to your computer and use it in GitHub Desktop.
Testing external SMTP server with PHP
<?php
# use composer : php composer.phar require swiftmailer/swiftmailer @stable
require __DIR__ . '/vendor/autoload.php';
// Approach 1: Change the global setting (suggested)
Swift_Preferences::getInstance()->setCharset('utf-8');
$smtp_host = '1.2.3.4';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance($smtp_host, 25);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$logger = new Swift_Plugins_Loggers_EchoLogger();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
// Pass it as a parameter when you create the message
$message = Swift_Message::newInstance('Subject here', 'My amazing body');
$message->setFrom(array('[email protected]' => 'No reply'));
$message->setTo(array('[email protected]' => 'Christian'));
// Or set it after like this
$message->setBody('My <em>amazing</em> body', 'text/html');
// Send the message
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
} else {
echo "Sent";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment