Send email from components

While working in Joomla CMS, many times need arises to send mail from the components.

Joomla has a class defined for doing this.

I am listing here the method for sending mail.


<?php
$admin_email = ‘test@test.com’;
$subject = ‘Mail Testing’;
$Body = “Sending mail from component”;
$sender_email = “sender@sender.com”;
$sender_name = “Sender Name”;

$mail = &JFactory::getMailer();   //create mail object
$mail->addRecipient( $admin_email );    //mail will be sent to this email id
$mail->setSubject( $subject );  //sets the subject of the mail
$mail->setBody( $Body ); //sets the body of the mail
$sender = array( $sender_email, $sender_name ); //creates an array of sender information
$mail->setSender($sender); //sets the sender information in the mail
if(file_exists($new_file)) //$new_file will contain the physical path of the file to be sent as an attachment in the mail
{
$mail->addAttachment( $new_file ); //add attachment
}
$mail->IsHTML(true); //enable html content in mail
$sent = $mail->Send();//send mail

if ($sent != 1) echo ‘Error sending email’; //checking whether mail is sent or not
?>

One thought on “Send email from components

  1. At last, I could find your article once again. You have few useful tips for my school project. This time, I won’t forget to bookmark it. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top