More than 800+ satisfied clients and counting, since 2010

Blog

Send Emails from Joomla Extensions in Easy Steps

Joomla content management system allows users to send emails from Joomla extensions. You can take advantage of this feature and send emails to anyone you wish, right from the Joomla extensions. This blog post will help you how to send emails from Joomla extensions in easy steps.

Important Parts of an Email

  1. Sender
  2. Receiver
  3. Mail Body
  4. Send Mail

Steps to Send Emails from Joomla Extensions

  • Set Email Sender in Joomla
  • Set Email Recipient in Joomla
  • Create the Mail Body
  • Send Your Mail From Extensions

Set Email Sender in Joomla

  • In Joomla CMS, the setSender function is used to send emails.
  • It takes an array with an email address and a name as argument.
  • The email addresses and names are fetched from the global configuration.
$config =& JFactory::getConfig();
 $sender = array(
 $config->getValue( 'config.mailfrom' ),
 $config->getValue( 'config.fromname' ) );
 $mailer->setSender($sender);

Set Email Recipient in Joomla

  • In Joomla CMS, the addRecipient function is used to receive the emails.
  • The email address of the logged in user is fetched from the user object.
  • An array of email addresses is used when there are multiple recipients.
$user =& JFactory::getUser();
$recipient = $user->email;
$mailer->addRecipient($recipient);

Create the Mail Body

  • Use setBody to compose the mail and add your message.
  • 2 ways to create mail body – plain text format or HTML format.
  • Use setSubject function for plain text or IsHTML for HTML format.

1. Plain Text Mail

$body   = "Your body stringnin 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.DS.'assets'.DS.'document.pdf');

2. HTML Format Mail

$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.DS.'assets'.DS.'logo128.jpg', 'logo_id', 'logo.jpg', 'base64', 'image/jpeg' );

Send Mail from Joomla Extensions

  • In Joomla CMS, the Send function is used to receive the emails.
  • It returns True on success else returns a JError object.
  • You can even customize and write your own error handler.
$send =& $mailer->Send();
if ( $send !== true ) {
echo 'Error sending email: ' . $send->message;
} else {
echo 'Mail sent';
}

Follow the above steps carefully to send emails from Joomla extensions. If you are unable to send mails successfully or get an error message, Request a Quote. Our Joomla team of experts will assist you to send emails from Joomla extensions.

We also develop custom Joomla extensions. Ask for designing a Joomla extension to send mails easily. You may also check our Joomla store for more useful extensions for your website.

Written by

The author didnt add any Information to his profile yet