Sending email from extensions: Difference between revisions

From Joomla! Documentation

m Marked for translation
Marked this version for translation
 
Line 2: Line 2:
{{Joomla version|version=2.5|time=and after}}
{{Joomla version|version=2.5|time=and after}}
{{-}}
{{-}}
<translate>This is an example of how to send an email from a Joomla component. You would typically put this into your component's controller.</translate>
<translate><!--T:1--> This is an example of how to send an email from a Joomla component. You would typically put this into your component's controller.</translate>
<translate>
<translate>
== Fetch the Mail Object ==
== Fetch the Mail Object == <!--T:2-->
</translate>
</translate>
<translate>A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.</translate>
<translate><!--T:3--> A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.</translate>


<source lang="php">
<source lang="php">
Line 13: Line 13:


<translate>
<translate>
== Set a Sender ==
== Set a Sender == <!--T:4-->
</translate>
</translate>
<translate>The sender of an email is set with [https://api.joomla.org/cms-3/classes/JMail.html#method_setSender setSender]. The function takes an array with an email address and a name as an argument. We fetch the site's email address and name from the global configuration. These are set in the administration back-end (Global Configuration {{rarr}} Server {{rarr}} Mail Settings).</translate>
<translate><!--T:5--> The sender of an email is set with [https://api.joomla.org/cms-3/classes/JMail.html#method_setSender setSender]. The function takes an array with an email address and a name as an argument. We fetch the site's email address and name from the global configuration. These are set in the administration back-end (Global Configuration {{rarr}} Server {{rarr}} Mail Settings).</translate>


<source lang="php">
<source lang="php">
Line 30: Line 30:


<translate>
<translate>
== Recipient ==
== Recipient == <!--T:6-->
</translate>
</translate>
<translate>You set the recipient of an email with the function [https://api.joomla.org/cms-3/classes/JMail.html#method_addRecipient addRecipient].
<translate><!--T:7-->
You set the recipient of an email with the function [https://api.joomla.org/cms-3/classes/JMail.html#method_addRecipient addRecipient].
To set the email address to the currently logged in user, we fetch it from the user object.</translate>
To set the email address to the currently logged in user, we fetch it from the user object.</translate>


Line 42: Line 43:
</source>
</source>


<translate>If we had multiple recipients, we would put each recipient's email address in an array.</translate>
<translate><!--T:8--> If we had multiple recipients, we would put each recipient's email address in an array.</translate>


<source lang="php">
<source lang="php">
Line 51: Line 52:


<translate>
<translate>
== Create the Mail ==
== Create the Mail == <!--T:9-->
</translate>
</translate>
<translate>We need to set a subject line and create the text body. The subject is set with [https://api.joomla.org/cms-3/classes/JMail.html#method_setSubject setSubject].</translate>  
<translate><!--T:10--> We need to set a subject line and create the text body. The subject is set with [https://api.joomla.org/cms-3/classes/JMail.html#method_setSubject setSubject].</translate>  
<translate>
<translate>
<!--T:11-->
The easy way to create an email body is as a string with plain text. Use the function [https://api.joomla.org/cms-3/classes/JMail.html#method_setBody setBody] to add a message to the mail body. You can also attach a file with [https://api.joomla.org/cms-3/classes/JMail.html#method_addAttachment addAttachment]. It takes a single file name or an array of file names as the argument.</translate>
The easy way to create an email body is as a string with plain text. Use the function [https://api.joomla.org/cms-3/classes/JMail.html#method_setBody setBody] to add a message to the mail body. You can also attach a file with [https://api.joomla.org/cms-3/classes/JMail.html#method_addAttachment addAttachment]. It takes a single file name or an array of file names as the argument.</translate>


Line 65: Line 67:
</source>
</source>


<translate>If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with [https://api.joomla.org/cms-3/classes/JMail.html#method_isHtml isHTML]. When sending HTML emails you should normally set the [http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#property_Encoding Encoding] to base64 in order to avoid unwanted characters in the output. 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://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addEmbeddedImage AddEmbeddedImage].</translate>
<translate><!--T:12--> If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with [https://api.joomla.org/cms-3/classes/JMail.html#method_isHtml isHTML]. When sending HTML emails you should normally set the [http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#property_Encoding Encoding] to base64 in order to avoid unwanted characters in the output. 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://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addEmbeddedImage AddEmbeddedImage].</translate>


<source lang="php">
<source lang="php">
Line 78: Line 80:
</source>
</source>


<translate>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.</translate>
<translate><!--T:13--> 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.</translate>


<translate>
<translate>
== Sending the Mail ==
== Sending the Mail == <!--T:14-->
</translate>
</translate>
<translate>The mail is sent with the function [https://api.joomla.org/cms-3/classes/JMail.html#method_Send Send]. It returns '''true''' on success or a JError object.</translate>
<translate><!--T:15--> The mail is sent with the function [https://api.joomla.org/cms-3/classes/JMail.html#method_Send Send]. It returns '''true''' on success or a JError object.</translate>


<source lang="php">
<source lang="php">
Line 94: Line 96:
</source>
</source>


<translate>You would probably want to write your own error handler, if there is an error sending the mail.</translate>
<translate><!--T:16--> You would probably want to write your own error handler, if there is an error sending the mail.</translate>


<translate>The JMail object is used for sending mail in Joomla's contact manager. See the file <tt>joomla/components/com_contact/controller.php</tt></translate>
<translate><!--T:17--> The JMail object is used for sending mail in Joomla's contact manager. See the file <tt>joomla/components/com_contact/controller.php</tt></translate>
<translate>
<translate>
==See also==
==See also== <!--T:18-->
</translate>
</translate>
* [https://api.joomla.org/cms-3/classes/JFactory.html#method_getMailer JFactory->getMailer <translate>on api.joomla.org</translate>]
* [https://api.joomla.org/cms-3/classes/JFactory.html#method_getMailer JFactory->getMailer <translate><!--T:19--> on api.joomla.org</translate>]


<noinclude>
<noinclude>

Latest revision as of 00:26, 29 August 2017

Joomla! 
≥ 2.5

This is an example of how to send an email from a Joomla component. You would typically put this into your component's 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.

$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 site's email address and name from the global configuration. These are set in the administration back-end (Global Configuration    Server    Mail Settings).

$config = JFactory::getConfig();
$sender = array( 
    $config->get( 'mailfrom' ),
    $config->get( 'fromname' ) 
);

$mailer->setSender($sender);

In 3.1, $config->getValue() should be changed to $config->get()

Recipient

You set the recipient of an email with the function addRecipient. To set the email address to the currently logged in user, we fetch it from the user object.

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

$mailer->addRecipient($recipient);

If we had multiple recipients, we would put each recipient's email address 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 the 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(JPATH_COMPONENT.'/assets/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. When sending HTML emails you should normally set the Encoding to base64 in order to avoid unwanted characters in the output. 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->Encoding = 'base64';
$mailer->setBody($body);
// Optionally add embedded image
$mailer->AddEmbeddedImage( JPATH_COMPONENT.'/assets/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

The mail is sent with the function Send. It returns true on success or a JError object.

$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ';
} 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 Joomla's contact manager. See the file joomla/components/com_contact/controller.php

See also