Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mail sending tester via Sfiftmailer (originally made for use with `drupal/swiftmailer` and `drupal/smtp` composer packages installed)
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function getValue($name, $default = null) {
if (isset($_POST[$name])) {
return (!empty($_POST[$name]) ? $_POST[$name] : null);
}
return $default;
}
// Config mailserver
$mailTransportType = getValue('mailTransportType', 'smtp');
$mailServer = getValue('mailServer', null);
$mailServerPort = getValue('mailServerPort', 25);
$mailServerEncryption = getValue('mailServerEncryption', null);
$mailServerUsername = getValue('mailServerUsername', null);
$mailServerPassword = getValue('mailServerPassword', null);
// Config message
$messageFrom = getValue('messageFrom', null);
$messageTo = getValue('messageTo', null);
$messageSubject = getValue('messageSubject', 'Test message');
$messageBody = getValue('messageBody', 'Mail transport test message.');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>Mail transport tester</title>
</head>
<body>
<div class="container">
<h1>Mail transport tester</h1>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Begin
$autoloader = require_once 'autoload.php';
$transport = (new Swift_SmtpTransport($mailServer, $mailServerPort, $mailServerEncryption))
->setUsername($mailServerUsername)
->setPassword($mailServerPassword);
$mailer = new Swift_Mailer($transport);
$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
$message = \Swift_Message::newInstance()
->setSubject($messageSubject)
->setFrom($messageFrom)
->setTo($messageTo)
->setBody($messageBody);
try {
if ($mailer->send($message, $failures)) {
echo '<h2 class="alert alert-success" role="alert">Successfully sent email to ' . $messageTo . '</h2>';
} else {
echo '<h2 class="alert alert-warning" role="alert">Error during email sending procedure</h2>';
}
echo '<div class="card"><div class="card-body">';
if (!empty($failures)) {
echo '<p>Failures:</p>';
echo '<pre><code>';
print_r($failures);
echo '</code></pre>';
}
echo '<p>Log:</p>';
echo '<pre><code>';
echo htmlspecialchars($mailLogger->dump());
echo '</code></pre>';
echo '</div></div>';
} catch (Swift_TransportException $e) {
echo '<h2 class="alert alert-danger" role="alert">Exception during email sending procedure</h2>';
echo '<h3>Exception</h3>';
echo '<div class="card"><div class="card-body">';
echo '<pre><code>';
echo htmlspecialchars($e->getMessage());
echo '</code></pre>';
echo '</div></div>';
};
}
?>
<h2>Configuration</h2>
<form method="post" class="mb-4">
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailTransportType">Type</label>
<div class="col-sm-10">
<div class="custom-control custom-radio">
<input type="radio" id="mailTransportType1" name="mailTransportType" class="custom-control-input" value=""<?= ($mailTransportType === 'smtp' ? ' checked' : '') ?>>
<label class="custom-control-label" for="mailTransportType1">SMTP</label>
</div>
<small id="mailServerEncryptionHelp" class="form-text text-muted">Mail transport type</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailServer">Server</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mailServer" aria-describedby="mailServerHelp" placeholder="Enter address" value="<?= $mailServer ?>" name="mailServer">
<small id="mailServerHelp" class="form-text text-muted">Mail transport server</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailServerPort">Port</label>
<div class="col-sm-10">
<input type="number" min="1" max="65535" class="form-control" id="mailServerPort" aria-describedby="mailServerPortHelp" placeholder="Enter number" value="<?= $mailServerPort ?>" name="mailServerPort">
<small id="mailServerPortHelp" class="form-text text-muted">Mail transport server port number. Usually <strong>25</strong> for unencrypted and <strong>587</strong> for encrypted email.</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailServerEncryption">Encryption</label>
<div class="col-sm-10">
<div class="custom-control custom-radio">
<input type="radio" id="mailServerEncryption1" name="mailServerEncryption" class="custom-control-input" value=""<?= (empty($mailServerEncryption) ? ' checked' : '') ?>>
<label class="custom-control-label" for="mailServerEncryption1">No encryption</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" id="mailServerEncryption2" name="mailServerEncryption" class="custom-control-input" value="ssl"<?= ($mailServerEncryption === 'ssl' ? ' checked' : '') ?>>
<label class="custom-control-label" for="mailServerEncryption2">SSL</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" id="mailServerEncryption3" name="mailServerEncryption" class="custom-control-input" value="tls"<?= ($mailServerEncryption === 'tls' ? ' checked' : '') ?>>
<label class="custom-control-label" for="mailServerEncryption3">TLS</label>
</div>
<small id="mailServerEncryptionHelp" class="form-text text-muted">Mail transport encryption</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailServerUsername">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mailServerUsername" aria-describedby="mailServerUsernameHelp" placeholder="No username" value="<?= $mailServerUsername ?>" name="mailServerUsername">
<small id="mailServerUsernameHelp" class="form-text text-muted">Mail transport authentication username</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="mailServerPassword">Password</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mailServerPassword" aria-describedby="mailServerPasswordHelp" placeholder="No password" value="<?= $mailServerPassword ?>" name="mailServerPassword">
<small id="mailServerPasswordHelp" class="form-text text-muted">Mail transport authentication password</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="messageFrom">Sender e-mail address</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="messageFrom" aria-describedby="messageFromHelp" placeholder="Enter e-mail" value="<?= $messageFrom ?>" name="messageFrom" required>
<small id="messageFromHelp" class="form-text text-muted">Message "From:" part</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="messageTo">Recipient e-mail address</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="messageTo" aria-describedby="messageToHelp" placeholder="Enter e-mail" value="<?= $messageTo ?>" name="messageTo" required>
<small id="messageToHelp" class="form-text text-muted">Message "To:" part</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="messageSubject">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="messageSubject" aria-describedby="messageSubjectHelp" placeholder="Enter text" value="<?= $messageSubject ?>" name="messageSubject" required>
<small id="messageSubjectHelp" class="form-text text-muted">Message "Subject:" part</small>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="messageBody">Body</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="messageBody" aria-describedby="messageBodyHelp" placeholder="Enter text" value="<?= $messageBody ?>" name="messageBody">
<small id="messageBodyHelp" class="form-text text-muted">Message "Body:" part</small>
</div>
</div>
<button type="submit" class="btn btn-primary">Send email</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.