Web Development Guides and Tutorials

  • facebook
  • twitter
  • github

PHP Mailer

11th May 2015 10:16:02 Progress & Learning, PHP by Kerry Owston

PHP Mailer

What is PHP Mailer

PHP Mailer is a PHP LIbrary. A Library is a collection of codes (functions, properties etc) that can be resuable across a project or multiple projects. PHP Mailer is a library with a collection of functions that handel email in many scenarios that may be required.

How to use PHP Mailer

There are several way to use PHP mailer many of these I know little about like composer and GIT.

I’m going with good old download zip.

Once I have copied the files from the zip folder into my project I will need to require the phpmailerautoloader.php file into the file i’m working with like below:

require 'phpmailer/PHPMailerAutoload.php';

This tells the script to look in the phpmailer folder for the PHPMailerAutoload.php file. This allows all of the functionality available in PHP Mailer linbrary is available to this file. Quite amazing really, also reminds me of the way helpers work in SMVCF.

On a side note require is just like include except instead of creating a warning if there is an error it willl stop the scipt from running entirely.

PHP Mailer is a class so I need to create a new instance of it.

$mail = new PHPMailer;

From here I just add in the email perameters it’s just a difrent way of writing them. i also need to tell it to use the sendmail transport

$mail->isSendmail();

Altogether is should liok like this:

<?php

require('phpmailer/PHPMailerAutoload.php');

$mail = new PHPMailer;

$mail->isSendmail();

?>

Now this is in place I just add a new line of code for each, they’re are pretty self explanitory, see below.

How to send an email

These exaamples below are a small portion of the diffrent possibilities avaiable witin PHP Mailer, these are the cmmon ones.

$mail->setFrom('[email protected]', 'First Last');

This above sets the to parameter and expects sender email address as a string and sender name as a string

$mail->addAddress('[email protected]', 'John Doe');

The above set the from parameter that woud have been a header and expects recipitent email address as a string and reciptent name as a string.

$mail->Subject = 'PHPMailer sendmail test';

The above sets the subject line this time it’s not a function it’s just a varable with a value

$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

The above sets the email message for html emails and is looking for an external file which the file_get_contents() function is able to locate

$mail->AltBody = 'This is a plain-text message body';

The above is an alternaive for plain text

Anyheaders are wrapped up for me behind the scenes, basicly PHPMailer is doing the legwork for this.

Sending email using PHP Mailer is made up of several of these lines used as required.

And when the above is used in practice it looks like the example below.

<?php

require ('../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSendmail();
$mail->setFrom('[email protected]', 'First Last');
$mail->addAddress('[email protected]', 'John Doe');
$mail->Subject = 'PHPMailer sendmail test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';

?>

This is taken from a PHP Mailer example that I have disected somewhat.

My experimentation

i sent an email to David as my exerimentation – the details ahve been changed

This is the email sent:

<?php

require('phpmailer/PHPMailerAutoload.php');

$mail = new PHPMailer;

$mail->isSendmail();

$mail->setFrom('[email protected]', 'Sender name here');

$mail->addReplyTo('[email protected]', 'reply to name here');

$mail->addAddress('[email protected]', 'recipitent name here');

$mail->Subject = 'Hey you have an email';

$mail->msgHTML(file_get_contents('content.html'), dirname(__FILE__));

$mail->AltBody = 'some default text if html is not possible';

$mail->addAttachment('hey-there.jpg');

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

This example shows attachments in use, also it shows how to uses another file as the html email

Using PHP Mailer in SMVCF

One of the thing David noticed while building SMVCF what that the inconsistent format made it difficult to remember what xpecte what. A PHP Mailer helper class was created to use PHP Mailer with easier to remember parameters while still retaining it’s functionality.

How is this used?

The example below demonstrates how an email can be sent using SMVCF and how to include this in the code. 

	public function task5(){

		$data['title'] = 'php mailer';

		$mail = new \helpers\phpmailer\mail();

		$mail = new \helpers\phpmailer\mail();
		$mail->setFrom('[email protected]');
		$mail->addAddress('[email protected]');
		$mail->subject('Email');
		$mail->body("<h1>Just a test</h1><p>another test</p>");
		$mail->send();

		View::rendertemplate('header', $data);
		View::render('dailyTasks/task5', $data);
		View::rendertemplate('footer', $data);

	}

The route for this is:

Router::any('/task5', '\controllers\[email protected]');

So when I visit the corresponding page an email will automaticly be sent to me

my experimentation

I sent an email to David as my exerimentation – the details ahve been changed:

Below is an example of this.

Assesment

I now have a much clearer understanding of how emails are sent why using a third party library is a much more efficiant choice

Davids Assesment

ddfdf