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.
Nice one thanks.
ReplyDeletevery nice explaination ,thanks
ReplyDeletegreat 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?
ReplyDeletePlz Can Any Body Give me Complete Example How to Send Email With Image Attachment
ReplyDeletePlz it a request
aftabahmed999@yahoo.com
i shall b thankful for all u
very nice tutorial
ReplyDeletethanks
Give me Complete Example How to Send Email With Image
ReplyDeletechetanj968@gmail.com
how to pass arguement in phtml file after rending to it
ReplyDeleteWe can Pass Arguement in phtml file as follows
ReplyDelete$myView->title="Your argument value";
Hi
ReplyDeletei got the error
An error occurred
Application error
plz help me how to solve this problem
hi
ReplyDeletei got this following Error..
please help me..
Application error
Exception information:
Message: No entry is registered for key 'sessionNamespaceFB'
Stack trace:
i am getting this error
ReplyDeleteNotice: 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..
Great Tutorial ...
ReplyDeleteThanks.
Thanks for this tutorial ...
ReplyDeleteAwesome tutorial ....very easy to understand to new candidate.....
ReplyDeletehow i am find the path of the upload image
ReplyDelete