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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
mum
  • 1,637
  • 11
  • 34
  • 58
  • 4
    what is on 142 line of view.html.php ? – GBD Oct 02 '12 at 12:27
  • 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();`. – vstm Oct 02 '12 at 12:31
  • GBD: echo 'Error sending email: ' . $send->message; – mum Oct 02 '12 at 12:32
  • But primary problem is Could not instantiate mail function. – mum Oct 02 '12 at 12:34
  • can you try to set smtp settings under Global Configuration of your joomla admin panel ? – GBD Oct 02 '12 at 12:36
  • add this line to top of that file.`jimport( 'joomla.utilities.utility' );` – Toretto Oct 02 '12 at 12:42
  • This error occurs when the [`mail`](http://php.net/manual/en/function.mail.php)-function returns false. Check if the `sendmail_path` is correct and that the `$sender` and `$mailto` are valid inputs. – vstm Oct 02 '12 at 12:46
  • – GBD:i set in Global Configuration to SMTP , show echo 'Mail sent'; But In mail to , i don't receiver mail. – mum Oct 02 '12 at 13:20
  • OK. thank all , i had sent sucessfully – mum Oct 02 '12 at 13:32

4 Answers4

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.

Mirko Adari
  • 5,083
  • 1
  • 15
  • 23
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.

TryHarder
  • 2,704
  • 8
  • 47
  • 65
1

In joomla send a mail with attachment file

   $from="noreplay@gmail.com";//Please set Proper email id
   $fromname="noreplay";
   $to ='admin@gmail.com';
   // 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.

Mehul Jethloja
  • 637
  • 4
  • 9
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.

Lodder
  • 19,758
  • 10
  • 59
  • 100
Edward
  • 9,430
  • 19
  • 48
  • 71
  • 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 aiContactSafe – Lodder Oct 02 '12 at 13:50
  • The company, rsjoomla makes a free version with less features but it's only for Joomla 1.5: http://www.rsjoomla.com/joomla-extensions/rsform.html There likely others that are free in Joomla Extension Directory: http://extensions.joomla.org But I've found RS Form Pro to be worth the price. – Edward Oct 02 '12 at 15:14
  • 2
    true, however this question is regarding 2.5 ;) – Lodder Oct 02 '12 at 15:16
  • 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! – Edward Oct 02 '12 at 15:18