Email / PHP / Placeholders / SMTP / Template · May 30, 2016 0

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//index.php
 
require_once 'Tecnotch/bootstrap.php';
use \Tecnotch;
 
 
$mailer = \Tecnotch\Factory::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