Skip to content

Instantly share code, notes, and snippets.

@petertwise
Last active August 19, 2018 19:51
Embed
What would you like to do?
example of swiftmailer usage
<?php
define('SITE_EMAIL', 'no-reply@example.com');
define('SITE_NAME', 'My Project');
function send_password_reset($email, $name, $resetlink) {
require_once( 'swiftmailer/swift_required.php' );
$subject = 'Resetting your password for ' . SITE_NAME;
$text_body = "Hello,\n\nTo reset your password, visit " . $resetlink;
$html_body = '<p>Hello,</p>
<p>To reset your password, click here:</p>
<p>
<a href="' . $resetlink . '"
style="-webkit-text-size-adjust: none; background: #6ec800; border-color: #6ec800;
border-style: solid; border-width: 10px 18px; box-sizing: border-box; color: #000;
display: inline-block; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
text-decoration: none;">
Reset Password
</a>
</p>';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject($subject);
// Set the From address with an associative array
$message->setFrom( array( SITE_EMAIL => SITE_NAME ) );
// Set the To addresses with an associative array
$message->setTo( array( $email => $name ) );
// Set the HTML version
$message->setBody($html_body, 'text/html');
// Add alternative parts with addPart() (alternative plain text version)
$message->addPart($text_body, 'text/plain');
$result = $mailer->send($message);
if ( $result ) {
// send worked
return TRUE;
}
else {
// failure ($result == 0)
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment