Note: each tutorial contains information about one specific part of Spoon. If something is unclear, post a comment.

Email

Written by Dave Lens -

Prerequisites

What will you learn?

  • Sending a basic e-mail
  • Sending an e-mail using SMTP
  • Use a template as an e-mail body

Features

Wether your application is large or small, at one point or another you will need e-mail functionality. SpoonEmail provides a fast and easy way to deal with this.
It supports sending e-mail with PHP (using mail()) or SMTP (using fsockopen()) with or without SSL/TLS.

Basics

So let's start with the most basic e-mail you can send. This will send the contents to two recipients (one regular, one in CC):

// required classes
require_once 'spoon/spoon.php';

// new e-mail instance
$email = new SpoonEmail();

// this is the folder your compiled e-mail templates will end up in
$email->setTemplateCompileDirectory('/path/to/your/preferred/compile/directory');

// set e-mail headers
$email->setFrom('crimson.king@thunderclap.mw', 'The Crimson King');
$email->addRecipient('roland.deschain@gilead.mw');
$email->addCC('eddie.dean@brooklyn.ny');

// set variables
$variables = array();
$variables['replaceMe'] = 'e-mail';

// set e-mail contents
$email->setSubject("It's all 19");
$email->setHTMLContent('<strong>This is an {$replaceMe}</strong>.', $variables);

// send the e-mail
$email->send();

All but two functions in the above example are self-explanatory: setTemplateCompileDirectory() and setHTMLContent().

setTemplateCompileDirectory()

The reason why we have to set a compile directory is because internally, SpoonEmail will generate a template file with the e-mail contents, which in turn will be compiled. These files need to be written in a directory of choice.

This makes the use of SpoonTemplate mechanics available; building up an array to assign variables, iterations and options.

setHTMLContent()

This function has two parameters:

  • $content: This can be either the full body of an e-mail or the path to a template file.
  • $variables: The array with variables to be parsed in the contents.

For more information about the buildup of the $variables array, there are the SpoonTemplate tutorials.

SMTP example - Gmail

Now that you know the basics, let's take the same e-mail used above with three important changes:

  • We connect to Gmail with SMTP using their TLS security layer.
  • We use an external template file as our e-mail body.
  • The debug mode is set to true, so we can use getOutput()
// required classes
require_once 'spoon/spoon.php';

// new e-mail instance
$email = new SpoonEmail();

// enable the debug mode, this will let you use getOutput()
$email->setDebug(true);

// this is the folder your compiled e-mail templates will end up in
$email->setTemplateCompileDirectory('/path/to/your/preferred/compile/directory');

// SMTP info, remember to use your Gmail account credentials
$email->setSMTPSecurity('tls');
$email->setSMTPConnection('smtp.gmail.com', '587');
$email->setSMTPAuth('youraccount@gmail.com', 'yourpassword');

// set e-mail headers
$email->setFrom('crimson.king@thunderclap.mw', 'The Crimson King');
$email->addRecipient('roland.deschain@gilead.mw');
$email->addCC('eddie.dean@brooklyn.ny');

// set variables
$variables = array();
$variables['replaceMe'] = 'e-mail';

// set e-mail contents
$email->setSubject("It's all 19");
$email->setHTMLContent('/path/to/templates/email.tpl', $variables);

// send the e-mail
$email->send();

// check what response we got from the SMTP server
echo $email->getOutput();

The purpose of setSMTPSecurity() is to set the security layer used for your SMTP connection. Depending on the server this function is optional, but not for Gmail.

After executing the example you will receive output along these lines, meaning all went well:

220 mx.google.com ESMTP 7sm8987948eyg.17
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
334 VXKlbn4wbBR6
334 UGFqv3dvcmS7
235 2.7.0 Accepted
250 2.1.0 OK 7sm8987948eyg.17
250 2.1.5 OK 7sm8987948eyg.17
250 2.1.5 OK 7sm8987948eyg.17
354 Go ahead 7sm8987948eyg.17
250 2.0.0 OK 1263983463 7sm8987948eyg.17

getOutput() will only work when you're connecting with SMTP. (There is no specific output for the mail() function.)

Conclusion

You can now send e-mails from a template file with variables assigned, with and without an SMTP connection.

4 comments

Lokendra Khandelwal wrote 2 years ago

Is this works when calling external style sheets are called in the html, when we send a e-mail to gmail, outlook etc.
These clients removes the external style sheets url from the html.

João Magalhães wrote 1 year ago

Hi, I'm trying to send e-mails using SMTP but I keep getting this error:
"End data with . "

I looked it up and tried to insert "\r\n.\r\n" in the end of the e-mail body but still nothing.

Can you help me?

Lorenz Quintens wrote 1 year ago

I was able to send an e-mail using this SpoonEmail object, but it only works correctly when using setHTMLContent. If I use setPlainContent (the method I prefer to use in this case), the e-mail is sent, but the content is blanked.
I tried to send it both in an test environment (WAMP + sendmail.exe, on windows 7 HP) and on the actual apache server, but with the same result.
The standard PHP mail() function does work correctly for me however.
I'm using the full Spoon library, and downloaded it approx. 2 months ago.

Lorenz Quintens wrote 1 year ago

addendum to previous post:
After going through the logs created by sendmail.exe, I noticed the possible cause of this:
after the 'content-type' header of the 'multipart/alternative' message (between the defined boundaries) is set to 'text/plain' and followed by the desired content as passed by my script, it is immediately followed by a blank messages, of which the 'content-type' is set to text/html. It also uses the same boundary hash.
Shouldn't that part be omitted if there is no html content? Or am I looking at this in the wrong way?