What is LSMailer?
LSMailer is a modern and efficient PHP library to send emails using SMTP driver. It can be use in simple projects, where there is a need for a simple way to send emails (as in contact forms, polls, etc).
It has very intuitive methods, as to(), cc(), bcc(), send(). More than that, these methods are chainable, so you can always play in an elegant way with the parameters.
It works with simple, TLS or SSL SMTP accounts. Debugging option also available (just use the debug flag).
At least PHP 5.4.
Optionally, phpunit to run the test suite.
Where to get it from?The first two methods are preffered, the archive might get outdated from time to time.
If you're using composer, then add this to your composer.json and run composer update.
Otherwise, in your code include autoload.php file from this package:
Before you start
You may need to update an internal value for sending host (identified by its fully qualified DNS host name - for example mail.example.com). Open LSMailer/Drivers/LSSmtp.php in a text editor of your choice.
$sending_host = 'localhost'; // replace it by $sending_host = 'mail.example.com';
Example of static config
Configuration is pretty straightforward, you need to supply the server information and credentials in a stdClass structure. For example, before sending an email in your code you must have:
<?php .... $conf = new \stdClass; $conf->host = 'mail.example.com'; $conf->port = 465; $conf->secure = 'ssl'; $conf->user = 'user'; $conf->pass = 'secret'; $conf->charset = 'ISO-8859-2'; $conf->timezone = 'UTC+2';
|Parameter name||Explanation||Default Value|
|host||hostname where the mailserver is located (e.g. localhost, mail.example.com). If using ssl, don't prepend with ssl:// but use the secure flag (see below)||-|
|port||port on which the smtp service is running||25|
|secure||ssl or tls -check your mailserver to see what's the preferred way.||-|
|user||username (only if smtp requires authorization - most of the cases).||-|
|pass||password (only if smtp requires authorization - most of the cases).||-|
|charset||character encoding for messages.||UTF-8|
From these parameters, only host, port are required, added to user and pass if authorization used.
Available to be dynamically changes is the format() of the messages.
It can be html or text (default:text)
Also, for testing purposes, you can use debug functionality:
Debug messages will be outputted to console. Defaults to 0.
After preparing the static configuration, you can use it like this:
<?php require 'vendor/autoload.php'; use LSMailer\LSMail; $conf = new \stdClass; $conf->host = 'smtp.gmail.com'; $conf->port = 465; $conf->secure = 'ssl'; $conf->user = 'mytestuser'; $conf->pass = 's93nfg2mss12'; $conf->timezone = 'Europe/Dublin'; $mailer = LSMail::factory('smtp', $conf); $mailer->format('html'); // dynamic configuration $mailer ->to(array('email@example.com', 'firstname.lastname@example.org; John Doe')) ->to('email@example.com; Another Joe') ->from('firstname.lastname@example.org'); $mailer ->cc('email@example.com; Jane') ->reply_to('firstname.lastname@example.org'); $mailer->subject("LSMailer - nice piece of software"); $mailer->format('html')->debug(1); $mailer->body("<strong>Text and roses.</strong>"); $mailer ->attach('/path/file1') ->attach(array('/path/file2', '/path/file3')); $mailer->send(); ...
All methods are chainable, and with names very intuitive (they correspond to the email fields, as To:, Cc:, ReplyTo:).