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).

+

The sender of an email is set with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setSender setSender]. The function takes an array with an email address and a name as an argument. We fetch the sites email address and name from the global configuration. These are set in the administration back-end (Global Configuration -> Server -> Mail Settings).

<source lang="php">

<source lang="php">

Line 21:

Line 21:

=== Recipient ===

=== Recipient ===

−

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

+

You set the recipient of an email with the function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html# addRecipient].

+

To set the email address to the currently logged in user, we fetch it from the user object.

<source lang="php">

<source lang="php">

Line 30:

Line 31:

</source>

</source>

−

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

+

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

<source lang="php">

<source lang="php">

Line 66:

Line 67:

=== Sending the mail ===

=== Sending the mail ===

+

+

The mail is sent with the function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#Send Send]. It returns '''true''' on success or a JError object.

<source lang="php">

<source lang="php">

−

$send = $mailer->Send();

+

$send =& $mailer->Send();

if ( $send !== true ) {

if ( $send !== true ) {

echo 'Error sending email: ' . $send->message;

echo 'Error sending email: ' . $send->message;

Revision as of 07:32, 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 sender of an email is set with setSender. The function takes an array with an email address and a name as an argument. We fetch the sites email address and name from the global configuration. These are set in the administration back-end (Global Configuration -> Server -> Mail Settings).

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.

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.