English 中文(简体)
Email Management
  • 时间:2024-09-17

Zend Framework - Email Management


Previous Page Next Page  

The Zend Framework provides a separate component called as zend-mail to send email messages. The zend-mail component also provides an option to read and write email messages with attachments both in text and html format. Sending an email in Zend is much easier and simple to configure.

Let us go through the email concepts, basic settings, advanced settings such as SMTP transport, etc., in this chapter.

Install Mail Component

The mail component can be installed using the following Composer command.

composer require zendframework/zend-mail

Basic Email Configuration

A basic email consists of one or more recipients, a subject, a body and a sender. Zend provides ZendMailMessage class to create a new email message. To send an email using the zend-mail, you must specify at least one recipient as well as a message body.

The partial code to create a new mail message is as follows −

use ZendMail;
$mail = new MailMessage(); 
$mail->setSubject( Zend email sample ); 
$mail->setBody( This is content of the mail message ); 
$mail->setFrom( sender@example.com , "sender-name"); 
$mail->addTo( recipient@test.com , "recipient-name"); 

Zend provides ZendMailSendmail class to send the mail message. Sendmail uses the php native mail function, mail to send the mail message and we can configure the transport layer using php configuration file.

The partial coding using Sendmail is as follow −

$transport = new MailTransportSendmail(); 
$transport->send($mail);

The zend-mail provides many transport layer and each may require many additional parameters such as username, password, etc

Email Management Methods

Some of the notable email management methods are as follows −

    isVapd − Messages without a ‘From’ address is invapd.

isVapd() : bool

    setEncoding − Set the message encoding.

setEncoding(string $encoding) : void

    getEncoding − Get the message encoding.

getEncoding() : string

    setHeaders − Compose headers.

setHeaders(ZendMailHeaders $headers) : void

    getHeaders − Access headers collection.

getHeaders() : ZendMailHeaders

    setFrom − Set (overwrite) From addresses. It contains a key/value pairs where the key is the human readable name and the value is the email address.

setFrom( 
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressList, 
      string|null $name 
) : void 

    addFrom − Add a ‘From’ address.

addFrom( 
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressOrList, 
      string|null $name 
) : void 

    getFrom − Retrieve pst of ‘From’ senders.

getFrom() : AddressList 
setTo - Overwrite the address pst in the To recipients. 
setTo( 
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressList, 
      null|string $name 
) : void 

    setSubject − Set the message subject header value.

setSubject(string $subject) :void 

    setBody − Set the message body.

setBody(null|string|ZendMimeMessage|object $body) : void 

SMTP Transport Layer

The zend-mail provides options to send an email using the SMTP server through the ZendMailTransportSmtpclass. It is pke Sendmail except that it has a few additional options to configure the SMTP host, port, username, password, etc.

The partial code is as follows −

use ZendMailTransportSmtp as SmtpTransport; 
use ZendMailTransportSmtpOptions;  
$transport = new SmtpTransport(); 
$options = new SmtpOptions([ 
    name  =>  localhost , 
    host  => smtp.gmail.com , 
    port  => 465, 
]); 
$transport->setOptions($options); 

Here,

    name − Name of the SMTP host.

    host − Remote hostname or IP address.

    port − Port on which the remote host is pstening.

Mail Concept – Example

Let us follow the following points to write a simple php console apppcation to understand the mail concept.

    Create a folder “mailapp”.

    Install zend-mail using the composer tool.

    Create a php file Mail.php inside the “mailapp” folder.

    Create the message using the ZendMailMessage.

$message = new Message(); 
$message->addTo( user1@gmail.com ); 
$message->addFrom( user2@gmail.com ); 
$message->setSubject( Hello! ); 
$message->setBody("My first Zend-mail apppcation!"); 

    Create the SMTP transport layer and add the necessary configuration.

// Setup SMTP transport using LOGIN authentication 
$transport = new SmtpTransport(); 
$options = new SmtpOptions([ 
    name  =>  localhost , 
    host  =>  smtp.gmail.com , // or any SMTP server 
    port  => 465, // port on which the SMTP server is pstening 
    connection_class  =>  login , 
    connection_config  => [ 
      username  =>  <your username> ,  password  =>  <your password> , 
       ssl  =>  ssl ], 
]); 
$transport->setOptions($options); 

    Send the email using the send method.

$transport->send($message);

The complete psting, Mail.php is as follows −

<?php  
require __DIR__ .  /vendor/autoload.php ;  

use ZendMailMessage; 
use ZendMailTransportSmtp as SmtpTransport; 
use ZendMailTransportSmtpOptions;  
  
$message = new Message(); 
$message->addTo( user1@gmail.com ); 
$message->addFrom( user2@gmail.com ); 
$message->setSubject( Hello! ); 
$message->setBody("My first Zend-mail apppcation!");  
  
// Setup SMTP transport using LOGIN authentication 
$transport = new SmtpTransport(); 
$options = new SmtpOptions([ 
    name  =>  localhost , 
    host  =>  smtp.gmail.com , // or any SMTP server 
    port  => 465, // port on which the SMTP server is pstening 
    connection_class  =>  login , 
    connection_config  => [ 
       username  =>  <your username> ,  password  =>  <your password> , 
       ssl  =>  ssl ], 
]); 
$transport->setOptions($options); 
$transport->send($message);

Now, run the apppcation in the command prompt php Mail.php. This will send the mail as configured in the apppcation.

Advertisements