Magento Mail 传输是从这两个函数中设置和执行的
Mage_Core_Model_Email_Template -> send()
Mage_Newsletter_Model_Template -> send()
这是我创建的工作模块代码,用于通过我们的电子邮件服务提供商引导交易电子邮件。它覆盖Mage_Core_Model_Email_Template -> send()
请注意,您需要为您的目的对额外的配置项进行硬编码,因为此代码示例缺少将字段添加到系统配置的设置,但它应该让您了解如何更改 send() 函数使用需要身份验证并可以提供 SSL/TLS 安全传输的 SMTP 服务器。
public function send($email, $name = null, array $variables = array())
{
if (!$this->isValidForSend()) {
Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
return false;
}
/* Set up mail transport to Email Hosting Provider SMTP Server via SSL/TLS */
$config = array(
'ssl' => Mage::getStoreConfig('system/smtp/ssl'), // option of none, ssl or tls
'port' => Mage::getStoreConfig('system/smtp/port'), // TLS 587 - SSL 465 - default 25
'auth' => Mage::getStoreConfig('system/smtp/auth'), // Auth type none, login, plain, CRAM-MD5
'username' => Mage::getStoreConfig('system/smtp/username'),
'password' => Mage::getStoreConfig('system/smtp/password')
);
/* Set up transport package to host */
$transport = new Zend_Mail_Transport_Smtp(Mage::getStoreConfig('system/smtp/host'), $config);
/* End transport setup */
$emails = array_values((array)$email);
$names = is_array($name) ? $name : (array)$name;
$names = array_values($names);
foreach ($emails as $key => $email) {
if (!isset($names[$key])) {
$names[$key] = substr($email, 0, strpos($email, '@'));
}
}
$variables['email'] = reset($emails);
$variables['name'] = reset($names);
// ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
// ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));
$mail = $this->getMail();
$setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
switch ($setReturnPath) {
case 1:
$returnPathEmail = $this->getSenderEmail();
break;
case 2:
$returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
break;
default:
$returnPathEmail = null;
break;
}
if ($returnPathEmail !== null) {
$mailTransport = new Zend_Mail_Transport_Sendmail("-f".$returnPathEmail);
Zend_Mail::setDefaultTransport($mailTransport);
}
foreach ($emails as $key => $email) {
$mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
}
$this->setUseAbsoluteLinks(true);
$text = $this->getProcessedTemplate($variables, true);
if($this->isPlain()) {
$mail->setBodyText($text);
} else {
$mail->setBodyHTML($text);
}
$mail->setSubject('=?utf-8?B?' . base64_encode($this->getProcessedTemplateSubject($variables)) . '?=');
$mail->setFrom($this->getSenderEmail(), $this->getSenderName());
try {
/* Send Transport, empty and log success */
$mail->send($transport); //transport object
$this->_mail = null;
Mage::log('Mailed to: ' . $this->getSenderEmail() . ' ' . $this->getSenderName() . ' ' .$this->getProcessedTemplateSubject($variables), null, 'email.log');
/* End */
}
catch (Exception $e) {
/* Or empty and log failure */
$this->_mail = null;
Mage::log('Failure: ' . $e, null, 'email.log');
Mage::logException($e);
return false;
/* End */
}
return true;
}