<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; Mail</title>
	<atom:link href="http://www.designing4u.de/tag/mail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>SMPT authentication with Zend_Mail</title>
		<link>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=smpt-authentication-with-zend_mail</link>
		<comments>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 16:30:02 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[SMTP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend_Mail]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=33</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.<br />
<span id="more-33"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @author Wojciech Gancarczyk
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> MyApp_Mail_Example
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Mail_Protocol_Smtp_Auth_Login
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_connection</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Mail_Transport_Smtp
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_transport</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_host</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo.bar.baz'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'password'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection <span style="color: #339933;">=</span>
            <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail_Protocol_Smtp_Auth_Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">-&gt;</span><span style="color: #004000;">helo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_transport <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail_Transport_Smtp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_transport<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setConnection</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the connection.
     *
     * @return Zend_Mail_Protocol_Smtp_Auth_Login
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the transport.
     *
     * @return Zend_Mail_Transport_Smtp
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTransport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_transport<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the host.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getHost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Send the emial
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo@bar.baz, '</span>Foo Bar<span style="color: #0000ff;">');
        $mail-&gt;setFrom('</span>foo<span style="color: #339933;">@</span>bar<span style="color: #339933;">.</span>baz<span style="color: #0000ff;">', '</span>Foo Bar<span style="color: #0000ff;">');
        $mail-&gt;setSubject(
            '</span>Demonstration <span style="color: #339933;">-</span> Sending Multiple Mails per SMTP Connection<span style="color: #0000ff;">'
        );
        $mail-&gt;setBodyText('</span><span style="color: #339933;">...</span>Your message here<span style="color: #339933;">...</span><span style="color: #0000ff;">');
        $mail-&gt;send($this-&gt;getTransport());
    }
}</span></pre></td></tr></table></div>

<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

