Index: library/Zend/Application/Resource/Mail.php =================================================================== --- library/Zend/Application/Resource/Mail.php (revision 0) +++ library/Zend/Application/Resource/Mail.php (revision 0) @@ -0,0 +1,108 @@ +getMail(); + } + + /** + * + * @return Zend_Mail_Transport_Abstract|null + */ + public function getMail() + { + if (null != $this->_transport) { + $options = $this->getOptions(); + + if(isset($options['transport']['type']) && + !is_numeric($options['transport']['type'])) + { + $this->_transport = $this->_createTransport($options['transport']); + if(!(isset($options['transport']['register']) && + !is_numeric($options['transport']['register']) && + (bool) $options['transport']['register'] == true)) + { + Zend_Mail::setDefaultTransport($this->_transport); + } + } + + // Move these calls to init() ? + $this->_setDefaults('email'); + $this->_setDefaults('replyTo'); + } + + return $this->_transport; + } + + protected function _setDefaults($type, $options) { + $key = 'default' . $type; + if(isset($options[$key]['email']) && + !is_numeric($options[$key]['email'])) + { + $method = array('Zend_Mail', 'setDefault' . ucfirst($type)); + if(isset($options[$key]['name']) && + !is_numeric($options[$key]['name'])) + { + call_user_func($method, $options[$key]['email'], + $options[$key]['name']) + } else { + call_user_func($method, $options[$key]['email']); + } + } + } + + protected function _setupTransport($options) + { + $transportName = ucfirst(strtolower($options['type']); + unset($options['type']); + + if(!Zend_Loader_Autoloader::autoload($transportName)) { + $transportName = 'Zend_Mail_Transport_' . $transportName; + + if(!Zend_Loader_Autoloader::autoload($transportName)) { + throw new Zend_Application_Resource_Exception( + "Specified Mail Transport '{$transportName}'" + . 'could not be found' + ); + } + } + + switch($transportName) { + case 'Zend_Mail_Transport_Smtp': + if(!isset($options['host'])) { + throw new Zend_Application_Resource_Exception( + 'A host is necessary for smtp transport,' + .' but none was given'); + } + + $transport = new $transportName($options['host'], $options); + break; + case 'Zend_Mail_Transport_Sendmail': + default: + $transport = new $transportName($options); + break; + } + + return $transport; + } +} Index: library/Zend/Mail.php =================================================================== --- library/Zend/Mail.php (revision 19677) +++ library/Zend/Mail.php (working copy) @@ -62,6 +62,18 @@ protected static $_defaultTransport = null; /** + * @var array + * @static + */ + protected static $_defaultFrom; + + /** + * @var array + * @static + */ + protected static $_defaultReplyTo; + + /** * Mail character set * @var string */ @@ -713,6 +725,106 @@ } /** + * Sets Default From-email and name of the message + * + * @param string $email + * @param string Optional $name + * @return void + */ + public static function setDefaultFrom($email, $name = null) + { + self::_defaultFrom = array('email' => $email, 'name' => $name); + } + + /** + * Returns the default sender of the mail + * + * @return null|array Null if none was set. + */ + public static function getDefaultFrom() + { + return self::_defaultFrom; + } + + /** + * Clears the default sender from the mail + * + * @return void + */ + public static function clearDefaultFrom() + { + self::_defaultFrom = null; + } + + /** + * Sets From-name and -email based on the defaults + * + * @return Zend_Mail Provides fluent interface + */ + public function setFromToDefaultFrom() { + $from = self::getDefaultFrom(); + if($from === null) { + require_once 'Zend/Mail/Exception.php'; + throw new Zend_Mail_Exception( + 'No default From Address set to use'); + } + + $this->setFrom($from['email'], $from['name']); + + return $this; + } + + /** + * Sets Default ReplyTo-address and -name of the message + * + * @param string $email + * @param string Optional $name + * @return void + */ + public static function setDefaultReplyTo($email, $name = null) + { + self::_defaultReplyTo = array('email' => $email, 'name' => $name); + } + + /** + * Returns the default Reply-To Address and Name of the mail + * + * @return null|array Null if none was set. + */ + public static function getDefaultReplyTo() + { + return self::_defaultFrom; + } + + /** + * Clears the default ReplyTo-address and -name from the mail + * + * @return void + */ + public static function clearDefaultReplyTo() + { + self::_defaultFrom = null; + } + + /** + * Sets ReplyTo-name and -email based on the defaults + * + * @return Zend_Mail Provides fluent interface + */ + public function setReplyToFromDefault() { + $replyTo = self::getDefaultReplyTo(); + if($replyTo === null) { + require_once 'Zend/Mail/Exception.php'; + throw new Zend_Mail_Exception( + 'No default Reply-To Address set to use'); + } + + $this->setReplyTo($replyTo['email'], $replyTo['name']); + + return $this; + } + + /** * Sets the Return-Path header of the message * * @param string $email @@ -1035,6 +1147,14 @@ $this->setDate(); } + if(null === $this->_from && null !== self::getDefaultFrom()) { + $this->setFromFromDefault(); + } + + if(null === $this->_replyTo && null !== self::getDefaultReplyTo()) { + $this->setReplyToFromDefault(); + } + $transport->send($this); return $this;