Skip to content

Instantly share code, notes, and snippets.

@v9n
Created August 17, 2012 07:35
Show Gist options
  • Select an option

  • Save v9n/3376770 to your computer and use it in GitHub Desktop.

Select an option

Save v9n/3376770 to your computer and use it in GitHub Desktop.
sample.php
public static function send($to, $from, $subject, $message, $html = FALSE, $attach_file=NULL)
{
// Connect to SwiftMailer
(Email::$mail === NULL) and email::connect();
// Determine the message type
$html = ($html === TRUE) ? 'text/html' : 'text/plain';
// Create the message
$message = Swift_Message::newInstance($subject, $message, $html, 'utf-8');
if ($attach_file && file_exists($attach_file)) {
$message->attach(new Swift_Message_Attachment(new Swift_File($attach_file), $filename,));
}
if (is_string($to))
{
// Single recipient
$message->setTo($to);
}
elseif (is_array($to))
{
if (isset($to[0]) AND isset($to[1]))
{
// Create To: address set
$to = array('to' => $to);
}
foreach ($to as $method => $set)
{
if ( ! in_array($method, array('to', 'cc', 'bcc'), true))
{
// Use To: by default
$method = 'to';
}
// Create method name
$method = 'add'.ucfirst($method);
if (is_array($set))
{
// Add a recipient with name
$message->$method($set[0], $set[1]);
}
else
{
// Add a recipient without name
$message->$method($set);
}
}
}
if (is_string($from))
{
// From without a name
$message->setFrom($from);
}
elseif (is_array($from))
{
// From with a name
$message->setFrom($from[0], $from[1]);
}
return Email::$mail->send($message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment