mum
mum

Reputation: 1645

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?

Upvotes: 7

Views: 5271

Answers (4)

Mehul Jethloja
Mehul Jethloja

Reputation: 637

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.

Upvotes: 1

TryHarder
TryHarder

Reputation: 2777

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.

Upvotes: 1

Mirko Adari
Mirko Adari

Reputation: 5103

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.

Upvotes: 2

Edward
Edward

Reputation: 9778

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.

Upvotes: 0

Related Questions