1

I need to send an email with the data that is in the mysql table. I am using joomla1.5.

  $dataarray = $this->getData(); // returns data from table
  $header = "<div> <table><tr></tr>";
  $body = "Here I need to add table data";
  $footer = "</table></div>";
  $mailer->setSubject('Mailing');
  $mailer->setBody($header.$body .$footer);

I dont how to do that. Please someone help me.

Thanks in advance.

vinay

2 Answers 2

2

You can use the JUtility class to send the mail:

JUtility::sendMail($mailfrom, $fromname, $recipient, $subject, $message, true);

$recipient is the email address you're sending the mail to, and the last parameter is a flag indicating whether the email uses HTML or not ( true = uses HTML ). However, if you have to send a lot of mails, it would be better to use the Joomla mailer instead of calling JUtility each time.

$mail =& JFactory::getMailer();

$mail->setSender(array($from, $fromname));
$mail->setSubject($subject);
$mail->setBody($body);
$mail->IsHTML(true);
$mail->addRecipient($recipient);
$mail->Send();

I hope it helped!

I'm editing, I forgot to mention how to work with your data

To craft the body of the message, it would depend on how your data is returned. If it's an associative array, you should do something like this:

$message = "Hello {$dataarray[ 'name' ]}, thank you for adding a comment to our article {$datarray[ 'article_title']}!";

If your "getData()" method is returning an object.. well in fact crafting the message is just building a string and filling it with your data. For very large emails, I usually have a template like this:

Hello %%USERNAME%%, thank you for adding a comment to our article %%ARTICLE_TITLE%%!

And then what you should do is:

$message = file_get_contents( 'your_template.tpl' ); $search = array( "%%USERNAME%%", "%%ARTICLE_TITLE%%" ); $replace = array( $dataarray[ 'name' ], $dataarray[ 'article_title' ] ); $message = str_replace( $search, $replace, $message );

That's all!

Sign up to request clarification or add additional context in comments.

Comments

0

This is the way php mail function works.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Now you need to get the values (i.e. message) from mysql tables. You can use normal mysql commands to fetch those data and thereafter use the php mail function.

2 Comments

Ok I got it cleared. Thanks for the reply Pankaj. My question was not about the mail function it was all about adding table contents to the variable. I just used .= instead of = . $mailbody = $this->getWishlists($user->id); $body .= "<div> <table><tr></tr>"; foreach($mailbody as $m){ $body .= "<tr><td>$m->author</td>$m->title<td></td><td>$m->agegroup</td></tr>"; }; $body .= "</table></div>"; Thanks again
Hi Vinay... good to know the problem has been resolved... I thought it was about mail function, and yes .= appends contents at end...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.