Sending emails based on templates and placeholders with attachments using php and smtp


Being a web developer I often need to send emails based on user requirements, some time these are simple emails but some times users want to take control of email contents and attachments.

To make it easier for me and other developers I have created a mail library which can be easily switched to php native mail() function or smtp backend.

For smtp it is using swiftmailer and phpmailer, you can configure any of these as your smtp backend, by default swiftmailer is enabled as default smtp sender.

How to use library

Please download the library from gitub at following location
https://github.com/tofeeq/tecmail

you can git clone or download to your project directory

create mail sender script

//index.php

require_once 'Tecnotch/bootstrap.php';
use Tecnotch;


$mailer = TecnotchFactory::mailer('simple');
//keep them in order as they are below
$placeholders = array(
    "[user_name]" => "John Doe"
);
$mailer
    ->setPlaceholders($placeholders)
    ->setTemplatePath(__DIR__ . '/html/email/en')
    ->setTemplate("sample.html")
    ->setTo(array("email1@example.com" => "Tofeeq Rehman", "email2@example.com" => "Tof33q"))
    ->setCc(array("email3@example.com" => "Tofeeq Testing", "email4@example.com" => "Dev Tofeeq"))
    ->setFrom(array("sender@example.com" => "Tecnotch"))
    ->setReplyTo(array("sender@example.com" => "Tecnotch-Suport"))
    ->addAttachment(__DIR__ . '/test1.pdf', 'Test PDF 1', 'pdf')
    ->addAttachment(__DIR__ . '/test2.pdf', 'Test PDF 2', 'pdf')
    ->addAttachment(__DIR__ . '/test3.pdf', 'Test PDF 3', 'pdf')
    ->send();

You can read further examples and documentation on Github page

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s