Skip to content

Instantly share code, notes, and snippets.

@Deele
Last active November 4, 2019 15:34
Show Gist options
  • Save Deele/7d0dbf037d9cadacd8877c30fede353d to your computer and use it in GitHub Desktop.
Save Deele/7d0dbf037d9cadacd8877c30fede353d to your computer and use it in GitHub Desktop.
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);
$autoloadPath = 'autoload.php';
if (!file_exists($autoloadPath)) {
$autoloadPath = '../' . $autoloadPath;
if (!file_exists($autoloadPath)) {
$autoloadPath = '../' . $autoloadPath;
}
}
$autoloader = require_once $autoloadPath;
function getValue($name, $default = NULL) {
if (isset($_POST[$name])) {
return (!empty($_POST[$name]) ? $_POST[$name] : NULL);
}
return $default;
}
function createEmailTarget($targets) {
$result = [];
foreach (explode(',', $targets) as $target) {
$targetParts = explode(' ', $target);
if (count($targetParts) > 1) {
$email = end($targetParts);
unset($targetParts[count($targetParts) - 1]);
$result[trim($email, '<>""')] = implode(' ', $targetParts);
}
else {
$result[] = $target;
}
}
return $result;
}
// Config mailserver
$mailTransportType = getValue('mailTransportType', 'smtp');
$mailTransportPath = getValue('mailTransportPath', '/usr/sbin/sendmail');
$mailTransportMode = getValue('mailTransportMode', '-bs');
$mailServer = getValue('mailServer', 'localhost');
$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
if ($mailTransportType === 'native') {
$transport = new Swift_SmtpTransport();
}
elseif ($mailTransportType === 'sendmail') {
$transport = new Swift_SmtpTransport(
$mailTransportPath .
' ' .
$mailTransportMode
);
}
else {
$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)
->setBody($messageBody);
$senders = createEmailTarget($messageFrom);
$message->setFrom($senders);
$recipients = createEmailTarget($messageTo);
$message->setTo($recipients);
$failures = [];
try {
if ($mailer->send($message, $failures)) {
echo '<h2 class="alert alert-success" role="alert">Successfully sent email</h2>';
}
else {
echo '<h2 class="alert alert-warning" role="alert">Error during email sending procedure</h2>';
}
echo '<div class="card"><div class="card-body">';
echo '<table class="table table-sm">';
foreach ($senders as $k => $v) {
echo '<tr>';
echo '<td>Sender</td>';
if (is_int($k)) {
echo '<td colspan="2" class="text-monospace">' . print_r($v,
TRUE) . '</td>';
}
else {
echo '<td class="text-monospace">' . print_r($v, TRUE) . '</td>';
echo '<td class="text-monospace">' . print_r($k, TRUE) . '</td>';
}
echo '</tr>';
}
foreach ($recipients as $k => $v) {
echo '<tr>';
echo '<td>Recipient</td>';
if (is_int($k)) {
echo '<td colspan="2" class="text-monospace">' . print_r($v,
TRUE) . '</td>';
}
else {
echo '<td class="text-monospace">' . print_r($v, TRUE) . '</td>';
echo '<td class="text-monospace">' . print_r($k, TRUE) . '</td>';
}
echo '</tr>';
}
if (!empty($failures)) {
foreach ($failures as $k => $v) {
echo '<tr>';
echo '<td>Failure</td>';
echo '<td>' . print_r($k, TRUE) . '</td>';
echo '<td><pre class="m-0"><code>' . print_r($v,
TRUE) . '</code></pre></td>';
echo '</tr>';
}
}
foreach (explode("\n", $mailLogger->dump()) as $k => $v) {
if (empty($v)) {
continue;
}
echo '<tr>';
echo '<td>Log</td>';
echo '<td colspan="2"><pre class="m-0 text-monospace"><code>' .
print_r(htmlspecialchars($v), TRUE) .
'</code></pre></td>';
echo '</tr>';
}
echo '</table>';
} 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 class="m-0"><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="smtp"<?= ($mailTransportType === 'smtp' ? ' checked' : '') ?>>
<label class="custom-control-label"
for="mailTransportType1">SMTP</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" id="mailTransportType2"
name="mailTransportType" class="custom-control-input"
value="sendmail"<?= ($mailTransportType === 'sendmail' ? ' checked' : '') ?>>
<label class="custom-control-label"
for="mailTransportType2">Sendmail</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" id="mailTransportType3"
name="mailTransportType" class="custom-control-input"
value="native"<?= ($mailTransportType === 'native' ? ' checked' : '') ?>>
<label class="custom-control-label"
for="mailTransportType3">PHP</label>
</div>
<small id="mailServerEncryptionHelp"
class="form-text text-muted">Mail transport type
</small>
</div>
</div>
<div class="form-group row form-group_sendmail">
<label class="col-sm-2 col-form-label"
for="mailTransportPath">MTA path</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mailTransportPath"
aria-describedby="mailTransportPathHelp"
placeholder="Enter address"
value="<?= $mailTransportPath ?>"
name="mailTransportPath">
<small id="mailTransportPathHelp" class="form-text text-muted">
The absolute path to the locally installed MTA.
</small>
</div>
</div>
<div class="form-group row form-group_sendmail">
<label class="col-sm-2 col-form-label"
for="mailTransportMode">Mode</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mailTransportMode"
aria-describedby="mailTransportModeHelp"
placeholder="Enter address"
value="<?= $mailTransportMode ?>"
name="mailTransportMode">
<small id="mailTransportModeHelp" class="form-text text-muted">
Mail
transport mode (<code>-bs</code> or <code>-t</code>). Not
sure which option to choose? Go with <em>bs</em>. You can
read more about the above two modes in the <a
href="http://swiftmailer.org/docs/sendmail-transport">Swift
Mailer documentation</a>.
</small>
</div>
</div>
<div class="form-group row form-group_smtp">
<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">SMTP
server address
</small>
</div>
</div>
<div class="form-group row form-group_smtp">
<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 form-group_smtp">
<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">The type of encryption which
should be used (if any)
</small>
</div>
</div>
<div class="form-group row form-group_smtp">
<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">
A username required by the SMTP server (leave blank if not
required)
</small>
</div>
</div>
<div class="form-group row form-group_smtp">
<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">
A password required by the SMTP server (leave blank if not
required)
</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="text" class="form-control" id="messageFrom"
aria-describedby="messageFromHelp"
placeholder="Enter e-mail" value="<?= $messageFrom ?>"
name="messageFrom">
<small id="messageFromHelp" class="form-text text-muted">Message
"From:" part. Use "Name &lt;[email protected]&gt;" format,
to provide sender name.
</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="text" 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. Use "Name &lt;[email protected]&gt;" format, to
provide recipient name.
</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">
<textarea class="form-control" id="messageBody"
aria-describedby="messageBodyHelp"
placeholder="Enter text"
name="messageBody"
rows="5"><?= $messageBody ?></textarea>
<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>
<script>
$(function () {
function render() {
let mailTransportType = $('[name="mailTransportType"]:checked').val();
if (mailTransportType === 'smtp') {
$('.form-group_sendmail').hide();
$('.form-group_smtp').show();
}
else if (mailTransportType === 'sendmail') {
$('.form-group_sendmail').show();
$('.form-group_smtp').hide();
}
else {
$('.form-group_sendmail').hide();
$('.form-group_smtp').hide();
}
}
$('[name="mailTransportType"]').on('change', function () {
render();
});
render();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment