Source code for file /joomla/utilities/mail.php
Documentation is available at mail.php
* @version $Id: mail.php 6649 2007-02-17 07:23:48Z tcp $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* E-Mail Class. Provides a common interface to send e-mail from the Joomla! Framework
* @package Joomla.Framework
// phpmailer has an issue using the relative path for it's language files
$this->SetLanguage('en', JPATH_LIBRARIES .
'/phpmailer/language/');
* Returns a reference to a global e-mail object, only creating it
* if it doesn't already exist.
* This method must be invoked as:
* <pre> $mail =& JMail::getInstance();</pre>
* NOTE: If you need an instance to use that does not have the global configuration
* values, use an id string that is not 'Joomla'.
* @param string $id The id string for the JMail instance [optional]
* @return object The global JMail object
if (!isset
($instances)) {
if (empty ($instances[$id])) {
$instances[$id] =
new JMail();
* @return mixed True if successful, a JError object otherwise
@ $result =
parent::Send();
// TODO: Set an appropriate error number
* @param array $from E-Mail address and Name of sender
* array( [0] => E-Mail Address [1] => Name )
// If $from is an array we assume it has an address and a name
// If it is a string we assume it is just the address
// If it is neither, we throw a warning
JError::raiseWarning( 0, "JMail:: Invalid E-Mail Sender: $from", "JMail::setSender($from)");
* @param string $subject Subject of the e-mail
* @param string $content Body of the e-mail
* Add recipients to the email
* @param mixed $recipient Either a string or array of strings [e-mail address(es)]
// If the recipient is an aray, add each recipient... otherwise just add the one
foreach ($recipient as $to) {
* Add carbon copy recipients to the email
* @param mixed $cc Either a string or array of strings [e-mail address(es)]
//If the carbon copy recipient is an aray, add each recipient... otherwise just add the one
* Add blind carbon copy recipients to the email
* @param mixed $cc Either a string or array of strings [e-mail address(es)]
// If the blind carbon copy recipient is an aray, add each recipient... otherwise just add the one
* Add file attachments to the email
* @param mixed $attachment Either a string or array of strings [filenames]
// If the file attachments is an aray, add each file... otherwise just add the one
foreach ($attachment as $file) {
* Add Reply to e-mail address(es) to the e-mail
* @param array $reply Either an array or multi-array of form
* array( [0] => E-Mail Address [1] => Name )
// Take care of reply email addresses
foreach ($replyto as $to) {
* Use sendmail for sending the e-mail
* @param string $sendmail Path to sendmail [optional]
* @return boolean True on success
* Use SMTP for sending the e-mail
* @param string $auth SMTP Authentication [optional]
* @param string $host SMTP Host [optional]
* @param string $user SMTP Username [optional]
* @param string $pass SMTP Password [optional]
* @return boolean True on success
function useSMTP($auth =
null, $host =
null, $user =
null, $pass =
null)
* E-Mail helper class, provides static methods to perform various tasks relevant
* to the Joomla e-mail routines.
* TODO: Test these methods as the regex work is first run and not tested thoroughly
* @package Joomla.Framework
* Cleans single line inputs.
* @param string $value String to be cleaned.
* @return string Cleaned string.
* Cleans multi-line inputs.
* @param string $value Multi-line string to be cleaned.
* @return string Cleaned multi-line string.
return trim( preg_replace( '/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i', '', $value ) );
* Cleans any injected headers from the E-Mail body.
* @param string $body E-Mail body string.
* @return string Cleaned E-Mail body string.
// Strip all E-Mail headers from a string
return preg_replace("/((From:|To:|Cc:|Bcc:|Subject:|Content-type:) ([\S]+))/", "", $body);
* Cleans any injected headers from the subject string.
* @param string $subject E-Mail subject string.
* @return string Cleaned E-Mail subject string.
return preg_replace("/((From:|To:|Cc:|Bcc:|Content-type:) ([\S]+))/", "", $subject);
* Verifies that an e-mail address does not have any extra headers injected into it.
* @param string $address E-Mail address.
* @return string|falseE-Mail address string or boolean false if injected headers are present.
* Verifies that the string is in a proper e-mail address format.
* @param string $email String to be verified.
* @return boolean True if string has the correct format; false otherwise.
if (preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $email)) {