Simple PHP Mail Function Using Swift Mailer

Using the built-in mail() function is PHP is pretty cool but not cool if you’re sending bulk or large mails and secondly it always head for the Spam box 😀 because not all needed headers are there. But using a mailer, I use Swift Mailer and it’s cool for me, get Swift Mailer and also read some docs about it. The part I want to do here is create a simple function like the mail() function, to send mail via the Swift Mailer, this function might not have all features Swift Mailer has but I guess it has the vital ones 😉

<?php
function send_mail ( $from, $to, $subject, $body, $html = false, $to_each = true ) {
	//$from variable must be an array and there must be receivers
	if ( !is_array ( $from ) ) {
		die ( 'Email: $from must be array' );
	}
	if( !$to )
		die( 'No receivers' );

	//Receivers are in comma separated, converting it to arrays Swift uses
	$to = explode ( ',', $to );

	//Remove repeated emails
	$to = array_unique( $to );

	//Start new instance
	$transport = Swift_MailTransport::newInstance();

	$mailer = Swift_Mailer::newInstance ( $transport );

	//Set the message data - subjects, from and body
	$message = Swift_Message::newInstance ( $subject )
		->setFrom ( $from )
		->setBody ( $body )
		;

	//If message is HTML, set header
	if ( $html ) {
		$header = $message->getHeaders()->get ( 'Content-Type' );
		$header->setValue ( 'text/html' );
	}

	//If to be sent to each mail at a time - Receivers won't see other emails that received it too
	if ( $to_each ) {
		foreach ( $to as $address) {
			//Send only to valid emails to escape exceptions
			if( Swift_Validate::email( $address ) ) {
				$message->setTo ( $address );
				$result = $mailer->send ( $message );
			}
		}
	}
	//Send to all mails at a time
	else {
		$message->setTo ( $to );
		$result = $mailer->send( $message );
	}
	return $result;
}

Arguments:

$from = This is the sender’s name and email address and must be in this format

array( '[email protected]' => 'Sender Name' )

$to = This are the receivers in comma separated e.g.

[email protected], [email protected]

$subject = The message subject

$body = The message content

$html = The default value of this argument when not supplied is false, which means the mail will be sent as plain text. Set to true if it’s a HTML content mail

$to_each = This set to true will send mail to each user at a time, making it more secure that a receiver won’t see other receivers too, this takes time than when set to false. But false…other receivers can see emails that received it too.

Example

Simple mail sending:

send_mail( array( '[email protected]' => 'Don Jajo' ), '[email protected], [email protected]', 'Testing Mail', 'This is the body' );

Sending HTML Content:

send_mail( array( '[email protected]' => 'Don Jajo' ), '[email protected], [email protected]', 'Testing Mail', '<strong><u>This is the body</u></strong>', true );

NB: The Swift_required.php must be included on the PHP file before this function works.