Created
December 2, 2015 10:04
-
-
Save chrisvoo/52a785b1265a08372877 to your computer and use it in GitHub Desktop.
Testing external SMTP server with PHP
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 | |
# 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