Sunday, July 5, 2009

Sending Email with attachment in Zend Framework

You may have used Php for sending emails in your application. I haven’t experienced it in plain php, so I don’t know whether its easy or difficult. I, however, know that Zend Framework provide very easy way for sending emails. You can even attach files with your email with single method call.
In this article I am going to discuss how to send email with attachments. Please do tell me whether it helped you or not.

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();


This, although, 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.phtml 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.

I am having the following code in my emailExample.phtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<table>
<tr>
<td>
all my contents here.
</td>
</tr>
</table>
</body>
</html>




Now in your controller, write
$myView = new Zend_View();
$myView->addScriptPath(ROOT_DIR . '/application/views/scripts/templates/');
$html_body = $ myView ->render(emailExample.phtml');

$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();


Here we are first creating an instance of Zend_View, call addScriptPath() to add script path- directory path where our template file reside and which serve as body of our email.
We then get the template by calling render() method on that view object, giving it the name of the template. The rest of the process is same, creating Zend_Mail object, setting body and so on.
Another question come to mind is how
to send an attachment?
This is pretty simple too.
First you will need to get the contents of the file, by calling a method file_get_contents(), like this.
$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";

That’s it. Cheers.

15 comments:

  1. very nice explaination ,thanks

    ReplyDelete
  2. great tutorial. I have a requirement that i need to send the html form with the user selected values as a file attachment in an email (the user has to sign the completed form and return the paper copy, but the details should already be in the DB). I'm wondering is possible to write the form object with the correct .phtml template, and then attach that file to an email?

    ReplyDelete
  3. Plz Can Any Body Give me Complete Example How to Send Email With Image Attachment
    Plz it a request
    aftabahmed999@yahoo.com
    i shall b thankful for all u

    ReplyDelete
  4. Give me Complete Example How to Send Email With Image
    chetanj968@gmail.com

    ReplyDelete
  5. how to pass arguement in phtml file after rending to it

    ReplyDelete
  6. We can Pass Arguement in phtml file as follows
    $myView->title="Your argument value";

    ReplyDelete
  7. Hi

    i got the error

    An error occurred
    Application error

    plz help me how to solve this problem

    ReplyDelete
  8. hi
    i got this following Error..
    please help me..

    Application error
    Exception information:

    Message: No entry is registered for key 'sessionNamespaceFB'
    Stack trace:

    ReplyDelete
  9. i am getting this error

    Notice: Use of undefined constant FACEBOOK_CALLBACK - assumed 'FACEBOOK_CALLBACK' in D:\internship project\virtualdomain\demoSociaload2\application\controllers\FacebookController.php on line 22
    please help me..

    ReplyDelete
  10. Thanks for this tutorial ...

    ReplyDelete
  11. Awesome tutorial ....very easy to understand to new candidate.....

    ReplyDelete
  12. how i am find the path of the upload image

    ReplyDelete