Zend Framework

Sending Email with attachment in zend framewok

In your controller, write something like this.

$emial_body = “<table><tr><td>Body of the email</td></tr></table”;
$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setBodyHtml($email_body);
$mail->setFrom(‘support@example.com’, ‘Example’);
$mail->addTo(‘user@abc.com’, ‘Username’);
$mail->setSubject(‘Sending email using Zend Framework’);
$mail->send();
Although, this will send email, however it’s not a good approach to create email in your controller as your mail body may contain thousands of words.
The best approach is to create a template, that contain the contents that will act as body of the email. To achieve this, create a file called emailExample.html in your /application/views/scripts/templates/ directory. And write the contents you want to put, those contents will serve as body of the email you are going to send.

$myView = new Zend_View();

$myView->addScriptPath(ROOT_DIR . ‘/application/views/scripts/templates/’);

$html_body = $ myView ->render(emailExample.html’);

$mail = new Zend_Mail();

$mail->setType(Zend_Mime::MULTIPART_RELATED);

$mail->setBodyHtml($html_body);

$mail->setFrom(‘support@example.com’, ‘Example’);

$mail->addTo(‘user@abc.com’, ‘Username’);

$mail->setSubject(‘Sending email using Zend Framework’);

$mail->send();

You can also use file_get_contents() function and then use createAttachment() function.

$fileContents = file_get_contents(‘path/to/file’);

And then call a simple method called createAttachment (), as

$file = $mail->createAttachment($fileContents);

And set the name of the attachment as

$file->filename = “yourfile.doc”;

Sending Email in Zend Framework

In this article I am going to discuss how to send email with SMTP authentication. Please do tell me whether it helped you or not.

You can send email using zend famework as follows:

$emial_body = “<table><tr><td>Body of the email</td></tr></table”;

$mail = new Zend_Mail();
$mail->setBodyHtml($email_body);
$mail->setFrom('support@domain.com', 'Example');
$mail->addTo('user@abc.xyz', 'Username');
$mail->setSubject('Sending email using Zend Framework');
$mail->send();

If you require SMTP authentication while sending email in zend framework, use following code: This way of sending email will help the mail going in spam.

$emial_body = “<table><tr><td>Body of the email</td></tr></table”;
$config = array('auth' => 'login','username' => "info@domainname.com",
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.domainname.com', $config);
$mail = new Zend_Mail(); $mail->setBodyHtml($email_body); $mail->setFrom('support@domain.com', 'Example'); $mail->addTo('user@abc.xyz', 'Username'); $mail->setSubject('Sending email using Zend Framework'); $mail->send(
$transport);