Skip to content

Instantly share code, notes, and snippets.

@pixelbrackets
Last active April 26, 2023 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbrackets/dbfe08765e5c7780db72b2dda66dfa2c to your computer and use it in GitHub Desktop.
Save pixelbrackets/dbfe08765e5c7780db72b2dda66dfa2c to your computer and use it in GitHub Desktop.
PHP Mail Transport Test - Swiftmailer & Symfony Mailer standalone scripts, instantly usable without frameworks
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// composer require swiftmailer/swiftmailer
require_once 'vendor/autoload.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('10.10.10.1', 25));
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Testmail using Swiftmailer Standalone'))
->setFrom(['sender@example.com' => 'John Doe'])
->setTo(['recipient@example.com'])
->setBody('Here is the message itself');
// Send the message
$result = $mailer->send($message);
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// composer require symfony/mailer
require_once 'vendor/autoload.php';
// Create the Transport
$transport = Symfony\Component\Mailer\Transport::fromDsn('smtp://10.10.10.1:25');
//$transport = Symfony\Component\Mailer\Transport::fromDsn('smtp://10.10.10.1:25?verify_peer=0');
// Create the Mailer using your created Transport
$mailer = new Symfony\Component\Mailer\Mailer($transport);
// Create a message
$email = (new Symfony\Component\Mime\Email())
->from('sender@example.com')
->to('recipient@example.com')
->priority(Email::PRIORITY_HIGHEST)
->subject('Testmail using Symfony Mailer Standalone')
->text('Here is the message itself')
->html('<strong>Here is the message itself</strong>');
// Send the message
$mailer->send($email);
@pixelbrackets
Copy link
Author

Swiftmailer is EOL since 2021/11. Symfony Mailer is the official successor.

The above scripts are usefull to debug mail transport on servers (most often firewall setups), without initializing a whole framework or mail GUIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment