'from' => $emails[0],
'charset' => Horde_Nls::getCharset()));
$mail->addHeader('User-Agent', 'Agora ' . $registry->getVersion());
- $mail->send(Horde::getMailerConfig());
+ $mail->send($injector->getInstance('Mail'));
$notification->push($subject, 'horde.success');
header('Location: ' . $url);
$alt->setBasePart($alternative);
/* Send. */
- $result = $alt->send(Horde::getMailerConfig());
+ $result = $alt->send($injector->getInstance('Mail'));
if (is_a($result, 'PEAR_Error')) {
$notification->push(sprintf(_("There was an error sending your message: %s"), $result->getMessage()), 'horde.error');
} else {
//FIXME: This address should be configurable
$mail->addHeader('Sender',
'horde-problem@' . $conf['report_content']['maildomain']);
- return $mail->send(Horde::getMailerConfig());
+ return $mail->send($GLOBALS['injector']->getInstance('Mail'));
}
}
}
}
- return $mail->send(Horde::getMailerConfig());
+ return $mail->send($GLOBALS['injector']->getInstance('Mail'));
}
/**
return true;
}
- list($mail_driver, $mail_params) = Horde::getMailerConfig();
-
$mail = new Horde_Mime_Mail(array('subject' => $subject,
'body' => $body,
'from' => $this->_params['from_addr'],
continue;
}
$mail->addHeader('To', $to, Horde_Nls::getCharset(), true);
- $mail->send($mail_driver, $mail_params);
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
}
return true;
));
$mail->addHeader('Auto-Submitted', 'auto-generated');
$mail->addHeader('X-Horde-Alarm', $alarm['title'], Horde_Nls::getCharset());
- $sent = $mail->send(Horde::getMailerConfig());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
$alarm['internal']['mail']['sent'] = true;
$this->_internal($alarm['id'], $alarm['user'], $alarm['internal']);
'to' => $conf['signup']['email'],
'from' => $conf['signup']['email'],
'subject' => Horde_Nls::getCharset()));
- $mail->send(Horde::getMailerConfig());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
}
}
}
/**
- * Return the driver and parameters for the current mailer configuration.
- *
- * @return array Array with driver name and parameter hash.
- */
- static public function getMailerConfig()
- {
- $mail_driver = $GLOBALS['conf']['mailer']['type'];
- $mail_params = $GLOBALS['conf']['mailer']['params'];
- if ($mail_driver == 'smtp' && $mail_params['auth'] &&
- empty($mail_params['username'])) {
- $mail_params['username'] = Horde_Auth::getAuth();
- $mail_params['password'] = Horde_Auth::getCredential('password');
- }
-
- return array('type' => $mail_driver, 'params' => $mail_params);
- }
-
- /**
* Checks if all necessary parameters for a driver configuration
* are set and throws a fatal error with a detailed explanation
* how to fix this, if something is missing.
--- /dev/null
+<?php
+class Horde_Core_Binder_Mail implements Horde_Injector_Binder
+{
+ public function create(Horde_Injector $injector)
+ {
+ $driver = $GLOBALS['conf']['mailer']['type'];
+ $params = $GLOBALS['conf']['mailer']['params'];
+
+ if (($driver == 'smtp') &&
+ $params['auth'] &&
+ empty($params['username'])) {
+ $params['username'] = Horde_Auth::getAuth();
+ $params['password'] = Horde_Auth::getCredential('password');
+ }
+
+ $result = Mail::factory($driver, $params);
+ if ($result instanceof PEAR_Error) {
+ throw new Horde_Exception($result);
+ }
+
+ return $result;
+ }
+
+ public function equals(Horde_Injector_Binder $binder)
+ {
+ return false;
+ }
+}
'Horde_Template' => new Horde_Core_Binder_Template(),
'Horde_Token' => new Horde_Core_Binder_Token(),
'Horde_Vfs' => new Horde_Core_Binder_Vfs(),
+ 'Mail' => new Horde_Core_Binder_Mail(),
'Net_DNS_Resolver' => new Horde_Core_Binder_Dns()
);
<file name="Dns.php" role="php" />
<file name="Lock.php" role="php" />
<file name="Logger.php" role="php" />
+ <file name="Mail.php" role="php" />
<file name="Memcache.php" role="php" />
<file name="Notification.php" role="php" />
<file name="Perms.php" role="php" />
<install name="lib/Horde/Core/Binder/Dns.php" as="Horde/Core/Binder/Dns.php" />
<install name="lib/Horde/Core/Binder/Lock.php" as="Horde/Core/Binder/Lock.php" />
<install name="lib/Horde/Core/Binder/Logger.php" as="Horde/Core/Binder/Logger.php" />
+ <install name="lib/Horde/Core/Binder/Mail.php" as="Horde/Core/Binder/Mail.php" />
<install name="lib/Horde/Core/Binder/Memcache.php" as="Horde/Core/Binder/Memcache.php" />
<install name="lib/Horde/Core/Binder/Notification.php" as="Horde/Core/Binder/Notification.php" />
<install name="lib/Horde/Core/Binder/Perms.php" as="Horde/Core/Binder/Perms.php" />
/**
* Sends this message.
*
- * For the possible Mail drivers and parameters see the PEAR Mail
- * documentation.
- * @link http://pear.php.net/Mail
- *
- * @param array $config A hash with the Mail driver to use in 'type' and
- * any parameters necessary for the Mail driver in
- * 'params'.
+ * @param Mail $mailer A Mail object.
* @param boolean $resend If true, the message id and date are re-used;
* If false, they will be updated.
* @param boolean $flowed Send message in flowed text format.
*
* @throws Horde_Mime_Exception
*/
- public function send($config, $resend = false, $flowed = true)
+ public function send($mailer, $resend = false, $flowed = true)
{
/* Add mandatory headers if missing. */
$has_header = $this->_headers->getValue('Message-ID');
}
}
- /* Check mailer configuration. */
- if (!empty($config['type'])) {
- $this->_mailer_driver = $config['type'];
- }
- if (!empty($config['params'])) {
- $this->_mailer_params = $config['params'];
- }
-
/* Send message. */
return $basepart->send(implode(', ', $this->_recipients),
- $this->_headers, $this->_mailer_driver,
- $this->_mailer_params);
+ $this->_headers, $mailer);
}
/**
- * Return error string corresponding to a sendmail error code.
+ * Utility function to send a message using a PEAR Mail driver. Handles
+ * errors from this class.
*
- * @param integer $code The error code.
+ * @param Mail $mailer A Mail object.
+ * @param string $email The address list to send to.
+ * @param array $headers An array of message headers.
+ * @param string $msg The message text.
*
- * @return string The error string, or null if the code is unknown.
+ * @throws Horde_Mime_Exception
*/
- static public function sendmailError($code)
+ static public function sendPearMail($mailer, $email, $headers, $msg)
{
- switch ($code) {
+ $result = $mailer->send($email, $headers, $msg);
+
+ if (!$result instanceof PEAR_Error) {
+ return;
+ }
+
+ if (!($mailer instanceof Mail_sendmail)) {
+ throw new Horde_Mime_Exception($result);
+ }
+
+ switch ($result->getCode()) {
case 64: // EX_USAGE
- return 'sendmail: ' . _("command line usage error") . ' (64)';
+ $msg = 'command line usage error';
+ break;
case 65: // EX_DATAERR
- return 'sendmail: ' . _("data format error") . ' (65)';
+ $msg = 'data format error';
+ break;
case 66: // EX_NOINPUT
- return 'sendmail: ' . _("cannot open input") . ' (66)';
+ $msg = 'cannot open input';
+ break;
case 67: // EX_NOUSER
- return 'sendmail: ' . _("addressee unknown") . ' (67)';
+ $msg = 'addressee unknown';
+ break;
case 68: // EX_NOHOST
- return 'sendmail: ' . _("host name unknown") . ' (68)';
+ $msg = 'host name unknown';
+ break;
case 69: // EX_UNAVAILABLE
- return 'sendmail: ' . _("service unavailable") . ' (69)';
+ $msg = 'service unavailable';
+ break;
case 70: // EX_SOFTWARE
- return 'sendmail: ' . _("internal software error") . ' (70)';
+ $msg = 'internal software error';
+ break;
case 71: // EX_OSERR
- return 'sendmail: ' . _("system error") . ' (71)';
+ $msg = 'system error';
+ break;
case 72: // EX_OSFILE
- return 'sendmail: ' . _("critical system file missing") . ' (72)';
+ $msg = 'critical system file missing';
+ break;
case 73: // EX_CANTCREAT
- return 'sendmail: ' . _("cannot create output file") . ' (73)';
+ $msg = 'cannot create output file';
+ break;
case 74: // EX_IOERR
- return 'sendmail: ' . _("input/output error") . ' (74)';
+ $msg = 'input/output error';
+ break;
case 75: // EX_TEMPFAIL
- return 'sendmail: ' . _("temporary failure") . ' (75)';
+ $msg = 'temporary failure';
+ break;
case 76: // EX_PROTOCOL
- return 'sendmail: ' . _("remote error in protocol") . ' (76)';
+ $msg = 'remote error in protocol';
+ break;
case 77: // EX_NOPERM
- return 'sendmail: ' . _("permission denied") . ' (77)';
+ $msg = 'permission denied';
+ break;
+
+ case 77: // EX_NOPERM
+ $msg = 'permission denied';
+ break;
case 78: // EX_CONFIG
- return 'sendmail: ' . _("configuration error") . ' (78)';
+ $msg = 'configuration error';
+ break;
case 79: // EX_NOTFOUND
- return 'sendmail: ' . _("entry not found") . ' (79)';
+ $msg = 'entry not found';
+ break;
default:
- return null;
+ throw new Horde_Mime_Exception($result);
}
+
+ throw new Horde_Mime_Exception('sendmail: ' . $msg . ' (' . $result->getCode() . ')');
}
+
}
* @param string $email The address list to send to.
* @param Horde_Mime_Headers $headers The Horde_Mime_Headers object
* holding this message's headers.
- * @param string $driver The Mail:: driver to use.
- * @param array $params Any parameters necessary for the
- * Mail driver.
+ * @param Mail $mailer A Mail driver.
*
* @throws Horde_Mime_Exception
+ * @throws InvalidArgumentException
*/
- public function send($email, $headers, $driver, $params = array())
+ public function send($email, $headers, $mailer)
{
- $mailer = Mail::factory($driver, $params);
- if ($mailer instanceof PEAR_Error) {
- throw new Horde_Mime_Exception($mailer);
+ if (!($mailer instanceof Mail)) {
+ throw new InvalidArgumentException('Invalid Mail object passed to send().');
}
$old_basepart = $this->_basepart;
* BINARYMIME (RFC 3030) extensions? Requires PEAR's Mail package
* version 1.2+ and Net_SMTP version 1.3+. */
$encode = self::ENCODE_7BIT;
- if ($driver == 'smtp') {
+ if ($mailer instanceof Mail_smtp) {
$net_smtp = $mailer->getSMTPObject();
if (!($net_smtp instanceof PEAR_Error)) {
$smtp_ext = $net_smtp->getServiceExtensions();
}
}
- $result = $mailer->send(Horde_Mime::encodeAddress($email, $this->getCharset()), $headers->toArray(array('charset' => $this->getHeaderCharset())), $msg);
-
$this->_basepart = $old_basepart;
- if (($result instanceof PEAR_Error) && ($driver == 'sendmail')) {
- $error = Horde_Mime_Mail::sendmailError($result->getCode());
- if (is_null($error)) {
- $error = $result;
- }
- throw new Horde_Mime_Exception($error);
- }
-
- return $result;
+ Horde_Mime_Mail::sendPearMail($mailer, Horde_Mime::encodeAddress($email, $this->getCharset()), $headers->toArray(array('charset' => $this->getHeaderCharset())), $msg);
}
/**
'to' => 'recipient@example.com',
'from' => 'sender@example.com',
'charset' => 'iso-8859-15'));
-echo $mail->send(array('type' => 'dummy'));
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
$mail->addHeader('From', 'sender@example.com');
$mail->removeHeader('Cc');
-echo $mail->send(array('type' => 'dummy'));
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
'from' => 'sender@example.com',
'charset' => 'iso-8859-1'));
$mail->addHeader('Cc', 'Der schöne Peter <peter@example.com>', 'iso-8859-15');
-echo $mail->send(array('type' => 'dummy'));
+
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
file_get_contents(dirname(__FILE__) . '/fixtures/attachment.bin'),
null, 'attachment');
-echo $mail->send(array('type' => 'dummy'));
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
'to' => 'recipient@example.com',
'from' => 'sender@example.com'));
$mail->setHTMLBody("<h1>Header Title</h1>\n<p>This is<br />the html text body.</p>");
-echo $mail->send(array('type' => 'dummy'));
+
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
$mail->addAttachment(dirname(__FILE__) . '/fixtures/attachment.bin');
$mail->addAttachment(dirname(__FILE__) . '/mail_dummy.inc', 'my_name.html', 'text/html', 'iso-8859-15');
-echo $mail->send(array('type' => 'dummy'));
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
require_once 'Horde/String.php';
require_once 'Horde/Util.php';
+$dummy = Mail::factory('dummy');
+
$mail = new Horde_Mime_Mail(array('subject' => 'My Subject',
'body' => "This is\nthe body",
'to' => 'recipient@example.com',
'from' => 'sender@example.com',
'charset' => 'iso-8859-15'));
-$raw = $mail->send(array('type' => 'dummy'));
+
+$mail->send($dummy);
+$raw = $dummy->send_output;
+
echo $raw;
preg_match('/^Message-ID: (.*)$/m', $raw, $id1);
$mail->addHeader('To', 'Änderung <other@example.com>', 'utf-8');
$raw = $mail->send(array('type' => 'dummy'));
-echo $raw;
+
+$mail->send($dummy);
+$raw = $dummy->send_output;
+
preg_match('/^Message-ID: (.*)$/m', $raw, $id2);
echo "====================================================================\n";
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
-echo $mail->send(array('type' => 'dummy'));
+$dummy = Mail::factory('dummy');
+$mail->send($dummy);
+echo $dummy->send_output;
?>
--EXPECTF--
require dirname(__FILE__) . '/../../../lib/Horde/Mime/Part.php';
$_SERVER['SERVER_NAME'] = 'mail.example.com';
-class Mail_dummy extends Mail {
+class Mail_dummy extends Mail
+{
+ var $send_output;
+
function send($recipients, $headers, $body)
{
list(,$text_headers) = Mail::prepareHeaders($headers);
- return $text_headers . "\n\n" . $body;
+ $this->send_output = $text_headers . "\n\n" . $body;
}
}
$mail = new Horde_Mime_Mail();
$mail->setBody($body, 'utf-8', false);
$mail->addHeaders($headers);
- $result = $mail->send(array('type' => $this->_options['mailer']['type'], 'params' => $this->_options['mailer']['params']));
- if (is_a($result, 'PEAR_Error')) {
- print $result->getMessage() . "\n";
+ try {
+ $mail->send(Mail::factory($this->_options['mailer']['type'], $this->_options['mailer']['params']));
+ } catch (Horde_Mime_Exception $e) {
+ print $e->getMessage() . "\n";
}
}
}
try {
- $mail->send(Horde::getMailerConfig());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
/* We succeeded. */
Horde::logMessage(
'from' => $email,
'charset' => Horde_Nls::getCharset()));
try {
- $mail->send(Horde::getMailerConfig());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
$notification->push(_("Your password has been reset, check your email and log in with your new password."), 'horde.success');
header('Location: ' . Horde::getServiceLink('login')->add('url', $info['url'])->setRaw(true));
exit;
* @param Horde_Mime_Part $message The Horde_Mime_Part object that
* contains the text to send.
*
- * @throws Horde_Exception
* @throws IMP_Compose_Exception
*/
public function sendMessage($email, $headers, $message)
{
- global $conf;
+ $email = $this->_prepSendMessage($email, $headers, $message);
+
+ $mail_driver = $this->getMailDriver();
+ $mailer = Mail::factory($mail_driver['driver'], $mail_driver['params']);
+ if ($mailer instanceof PEAR_Error) {
+ throw new IMP_Compose_Exception($mailer);
+ }
+ try {
+ $message->send($email, $headers, $mailer);
+ } catch (Horde_Mime_Exception $e) {
+ throw new IMP_Compose_Exception($e);
+ }
+ }
+
+ /**
+ * Sanity checking/MIME formatting before sending a message.
+ *
+ * @param string $email The e-mail list to send to.
+ * @param Horde_Mime_Headers $headers The object holding this message's
+ * headers.
+ * @param Horde_Mime_Part $message The Horde_Mime_Part object that
+ * contains the text to send.
+ *
+ * @return string The encoded $email list.
+ * @throws IMP_Compose_Exception
+ */
+ protected function _prepSendMessage($email, $headers = null,
+ $message = null)
+ {
$timelimit = $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_timelimit');
if ($timelimit !== true) {
$sentmail = $GLOBALS['injector']->getInstance('IMP_Sentmail');
throw new IMP_Compose_Exception(_("The system is not properly configured. A detailed error description has been logged for the administrator."));
}
$sentmail = IMP_Sentmail::factory();
- $recipients = $sentmail->numberOfRecipients($conf['sentmail']['params']['limit_period'], true);
+ $recipients = $sentmail->numberOfRecipients($GLOBALS['conf']['sentmail']['params']['limit_period'], true);
foreach ($result as $address) {
$recipients += isset($address['grounpname']) ? count($address['addresses']) : 1;
}
try {
$error = Horde::callHook('perms_denied', array('imp:max_timelimit'));
} catch (Horde_Exception_HookNotSet $e) {
- $error = @htmlspecialchars(sprintf(_("You are not allowed to send messages to more than %d recipients within %d hours."), $timelimit, $conf['sentmail']['params']['limit_period']), ENT_COMPAT, Horde_Nls::getCharset());
+ $error = @htmlspecialchars(sprintf(_("You are not allowed to send messages to more than %d recipients within %d hours."), $timelimit, $GLOBALS['conf']['sentmail']['params']['limit_period']), ENT_COMPAT, Horde_Nls::getCharset());
}
throw new IMP_Compose_Exception($error);
}
}
/* Pass to hook to allow alteration of message details. */
- try {
- Horde::callHook('pre_sent', array($message, $headers), 'imp');
- } catch (Horde_Exception_HookNotSet $e) {}
+ if (!is_null($message)) {
+ try {
+ Horde::callHook('pre_sent', array($message, $headers), 'imp');
+ } catch (Horde_Exception_HookNotSet $e) {}
+ }
/* Properly encode the addresses we're sending to. */
try {
/* Validate the recipient addresses. */
try {
- $result = Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true));
- } catch (Horde_Mime_Exception $e) {
- return;
- }
-
- $mail_driver = $this->getMailDriver();
-
- try {
- $message->send($email, $headers, $mail_driver['driver'], $mail_driver['params']);
+ Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true));
} catch (Horde_Mime_Exception $e) {
throw new IMP_Compose_Exception($e);
}
+
+ return $email;
}
/**
$mail->setBasePart($multipart);
try {
- $mail->send(Horde::getMailerConfig());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
$notification->push(
sprintf(_("The event notification to %s was successfully sent."), $recipient),
'horde.success'
$mime_mail->addHeader('User-Agent', 'Kronolith ' . $GLOBALS['registry']->getVersion());
$mime_mail->setBody($message, Horde_Nls::getCharset(), true);
Horde::logMessage(sprintf('Sending event notifications for %s to %s', $event->title, implode(', ', $df_recipients)), 'DEBUG');
- $mime_mail->send(Horde::getMailerConfig(), false, false);
+ $mime_mail->send($GLOBALS['injector']->getInstance('Mail'), false, false);
}
}
}
} catch (Horde_Mime_Exception $e) {}
Horde::logMessage(sprintf('Sending daily agenda to %s', $email), 'DEBUG');
try {
- $mime_mail->send(Horde::getMailerConfig(), false, false);
+ $mime_mail->send($GLOBALS['injector']->getInstance('Mail'), false, false);
} catch (Horde_Mime_Exception $e) {}
}
}
Horde::logMessage(sprintf('Sending event notifications for %s to %s',
$task->name, implode(', ', $df_recipients)), 'INFO');
- $sent = $mail->send(Horde::getMailerConfig());
- if (is_a($sent, 'PEAR_Error')) {
- return $sent;
- }
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
}
}
}
News::getUrlFor('news', $id, true, -1));
$mail = new Horde_Mime_Mail(array('subject' => $row['title'], 'body' => $body, 'to' => $to, 'from' => $from, 'charset' => Horde_Nls::getCharset()));
-$result = $mail->send($conf['mailer']['type'], $conf['mailer']['params']);
-if ($result instanceof PEAR_Error) {
- $notification->push($result);
-} else {
+try {
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
$notification->push(sprintf(_("News succesfully send to %s"), $to), 'horde.success');
+} catch (Horde_Mime_Exception $e) {
+ $notification->push($e);
}
header('Location: ' . News::getUrlFor('news', $id));
$mail->addHeader('To', $to, Horde_Nls::getCharset());
try {
- $mail->send(Horde::getMailerConfig(), true);
+ $mail->send($GLOBALS['injector']->getInstance('Mail'), true);
$entry = sprintf('%s Message sent to %s from "%s"',
$_SERVER['REMOTE_ADDR'], $to,
Horde_Auth::getAuth());
$mail->addHeader($hkey, $headers[$hkey]);
}
try {
- $mail->send(Horde::getMailerConfig());
- } catch (Exception $e) {
- $GLOBALS['notification']->push($e->getMessage());
+ $mail->send($GLOBALS['injector']->getInstance('Mail'));
+ } catch (Horde_Mime_Exception $e) {
+ $GLOBALS['notification']->push($e);
}
}