Actions

Difference between revisions of "Sending email from extensions"

From Joomla! Documentation

(Complete rewrite)
m (Added attachements)
Line 39: Line 39:
  
 
=== Create the mail ===
 
=== 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
+
We need to set a subject line and create the text body. The subject is set with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setSubject setSubject].
  
<source lang="php">
+
The easy way to create an email body is as a string with plain text. Use the function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setBody setBody] to add a message to the mail body. You can also attach a file with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#addAttachment addAttachment]. It takes a single file name or an array of file names as argument.
$mailer->setSubject('Your subject string');
+
</source>
+
 
+
The body is a text string of your choice set with the setBody function.
+
  
 
<source lang="php">
 
<source lang="php">
 
$body  = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
 
$body  = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
 +
$mailer->setSubject('Your subject string');
 
$mailer->setBody($body);
 
$mailer->setBody($body);
 +
// Optional file attached
 +
$mailer->addAttachment(PATH_COMPONENT.DS.'assets'.DS.'document.pdf');
 
</source>
 
</source>
  
If you prefer an email formatted in HTML, you need to tell the mailer it is html.
+
If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with [http://api.joomla.org/Unknown/PHPMailer.html#methodIsHTML IsHTML]. The subject line and any attachments are handled as above, with the exception of images embedded in the HTML. These are taken care of with the function [http://api.joomla.org/Unknown/PHPMailer.html#AddEmbeddedImage AddEmbeddedImage].
  
 
<source lang="php">
 
<source lang="php">
$body  = "<h2>Our mail</h2><div>A message to our dear readers</div>";
+
$body  = '<h2>Our mail</h2>'
 +
    . '<div>A message to our dear readers'
 +
    . '<img src="cid:logo_id" alt="logo"/></div>';
 
$mailer->isHTML(true);
 
$mailer->isHTML(true);
 
$mailer->setBody($body);
 
$mailer->setBody($body);
 +
// Optionally add embedded image
 +
$mailer->AddEmbeddedImage( PATH_COMPONENT.DS.'assets'.DS.'logo128.jpg', 'logo_id', 'logo.jpg', 'base64', 'image/jpeg' );
 
</source>
 
</source>
+
 
 +
Normally you would leave any images on your server and refer to them with an ordinary HTML image tag, to reduce size of the mail and the time sending it.
 +
 
 
=== Sending the mail ===
 
=== Sending the mail ===
  

Revision as of 08:08, 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 subject line and create the text body. The subject is set with setSubject.

The easy way to create an email body is as a string with plain text. Use the function setBody to add a message to the mail body. You can also attach a file with addAttachment. It takes a single file name or an array of file names as argument.

$body   = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
$mailer->setSubject('Your subject string');
$mailer->setBody($body);
// Optional file attached
$mailer->addAttachment(PATH_COMPONENT.DS.'assets'.DS.'document.pdf');

If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with IsHTML. The subject line and any attachments are handled as above, with the exception of images embedded in the HTML. These are taken care of with the function AddEmbeddedImage.

$body   = '<h2>Our mail</h2>'
    . '<div>A message to our dear readers'
    . '<img src="cid:logo_id" alt="logo"/></div>';
$mailer->isHTML(true);
$mailer->setBody($body);
// Optionally add embedded image
$mailer->AddEmbeddedImage( PATH_COMPONENT.DS.'assets'.DS.'logo128.jpg', 'logo_id', 'logo.jpg', 'base64', 'image/jpeg' );

Normally you would leave any images on your server and refer to them with an ordinary HTML image tag, to reduce size of the mail and the time sending it.

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