Actions

Difference between revisions of "Sending email from extensions"

From Joomla! Documentation

m (Added link to "Docs JFactory->getMailer" and tagge source as php)
(Complete rewrite)
Line 1: Line 1:
This works to send an email to the currently logged in user when used in a function in a model for a component and called from a view:
+
This is an example of how to send an email from a component. You would typically put this into your components controller.
 +
 
 +
=== Fetch the mail object ===
 +
A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.
 +
 
 
<source lang="php">
 
<source lang="php">
  $user =& JFactory::getUser();
+
$mailer =& JFactory::getMailer();
  $message =& JFactory::getMailer();
+
  $message->addRecipient($user->email);
+
  $message->setSubject('Your subject string');
+
  $message->setBody("Your body string\nin double quotes if you want to parse the \nnewlines etc");
+
  $sender = array( 'sender@email.address.org', 'Sender Name' );
+
  $message->setSender($sender);
+
  $sent = $message->send();
+
  if ($sent != 1) echo 'Error sending email';
+
 
</source>
 
</source>
 +
 +
=== Set a sender ===
 +
The mailer expect the sender to be an array of an email address and a name. We fetch the sites email address and name from the configuration. These are set in the administration back-end (Global Configuration -> Server -> Mail Settings).
 +
 +
<source lang="php">
 +
$config =& JFactory::getConfig();
 +
$sender = array(
 +
    $config->getValue( 'config.mailfrom' ),
 +
    $config->getValue( 'config.fromname' ) );
 +
 +
$mailer->setSender($sender);
 +
</source>
 +
 +
=== Recipient ===
 +
The currently logged in users email address is stored in the user object.
 +
 +
<source lang="php">
 +
$user =& JFactory::getUser();
 +
$recipient = $user->email;
 +
 +
$mailer->addRecipient($recipient);
 +
</source>
 +
 +
If we had multiple recipients we would put each recipients email in an array.
 +
 +
<source lang="php">
 +
$recipient = array( 'person1@domain.com', 'person2@domain.com', 'person3@domain.com' );
 +
 +
$mailer->addRecipient($recipient);
 +
</source>
 +
 +
=== Create the mail ===
 +
We need to set a mail subject and create the body of the mail. The subject is easy to set using the mailer object
 +
 +
<source lang="php">
 +
$mailer->setSubject('Your subject string');
 +
</source>
 +
 +
The body is a text string of your choice set with the setBody function.
 +
 +
<source lang="php">
 +
$body  = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
 +
$mailer->setBody($body);
 +
</source>
 +
 +
If you prefer an email formatted in HTML, you need to tell the mailer it is html.
 +
 +
<source lang="php">
 +
$body  = "<h2>Our mail</h2><div>A message to our dear readers</div>";
 +
$mailer->isHTML(true);
 +
$mailer->setBody($body);
 +
</source>
 +
 +
=== Sending the mail ===
 +
 +
<source lang="php">
 +
$send = $mailer->Send();
 +
if ( $send !== true ) {
 +
    echo 'Error sending email: ' . $send->message;
 +
} else {
 +
    echo 'Mail sent';
 +
}
 +
</source>
 +
 +
You would probably want to write your own error handler, if there is an error sending the mail.
 +
 +
The JMail object is used for sending mail in Joomlas contact manager. See the file joomla/components/com_contact/controller.php
  
 
===See also===
 
===See also===
 
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory->getMailer]
 
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory->getMailer]
 +
* [http://api.joomla.org/Joomla-Framework/JFactory.html#getMailer JFactory->getMailer on api.joomla.org]

Revision as of 05:58, 21 January 2010

This is an example of how to send an email from a component. You would typically put this into your components controller.

Contents

Fetch the mail object

A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.

$mailer =& JFactory::getMailer();

Set a sender

The mailer expect the sender to be an array of an email address and a name. We fetch the sites email address and name from the configuration. These are set in the administration back-end (Global Configuration -> Server -> Mail Settings).

$config =& JFactory::getConfig();
$sender = array( 
    $config->getValue( 'config.mailfrom' ),
    $config->getValue( 'config.fromname' ) );
 
$mailer->setSender($sender);

Recipient

The currently logged in users email address is stored in the user object.

$user =& JFactory::getUser();
$recipient = $user->email;
 
$mailer->addRecipient($recipient);

If we had multiple recipients we would put each recipients email in an array.

$recipient = array( 'person1@domain.com', 'person2@domain.com', 'person3@domain.com' );
 
$mailer->addRecipient($recipient);

Create the mail

We need to set a mail subject and create the body of the mail. The subject is easy to set using the mailer object

$mailer->setSubject('Your subject string');

The body is a text string of your choice set with the setBody function.

$body   = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
$mailer->setBody($body);

If you prefer an email formatted in HTML, you need to tell the mailer it is html.

$body   = "<h2>Our mail</h2><div>A message to our dear readers</div>";
$mailer->isHTML(true);
$mailer->setBody($body);

Sending the mail

$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ' . $send->message;
} else {
    echo 'Mail sent';
}

You would probably want to write your own error handler, if there is an error sending the mail.

The JMail object is used for sending mail in Joomlas contact manager. See the file joomla/components/com_contact/controller.php

See also