Thứ Năm, 24 tháng 4, 2014

Sending mails [Chat and E-mail]

Zend Framework offers the Zend\Mail librarto send and receive e-mails. In this section, we will cover the basics of Zend Framework's mailing capabilities, and will also implement a simple mailing script.


Zend\Mail supports both plain text and MIME complaint multipart e-mail messages. The framework by default supports Sendmail, SMTP, and File transports; new transports can be implemented usingZend\Mail\Transport\TransportInterface. 

Zend\Mail\Transport

The Mail transport is used to send the e-mail messagtrecipients; Zend\Mail supports the following transports:

        Sendmail using Zend\Mail\Transport\Sendmail
        SMTP using Zend\Mail\Transport\Smtp
        File Transport using Zend\Mail\Transport\File

The Mail transport implements the send() method; this method accepts an object of type Zend\Mail\Message as the parameter; this object (Zend\Mail\Messagecontains all the necessary information for an e-mail message; the message is sent using the transport.


Zend\Mail\Message

Zend\Mail\Message is used tcompose the mail message in Zend Framework; this object takes various parameters including the from address, to address, subject, and body. If the message is a MIME complaint multipartmessage, then the body of the messagcan be seto Zend\Mime\Message mail message object using the setBody() method, and the messagcan be sent. Some of the most frequently-used methods inZend\Mail\Message are listed as follows:

        setFrom()
        setHeaders
        setTo()
        addCc() and addBcc()
        setSubject()
        setBody()


Zend\Mime\Message and Zend\Mime\Part

For sending HTML or multi-part content, each message part is defined as a Zend\Mime\ Part object along with its type and associated to the Zend\Mime\Message object using
the setParts() method. The Zend\Mime\Message object is assigned to the Zend\Mail\ Message object using the setBody() method.
In this activity, we will be creating an e-mail form making use of Zend's mailing capabilities:

1.       Create a simple e-mail form with input fields for subject, messagcontent, and addressee.
2.       Set up a necontroller to display the form and write the necessary views.
3.       Modify the controller so that it references the Zend\Mail namespace.
use Zend\Mail;

4.       Create a necontroller method that does the actual e-mailing; this can be placed within our 
group chacontroller (CommunicationApp/module/Users/src/ Users/Controller/GroupChatController.php) using the following code:
protected function sendOfflineMessage($msgSubj,
$msgText, $fromUserId, $toUserId)
{
$userTable = $this->getServiceLocator()
->get('UserTable');

$fromUser = $userTable->getUser($fromUserId);
$toUser = $userTable->getUser($toUserId);

$mail = new Mail\Message();
$mail->setFrom($fromUser->email, $fromUser->name);
$mail->addTo($toUser->email, $toUser->name);
$mail->setSubject($msgSubj);
$mail->setBody($msgText);

$transport = new Mail\Transport\Sendmail();
$transport->send($mail);

return true;
}
5.       Preview the form in a web browser and test if the e-mail is being received; a message similar to the following one would be received by the recipient:


What just happened?
We have used the Zend\Mail object to send e-mails within the system using the Sendmail
mail transport; we have also learned about how to send HTML or multi-part mail messages.
Before moving on to the next section, trto implement the e-mailing form for sending out HTML e-mails.

Không có nhận xét nào:

Đăng nhận xét