📜 ⬆️ ⬇️

Zend_Mail send authentication via SMTP

I reworked some one site for the unfortunate creators and it took me to send mail via SMTP with authentication.

We look in the manual on the site of the website http://framework.zend.com/manual/ru/zend.mail.smtp-authentication.html
and see: "... at the moment SMTP authentication is not supported" :(

What to do?
')
Well aware that the documentation on the site is becoming outdated earlier, and the latest version of the framework was released quite recently, we are looking at the code.

And what do we see? Zend / Mail / Protocol / Smtp / Auth !!! HOORAY!

so the result is:

config.ini (I store configs in the ini file)

[mail]
host = mailhost.ru
port = 25
auth = login
username = smtpusername
password = smtppassword


in accordance with the recommendation of the manual
“To send an email via SMTP, you need to create and register a Zend_Mail_Transport_Smtp object before the send () method is called. All subsequent calls to Zend_Mail :: send () will use SMTP in the current script. ”

index.php or bootstrap.php

$oConfig = new Zend_Config_Ini('config.ini');

/**
* . SMTP
*/
$tr = new Zend_Mail_Transport_Smtp( $oConfig->mail->host, $oConfig->mail->toArray() );
Zend_Mail::setDefaultTransport($tr);


here you should pay attention to the second parameter Zend_Mail_Transport_Smtp in accordance with the code is an array
/**
* Config options for authentication
*
* @var array
*/
protected $_config;

Zend_Mail_Transport_Smtp cannot work with the configuration object,
that is why it wants to convert it toArray ()

and where you need to send just a helmet mail

$mail = new Zend_Mail( 'windows-1251' );
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();

Source: https://habr.com/ru/post/41127/


All Articles