Image File Attachment

Zend_Mail allows you to attach most types of files to e-mail by invoking the createAttachment() method, which accepts four parameters:

createAttachment($fileContent, $mimeType, $disposition, $encoding)

The initial required parameter is the content of the file that is read using PHP's built-in fread() function. The remaining three parameters are optional. The second parameter accepts a string representing the file MIME type. Typical string values are image/gif or application/msword. By specifying the third parameter, the disposition type, you can inform the e-mail client if the attached file content should appear in the e-mail body when the e-mail is opened (Zend_Mime:DISPOSITION_INLINE) or as an icon/link that allows the user to download the file (Zend_Mime::DISPOSITION_ATTACHMENT). The final parameter is the encoding type. Acceptable values are shown in Table 6-3; by default the encoding type is set to base 64.

■ Note A good online resource containing a list of MIME types is available at http://www.webmaster-

Table 6-3. Encoding Values Zend Constant



7 bit encoding

8 bit encoding Quoted Printable Base 64 encoding

In the following example, you'll create an e-mail and attach an image using three parameters, as shown in Listing 6-8. This example will read a binary file such as an image and attach it to the e-mail before sending it to the recipient.

Open the EmailController once more and add the new action sendEmailWithAttachmentAction().

Listing 6-8. Sending an E-mail with an Attachment

* Send out email with attachment

public function sendEmailWithAttachmentAction() {

//Create SMTP connection $configInfo = array('auth' => 'login',

'username' => '<YOUR ACCOUNT USERNAME>', 'password' => '<YOUR SMTP ACCOUNT PASSWORD>', 'port' => '<SMTP PORT NUMBER>');

$smtpHost = new Zend_Mail_Transport_Smtp('<SMTP HOST>', $configInfo);

//Create Zend_Mail object. $MailObj = new Zend_Mail();

$message = "<h1>Welcome to the example</h1>". "<br><p>An example email.</p>";

//Read image data.

//Check if the file exists and is readable if(!$fileHandler = fopen($fileLocation, 'rb')){

throw new Exception("The file could not be found or is not readable.");

$fileContent = fread($fileHandler, filesize($fileLocation));



//Initialize parameters.

$fromEmail = "<FROM_EMAIL_ADDRESS"; $fromFullName = "<FROM_FULL_NAME>"; $to = "<YOUR_EMAIL_HERE>"; $subject = "This is an example";

$MailObj->setBodyHtml($message); $MailObj->setFrom($fromEmail, $fromFullName); $MailObj->addTo($to); $MailObj->setSubject($subject);

$MailObj->createAttachment($fileContent, '<MIME TYPE OF FILE>', Zend_Mime::DISPOSITION_ATTACHMENT);

//Send Email using transport protocol. try{

$MailObj->send($smtpHost); echo "Email sent successfully";

}catch(Zend_Mail_Exception $e){

//Your Error message here. echo $e->getMessage();

//Suppress the view.


The example shown in Listing 6-8 begins like all the other examples in this chapter. You create an instance of Zend_Mail_Transport_Smtp, create an instance of Zend_Mail, and initialize and set all recipient information. But then you use PHP's fread() function to read a file. You should specifically use fread() because it reads binary data. This binary data is the content that will be used in the createAttachment() method. For portability issues, set both r and b values. Once you read the content from the binary file, save the content into the $fileContent variable, flush the output buffer, and close the file handler because you no longer need the file functionality. After setting the recipient information, make a call to createAttachment(). Using the content read from the file, pass in the $fileContent variable as the initial value and specify the second parameter with the MIME type of the file. The third parameter is set to Zend_Mime::DISPOSITION_ATTACHMENT, allowing the browser to know how the attachment should be shown. If you did not specify a MIME type by default, Zend_Mail assumes that the attachment is a binary with a MIME type of application/octet-stream.

Try sending yourself an e-mail with a photo of your favorite band or your dog by updating the placeholders in the code shown in Listing 6-8 and loading the URL http://localhost/email/send-email-with-attachment.

Was this article helpful?

0 0

Post a comment