Wednesday, 31 October 2012

Zend Framework 2 : Send Html Mail with Simple Template

Here is my experiment with some help from my friend, how to use zend framework 2 mail component to sent mail as html. Even though we don't have any mail server or we just don't want to think of how to set mail server but with this component we could send mail easily. In this section, we will utilize a reliable smtp server like Gmail. Then we just have to have an account on the web mail to use the service for send mail.

Before we move on to the code, make sure that openssl have activated on our php.ini configuration. By doing uncomment extension=php_openssl.dll.

Let's assume that we have a module called Email with structure as below


1. Make a template for mail

In view/email directory, there's a tpl directory contains template.phtml. template.phtml file is used for our template content mail that will be sent. Actually, you can make template mail wherever you want.

template.phtml

Greetings

ini template buat email !!
2. Call Namespace Zend Component
// for email library
use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
3. Make action on your Controller to send mail
// setup SMTP options
$options = new Mail\Transport\SmtpOptions(array(
            'name' => 'localhost',
            'host' => 'smtp.gmail.com',
            'port'=> 587,
            'connection_class' => 'login',
            'connection_config' => array(
                'username' => 'put your gmail username ',
                'password' => 'put your gmail password',
                'ssl'=> 'tls',
            ),
));
                 
$this->renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = $this->renderer->render('email/tpl/template', null);

// make a header as html
$html = new MimePart($content);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html,));

// instance mail 
$mail = new Mail\Message();
$mail->setBody($body); // will generate our code html from template.phtml
$mail->setFrom('sender email address','Sender Name');
$mail->setTo('some email addressed');
$mail->setSubject('Your Subject');

$transport = new Mail\Transport\Smtp($options);
$transport->send($mail);

Comments (14)

Loading... Logging you in...
  • Logged in as
Matias Iglesias's avatar

Matias Iglesias · 612 weeks ago

Excelent article. If you want to pass some values to the view, just modify this line:

$content = $this->renderer->render('email/tpl/template', array(foo=>'foo'));
Victor Costa's avatar

Victor Costa · 595 weeks ago

a year after you saved my life!
it doesnt work when you run from console $this->getServiceLocator()->get('ViewRenderer');
@carlos You must add this to your controller:
use ZendViewRendererRendererInterface as ViewRenderer;
You can save yourself a lot of hassle and dependency injection trouble by using SoflomoMail. There are various e-mail modules out there for zf2, but all suffer to be incomplete or not being extendable. Therefore I created SoflomoMail to help everyone with sending emails in zf2.

Read more in a blog post: https://juriansluiman.nl/article/149/soflomo-mail...
Or just check out the Github page: https://github.com/Soflomo/Mail
good article.
But I can't login to my gmail, the message is : "5.7.14 Please log in via your web browser and then try again."
1 reply · active 536 weeks ago
Setting sender from address using - $mail->setFrom('sender email address','Sender Name'); is not working, in email it is showing the gmail user name as the sender email.
1 reply · active 533 weeks ago
Hi, have you found a solution for this?
I'd been doing something just slightly wrong -- which as we know all too well, is all it takes -- with my smtp settings, and losing my mind for the past couple hours. Now I'm good.

THANK YOU!
login
Hello Chalit,

Thanks for your POST.Very Helpfull!
Can you tell me how pass variables to view file.

Thanks in advance!
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
MySQL Online Certification | MySQL Training Course | Learn MySQL Course
Well done! Pleasant post! This truly helps me to discover the solutions for my inquiry. Trusting, that you will keep posting articles having heaps of valuable data. You're the best!
Best MEAN Stack Online Training |MEAN Stack Online Certification |MEAN Stack Certification Training

Post a new comment

Comments by

Copyright © 2012 Clighter | Powered by Blogger