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

4

7

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?

Mcclintock answered 2/10, 2012 at 12:23 Comment(9)
what is on 142 line of view.html.php ?Whitmire
Well JException::$message is indeed protected. You can access it by casting the exception-object to a string ('Error sending email: ' . (string)$send;) or just calling the __toString-method: 'Error sending email: ' . $send->__toString();.Vollmer
GBD: echo 'Error sending email: ' . $send->message;Mcclintock
But primary problem is Could not instantiate mail function.Mcclintock
can you try to set smtp settings under Global Configuration of your joomla admin panel ?Whitmire
add this line to top of that file.jimport( 'joomla.utilities.utility' );Fructose
This error occurs when the mail-function returns false. Check if the sendmail_path is correct and that the $sender and $mailto are valid inputs.Vollmer
– GBD:i set in Global Configuration to SMTP , show echo 'Mail sent'; But In mail to , i don't receiver mail.Mcclintock
OK. thank all , i had sent sucessfullyMcclintock
C
2

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.

Cushion answered 3/10, 2012 at 13:4 Comment(0)
W
1

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.

Whitefish answered 7/3, 2013 at 5:12 Comment(0)
F
1

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.

Flosser answered 13/3, 2014 at 9:48 Comment(0)
D
0

After several years of Joomla development, I recommend using RSFORM PRO by RSJOOMLA to send mail for you after the visitor to your website fills out the form. It is much easier to manage than having to deal with the internal mail server.

Diaphanous answered 2/10, 2012 at 13:26 Comment(4)
Might be an idea to link the OP to some free contact extensions too. Not everyone goes for commercial ;) I myself have usually stuck with the standard default contact form, however if I need something more complex, I use aiContactSafeTrichina
The company, rsjoomla makes a free version with less features but it's only for Joomla 1.5: rsjoomla.com/joomla-extensions/rsform.html There likely others that are free in Joomla Extension Directory: extensions.joomla.org But I've found RS Form Pro to be worth the price.Diaphanous
true, however this question is regarding 2.5 ;)Trichina
The code in 1.5 would still be useful to look at if the OP is determined to write their own. Study code and learn how the PROs do it!Diaphanous

© 2022 - 2024 — McMap. All rights reserved.