10 defined(
'JPATH_PLATFORM') or die;
27 protected static $instances = array();
33 public $CharSet =
'utf-8';
40 public function __construct()
43 $this->SetLanguage(
'joomla', JPATH_PLATFORM .
'/phpmailer/language/');
59 public static function getInstance($id =
'Joomla')
61 if (empty(self::$instances[$id]))
63 self::$instances[$id] =
new JMail;
66 return self::$instances[$id];
77 public function Send()
81 if (($this->Mailer ==
'mail') && !function_exists(
'mail'))
83 if (class_exists(
'JError'))
89 throw new RuntimeException(sprintf(
'%s::Send mail not enabled.', get_class($this)));
93 @$result = parent::Send();
97 if (class_exists(
'JError'))
103 throw new RuntimeException(sprintf(
'%s::Send failed: "%s".', get_class($this), $this->ErrorInfo));
129 public function setSender($from)
144 elseif (is_string($from))
154 throw new UnexpectedValueException(sprintf(
'Invalid email Sender: %s, JMail::setSender(%s)', $from));
169 public function setSubject($subject)
185 public function setBody($content)
208 protected function add($recipient, $name =
'', $method =
'AddAddress')
211 if (is_array($recipient))
215 $combined = array_combine($recipient, $name);
217 if ($combined ===
false)
219 throw new InvalidArgumentException(
"The number of elements for each array isn't equal.");
222 foreach ($combined as $recipientEmail => $recipientName)
226 call_user_func(
'parent::' . $method, $recipientEmail, $recipientName);
233 foreach ($recipient as $to)
236 call_user_func(
'parent::' . $method, $to, $name);
243 call_user_func(
'parent::' . $method, $recipient, $name);
259 public function addRecipient($recipient, $name =
'')
261 $this->add($recipient, $name,
'AddAddress');
276 public function addCC($cc, $name =
'')
281 $this->add($cc, $name,
'AddCC');
297 public function addBCC($bcc, $name =
'')
302 $this->add($bcc, $name,
'AddBCC');
321 public function addAttachment($attachment, $name =
'', $encoding =
'base64', $type =
'application/octet-stream')
324 if (isset($attachment))
326 if (is_array($attachment))
328 if (!empty($name) && count($attachment) != count($name))
330 throw new InvalidArgumentException(
"The number of attachments must be equal with the number of name");
333 foreach ($attachment as $key => $file)
337 parent::AddAttachment($file, $name[$key], $encoding, $type);
341 parent::AddAttachment($file, $name, $encoding, $type);
347 parent::AddAttachment($attachment, $name, $encoding, $type);
364 public function addReplyTo($replyto, $name =
'')
366 $this->add($replyto, $name,
'AddReplyTo');
380 public function isHtml($ishtml =
true)
382 parent::IsHTML($ishtml);
396 public function useSendmail($sendmail = null)
398 $this->Sendmail = $sendmail;
400 if (!empty($this->Sendmail))
428 public function useSMTP($auth = null, $host = null, $user = null, $pass = null, $secure = null, $port = 25)
430 $this->SMTPAuth = $auth;
432 $this->Username = $user;
433 $this->Password = $pass;
436 if ($secure ==
'ssl' || $secure ==
'tls')
438 $this->SMTPSecure = $secure;
441 if (($this->SMTPAuth !== null && $this->Host !== null && $this->Username !== null && $this->Password !== null)
442 || ($this->SMTPAuth === null && $this->Host !== null))
475 public function sendMail($from, $fromName, $recipient, $subject, $body, $mode =
false, $cc = null, $bcc = null, $attachment = null,
476 $replyTo = null, $replyToName = null)
478 $this->setSubject($subject);
479 $this->setBody($body);
487 $this->addRecipient($recipient);
490 $this->addAttachment($attachment);
493 if (is_array($replyTo))
495 $numReplyTo = count($replyTo);
497 for ($i = 0; $i < $numReplyTo; $i++)
499 $this->addReplyTo(array($replyTo[$i], $replyToName[$i]));
502 elseif (isset($replyTo))
504 $this->addReplyTo(array($replyTo, $replyToName));
508 $autoReplyTo = (empty($this->ReplyTo)) ?
true :
false;
509 $this->setSender(array($from, $fromName, $autoReplyTo));
511 return $this->Send();
529 public function sendAdminMail($adminName, $adminEmail, $email, $type, $title, $author, $url = null)
533 $message = sprintf(
JText::_(
'JLIB_MAIL_MSG_ADMIN'), $adminName, $type, $title, $author, $url, $url,
'administrator', $type);
534 $message .=
JText::_(
'JLIB_MAIL_MSG') .
"\n";
536 $this->addRecipient($adminEmail);
537 $this->setSubject($subject);
538 $this->setBody($message);
540 return $this->Send();