Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate mail() function and send an email with Joomla2.5?

Based on the Joomla! documentation @ http://docs.joomla.org/Sending_email_from_extensions, I'm trying to send emails with the code below:

function sendmail($file,$mailto)
{  
    $mailer =& JFactory::getMailer();
    //var_dump($mailer); exit;
    $config =&JFactory::getConfig();
    $sender = array( 
        $config->getValue( 'config.mailfrom' ),
        $config->getValue( 'config.fromname' )
    );

    $mailer->setSender($sender);         

    $recipient = array($mailto);           
    $mailer->addRecipient($recipient);

    $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_BASE.DS.'CSV'.DS.$file);

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

    if ( $send !== true ) {
        echo 'Error sending email: ' . $send->message;
    } else {
        echo 'Mail sent';
    }
}

($file is the full path of a file zip and $mailto is a my gmail.)

However, when I send mail, I receive the error:

Could not instantiate mail function.
Fatal error: Cannot access protected property JException::$message in /var/www/html/dai/components/com_servicemanager/views/i0602/view.html.php on line 142

What is causing this error?

like image 523
mum Avatar asked Oct 02 '12 12:10

mum


3 Answers

Please save yourself some sanity and do not try to use Joomla's mailer implementation. Not only is it unreliable as you've experienced, it handles different charsets and HTML content poorly. Just include and use PHPMailer.

like image 50
Mirko Adari Avatar answered Nov 12 '22 08:11

Mirko Adari


Change

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

to

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

then run your code again. The error that you get should tell us why it isn't instantiating.

like image 31
TryHarder Avatar answered Nov 12 '22 07:11

TryHarder


In joomla send a mail with attachment file

   $from="[email protected]";//Please set Proper email id
   $fromname="noreplay";
   $to ='[email protected]';
   // Set a you want send email to
   $subject = "Download";
   $message = "Thank you For Downloading";
   $attachment = JPATH_BASE.'/media/demo.pdf';
   // set a file path
   $res = JFactory::getMailer()->sendMail($from, $fromname, $to,$subject,     $message,$mode=1,$cc = null, $bcc = null, $attachment);
   if($res)
   {
        $errormsg = "Mail Successfully Send";
   }
   else
   {
     $errormsg ="Mail Not Send";
   }

after you have check mail in your inbox or spam folder.
mail in spam folder because not proper set email id in from id.

like image 1
Mehul Jethloja Avatar answered Nov 12 '22 07:11

Mehul Jethloja