designing4u.de Yet Another Coding Blog

5Sep/090

SMPT authentication with Zend_Mail

It's been a long time since I wrote my last post. Well, I was busy working and getting familiar with Zend Framework. Since the Zend Framework documentation is not really helpful when it comes to send emails using SMTP, I decided I show you how I accomplished that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * @author Wojciech Gancarczyk
 */
class MyApp_Mail_Example
{
    /**
     * @var Zend_Mail_Protocol_Smtp_Auth_Login
     */
    protected $_connection;
 
    /**
     * @var Zend_Mail_Transport_Smtp
     */
    protected $_transport;
 
    /**
     * @var string
     */
    protected $_host;
 
    public function __construct()
    {
        $this->_host = 'foo.bar.baz';
        $port = 25;
        $params = array(
            'username' => 'username',
            'password' => 'password'
        );
        $this->_connection =
            new Zend_Mail_Protocol_Smtp_Auth_Login($this->_host, $port, $params);
        $this->_connection->connect();
        $this->_connection->helo($this->_host);
        $this->_transport = new Zend_Mail_Transport_Smtp();
        $this->_transport->setConnection($this->_connection);
    }
 
    /**
     * Get the connection.
     *
     * @return Zend_Mail_Protocol_Smtp_Auth_Login
     */
    public function getConnection()
    {
        return $this->_connection;
    }
 
    /**
     * Get the transport.
     *
     * @return Zend_Mail_Transport_Smtp
     */
    public function getTransport()
    {
        return $this->_transport;
    }
 
    /**
     * Get the host.
     *
     * @return string
     */
    public function getHost()
    {
        return $this->_host;
    }
 
    /**
     * Send the emial
     */
    public function send()
    {
        $mail = new Zend_Mail();
        $mail->addTo('foo@bar.baz, 'Foo Bar');
        $mail->setFrom('foo@bar.baz', 'Foo Bar');
        $mail->setSubject(
            'Demonstration - Sending Multiple Mails per SMTP Connection'
        );
        $mail->setBodyText('...Your message here...');
        $mail->send($this->getTransport());
    }
}

That's it. Of course, you can break it into smaller units of code, resolve the dependencies etc. I just wanted to demonstrate the basics of operation. Class constructor demonstrates mostly, what you need to do to get it work. Basically you need to initialize Zend_Mail_Protocol_Smtp_Auth_Login by passing the host name, port and credentials. After that you connect to your mail server and initiate helo/ehlo sequence which marks the session as valid. After that you need to initialize Zend_Mail_Transport_Smtp and set the connection. The parameter you pass to setConnection method has to be an instance of Zend_Mail_Protocol_Abstract. After performing this initialization you are ready to send the emails. I put an example method send() to demonstrate how to do that. Don't forget to change the credentials, host name and all the emails in the send method. I'm waiting for your feedback, how to improve that.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Pages

Categories

Blogroll

Archive

Meta