Skip to content

Instantly share code, notes, and snippets.

@fracsi
Last active December 21, 2020 21:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fracsi/5c9c76fca6a27b5156abde8596274b21 to your computer and use it in GitHub Desktop.
Save fracsi/5c9c76fca6a27b5156abde8596274b21 to your computer and use it in GitHub Desktop.
DSN Feature for SwiftMailer
<?php
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.example.org', 25)
);
// NOTIFY=NEVER
$mailer->registerPlugin(new DsnPlugin([new NotifyHandler('never')]));
// --- OR ---
// NOTIFY=SUCCESS,FAILURE
$mailer->registerPlugin(new DsnPlugin([new NotifyHandler('success,failure')]));
<?php
/**
* Interface DsnHandlerInterface
*/
interface DsnHandlerInterface
{
}
<?php
/**
* Class DsnPlugin.
*/
class DsnPlugin implements Swift_Events_TransportChangeListener
{
/**
* @var DsnHandlerInterface[]
*/
private $_dsnHandlers;
/**
* DsnPlugin constructor.
*
* @param DsnHandlerInterface[] $dsnHandlers
*/
public function __construct(array $dsnHandlers = [])
{
$this->_dsnHandlers = $dsnHandlers;
}
/**
* Invoked just before a Transport is started.
*
* @param Swift_Events_TransportChangeEvent $evt
*/
public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
{
$transport = $evt->getTransport();
if ($this->_dsnHandlers && $transport instanceof Swift_Transport_EsmtpTransport) {
$handlers = $transport->getExtensionHandlers();
$handlers = array_merge($handlers, $this->_dsnHandlers);
$transport->setExtensionHandlers($handlers);
}
}
/**
* Invoked immediately after the Transport is started.
*
* @param Swift_Events_TransportChangeEvent $evt
*/
public function transportStarted(Swift_Events_TransportChangeEvent $evt)
{
//NOOP
}
/**
* Invoked just before a Transport is stopped.
*
* @param Swift_Events_TransportChangeEvent $evt
*/
public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
{
//NOOP
}
/**
* Invoked immediately after the Transport is stopped.
*
* @param Swift_Events_TransportChangeEvent $evt
*/
public function transportStopped(Swift_Events_TransportChangeEvent $evt)
{
//NOOP
}
}
<?php
/**
* Class NotifyHandler.
*/
class NotifyHandler implements Swift_Transport_EsmtpHandler, DsnHandlerInterface
{
/**
* @var bool
*/
private $_hasDsn = false;
/**
* @var string
*/
private $_notify;
/**
* NotifyHandler constructor.
*
* @param $notify
*/
public function __construct($notify)
{
$this->_notify = strtoupper($notify);
}
/**
* Get the name of the ESMTP extension this handles.
*
* @return bool
*/
public function getHandledKeyword()
{
return 'DSN';
}
/**
* Set the parameters which the EHLO greeting indicated.
*
* @param string[] $parameters
*/
public function setKeywordParams(array $parameters)
{
$this->_hasDsn = true;
}
/**
* Runs immediately after a EHLO has been issued.
*
* @param Swift_Transport_SmtpAgent $agent to read/write
*/
public function afterEhlo(Swift_Transport_SmtpAgent $agent)
{
//NOOP
}
/**
* Get params which are appended to MAIL FROM:<>.
*
* @return string[]
*/
public function getMailParams()
{
return [];
}
/**
* Get params which are appended to RCPT TO:<>.
*
* @return string[]
*/
public function getRcptParams()
{
return $this->_hasDsn && $this->_notify ? ['NOTIFY='.$this->_notify] : [];
}
/**
* Runs when a command is due to be sent.
*
* @param Swift_Transport_SmtpAgent $agent to read/write
* @param string $command to send
* @param int[] $codes expected in response
* @param string[] $failedRecipients to collect failures
* @param bool $stop to be set true by-reference if the command is now sent
*/
public function onCommand(
Swift_Transport_SmtpAgent $agent,
$command,
$codes = [],
&$failedRecipients = null,
&$stop = false
) {
//NOOP
}
/**
* Returns +1, -1 or 0 according to the rules for usort().
*
* This method is called to ensure extensions can be execute in an appropriate order.
*
* @param string $esmtpKeyword to compare with
*
* @return int
*/
public function getPriorityOver($esmtpKeyword)
{
return 0;
}
/**
* Returns an array of method names which are exposed to the Esmtp class.
*
* @return string[]
*/
public function exposeMixinMethods()
{
return [];
}
/**
* Tells this handler to clear any buffers and reset its state.
*/
public function resetState()
{
//NOOP
}
}
@BoShurik
Copy link

Is it possible to set ORCPT parameter?
RCPT TO:<user@example.com> NOTIFY=SUCCESS,FAILURE ORCPT=rfc822;user@example.com

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