cwhisperer
cwhisperer

Reputation: 1926

zend framework sending mail

I need to send mails through smtp. What is the best approach: declaring everything in the application.ini like here or writting a module for it?

Does anyone has a working example?

Regards Andrea

Upvotes: 2

Views: 2618

Answers (1)

Jerry Saravia
Jerry Saravia

Reputation: 3837

resources.mail.transport.type = smtp
resources.mail.transport.host = "server.smtp.com"
resources.mail.transport.auth = login
resources.mail.transport.username = "[email protected]"
resources.mail.transport.password = "ultraPass"
resources.mail.transport.ssl = ssl
resources.mail.transport.port = 465
resources.mail.transport.register = true ; True by default

resources.mail.defaultFrom.email = [email protected]
resources.mail.defaultFrom.name = "The bank"
resources.mail.defaultReplyTo.email = [email protected]
resources.mail.defaultReplyTo.name = "replier"

The above is what I have in my production section of the application.ini file.

Later, when I want to send mail I do the following

    $view = $this->getView();

    $mail = new Zend_Mail();

    $emailText = $view->render( 'theEmailFileSomewhere.phtml' );

    $mail->addTo( $emailAddress, $usersfirstname )
            ->setSubject( 'Hi ' . $usersfirstname  . '!' )
            ->setBodyText( $emailText, $this->_charset ) //charset is utf-8
            ->send();

    return true;

I the pro is that later, in the development section of the ini file, I can easily change this to a simple file mailer. So that emails are output as files on the local system rather than actually sending out email.

Another pro is that someone 'installing' the app can do it all from the ini file, if that's how you've designed it.

You could setup your resource to do this as well, however.

The con with this is that your credentials and all are in the .ini file when maybe you don't want them there.

What you could do is setup a resource that will pull the credentials from environment variables so that the creds never really have to be a part of the application.

If you setup your resource this way, then whoever installs the app has to know about setting those environment variables or they'll have a hard time figuring it out themselves, unless you add some verbose errors that is.

If you're curious, this is what I put in the development sections of the ini.

resources.mail.transport.type = file
resources.mail.transport.path = APPLICATION_PATH "/../data/testemails/"
resources.mail.transport.callback[] = "My_Service_Email"
resources.mail.transport.callback[] = "generateEmailFilename"
resources.mail.transport.register = true ; True by default

In this case, My_Service_Email is a class I made that sends email using view scripts. In development the generateEmailFilename will be called to generate the name of the email file. It just makes a filename with a timestamp so that emails don't override each other. This is enough for my testing purposes at least. Mostly I just want to see that the text from the view scripts is making its way to the email.

Upvotes: 9

Related Questions