Category Archives: XpertMailer

Send e-mail with inline image attachment

Since I start coding in PHP that one of the key feature were send and simple e-mail by a form. This can be done with many API’s that exist to work with the e-mail system. With the evolution of the Internet, sending e-mails with a design and not only with text has become to be normal and widely used. I’ve used many solutions like php mailer, xpert mailer, version 2 and 4, code igniter e-mail class, but still using the version 2 of xpert mailer, also known as XPM2.This older version, already with lack of support by its developers is simpler and easier to include in one project and do everything I need to send e-mail, from normal text based e-mail to rich content e-mail in html and attachments.

There are many ways for doing that by using html e-mails. This task isn’t as simple as it looks like, because html e-mail has some limitations and some rules that we can’t break if we want to see our e-mail well formed. One of the principal limitations is use a table based layout instead of a div one.

While using rich e-mails we face some difficulties and issues. The way we put images on the html e-mail is important. We’ve to choices to doing that.

  1. The first one is use full path to an image that is store in a web server accessible everywhere. This has some problems because we may not have space on a web server to put the files. Another problem is the limitation of the domain. If we want to change our domain or use our script in another one, we’ve to change all image and file path.
  2. Another way to put files on the e-mail, especially images, is putting them inline of the e-mail like an embed attachment. This solves the problems described but is much hard to use and to understand. Some API’s are simplifying the programmers work by offering a simpler to use Mime content inside an e-mail. The XPM2 have this feature and turns it simply to use when we do it right.

The code shown below is simple and sends an example e-mail with an inline image.

// XPM2 with attach inline example
require(“xpm2-0.1/smtp.php”);

// Create XPM instance
$mail = new smtp();

// Select delivery mode
$mail->Delivery(‘local’);

// Set timeout
$mail->TimeOut(10);

// Set e-mail priority
$mail->Priority(‘Normal’);

// Set from e-mail
$mail->From(“test@ws4.org”,”Test”);

// Set to e-mail
$mail->addTo(“protech@ws4.org”,”ProTech”);

// Set body content has HTML dining is encoding
$mail->Html(‘<html><head><title>Test</title></head><body>Test<br /><img src=”image.jpg” alt=”Inline image” /></body></html>’,’ISO-8859-1′, ‘base64’);

// Attach file to the e-mail
$mail->AttachFile(‘image.jpg’, false, ‘autodetect’, ‘inline’ );

// Send e-mail and set the subject and it’s encoding
$sent = $mail->Send(‘Hello World!’,’ISO-8859-1′, ‘base64’);

// Check the status and see the result if error
echo $sent ? ‘Success’ : $mail->result;

The method attach file is used to add files to the e-mail, either as a normal attachment and an inline one. If we see the parameters we find a disposition that allows us to define the type of attachment. Using the second parameter we can set a name or use the full file name. Is this example, I attach an image as inline content and use it in html. It’s so easy and clean.

This API also allow you to choose the better way to send your e-mails using local, relay and client letting you use your servers e-mail layer or connecting to an SMTP server. Other feature is the possibility to connect to a throw POP3.

See the documentation about XPM2

I haven’t use the XPM4 because I had some header encoding issues and haven’t tried to fix the problem yet. Besides, I still use XPM2 because allows sending e-mail the way I like and is too small and easy to deploy and integrate with some frameworks, like Code Igniter.