Skip to content

Instantly share code, notes, and snippets.

@vivirenremoto
Created August 25, 2011 02:52
Show Gist options
  • Save vivirenremoto/1169870 to your computer and use it in GitHub Desktop.
Save vivirenremoto/1169870 to your computer and use it in GitHub Desktop.
Send emails via smtp with sendgrid.com
<?php
// sendgrid.com is a cheap service to send emails
// swift mailer class is required, you can download it in swiftmailer.org
require 'class/swift/swift_required.php';
class Mailer{
var $author;
var $from;
var $to;
var $subject;
var $text;
function send(){
$to = array();
$from = array( $this->from => $this->author );
if( !is_array( $this->to ) ) $to[] = $this->to;
else $to = $this->to;
// setup Swift mailer parameters
$transport = Swift_SmtpTransport::newInstance( 'smtp.sendgrid.net', 25 );
$transport->setUsername( SWIFT_EMAIL );
$transport->setPassword( SWIFT_PASS );
$swift = Swift_Mailer::newInstance( $transport );
// create a message (subject)
$message = new Swift_Message( $this->subject );
// attach the body of the email
$message->setFrom( $from );
$message->setBody( $this->text, 'text/html' );
$message->setTo( $to );
// send email
if( $recipients = $swift->send( $message, $failures ) ){
echo 'Message sent out to ' . $recipients . ' users';
}else{
print_r( $failures );
}
}
}
// define smtp account
define('SWIFT_EMAIL','[email protected]');
define('SWIFT_PASS','*******');
// example
$mail = new Mailer;
$mail->author = 'Brucut';
$mail->from = '[email protected]';
$mail->to = $email; // this variable can be also an array with a bunch of mails
$mail->subject = "Datos de acceso a brucut";
$mail->text = require 'views/email/register.htm';
$mail->send();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment