Forums

Howto sent email reminder for CCNewsletter

dannyh 23 Mar, 2009
Hi All,

At the moment I'm using ....or abusing CF to sent reminder emails for the CCnewsletter module.
CCNewsletter is a good newsletter module for Joomla! but it's missing this feature at the moment.
Since recent I'm using ChronoForms...and while I'm totally not using this tool what it was once build for...it does the work.

I'll post the code here hoping others can use it as well. I've seen many requests in the forum requesting a simulair code for different issues: but many came down to the same question: email multple different email users which are stored in a table.

This code has advantages..but it also completly abuses ChronoForms..
Joomla! 1.5.8 is used.

I will enhance the code so it will store in the table that a user has been emailed.
Then, either I'll enhance the script OR I'll create another form with a different query to sent a second reminder; again updating the number of times the user has been emailed.
Then a 3rd script (or enhanced version of the stuff below) will email the user that they are deleted and the script will delete the user.

If you have any interst in such a script please do not hesitate to sent me an email of post your request as a reply to this howto.
If you enhance the script even further, please be so kind to add your enhancement as well.
The listing of the non enabled users is a raw list. Ofcourse you can put this in a table. Might do that later as well.

Steps:
1) Create a form -> Under general: name the form.
2) Fill out the code below under FORM CODE
3) Adjust your query and ofcourse adjust the stuff you need emailed.
---> check the words in CAPITAL letters in the code below..this is the stuff you need to fill out yourself!!

That's it.

It started out as a simple test trying to get a list of users emailed from a table query..but it turned out I'm totally abusing CF and I've completly used different code. With jimport('joomla.mail.helper'); and all.

Bob: I hope you don't mind🙂

To use the form: click on the link next to your forms name in the ChronoForms Manager.
Check if all the users you want emailed are in the list and click submit...CAREFULL: there's no check if you submit it twice etc...script WILL email everytime you click.



FORM HTML:


<?php

$database =& JFactory::getDBO();

$query = "SELECT id,name,email, enabled  FROM jos_ccnewsletter_subscribers WHERE enabled = '0' ";
$result = mysql_query($query) or die ('Error, insert query failed'.mysql_error());

while($row = mysql_fetch_array($result)) {

$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$code = md5($id) ;
echo "These users will be emailed:<br> $id,|  $name, | $email | $code <br>"; 
}


?>
<input value="Submit" name="undefined" type="submit">




Then in the on submit aftersending email I use this code:



<?php

$database =& JFactory::getDBO();

$query = "SELECT id,name,email, enabled  FROM jos_ccnewsletter_subscribers WHERE enabled = '0' AND name = 'lkjkl'";
$result = mysql_query($query) or die ('Error, insert query failed'.mysql_error());

while($row = mysql_fetch_array($result)) {


$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$code = md5($id) ;
echo "These users will be emailed:<br> $id,|  $name, | $email | $code <br>"; 
}

$mailcode="http://www.DOMAIN.TLD/index.php?option=com_ccnewsletter&task=activate&code=$code" ;
$subject = "YOUR SUBJECT"; 

$html_message= "DEAR $name,<br><br>YOUR TEXT. U CAN USE HTML CODE HERE. <br>THE LINK TO REGISTER : <a href='$mailcode'>CLICK TO REGISTER</a><br><br>MORE TEXT. <br><br>KIND REGARDS,<br><br>YOURDOMAIN.TLD<br><br>"; 


$fromName = "YOURFROMNAME";


$fromEmail="YOURFROMEMAIL"; 

jimport('joomla.mail.helper');

$mail = JFactory::getMailer();
$mail->addRecipient( $email );
$mail->setSender( array( $fromEmail, $fromName ) );
$mail->addReplyTo( array( $fromEmail, $fromName ) );
$mail->setSubject( $subject );
$mail->setBody( $html_message );
$mail->IsHTML(true);
$sent = $mail->Send();
}


?>



That's it.

Feel free to use it...and feel free to comment on this code, the idea or post enhancements or requests for enhancements.
Max_admin 24 Mar, 2009
Hi Dannyh,

Thank you for sharing this, the only enhancement I can see is that it would be better to use the Joomla codespace code, however your code looks ok and is platform independent, you don't need to add the $database line since you are not using it too, I will make it sticky!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
dannyh 24 Mar, 2009
Hi Max,

Thank you for the tips. I've got basic coding skills learning along the way.

Maybe people with more skills are willing to make it platform independent and/or enhance it further. If I run into the correct code that I need to use..I'll post the corrections!

Danny
GreyHead 24 Mar, 2009
Hi Danny,

This has the potential to be useful for many people so I've Joomla'd the code. Form HTML
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT id, name, email, enabled 
    FROM `#__ccnewsletter_subscribers`
    WHERE `enabled` = '0' ";
$db->setQuery($query);
$subscribers = $db->loadObjectList();
echo '$subscribers: '.print_r($subscribers, true).'<br /><br />';
echo "<div>These users will be emailed:</div>";
$s_array = array();
foreach ( $subscribers as $s ) {
  echo "<div>".$s->id." | ".$s->name." | ".$s->email."</div>";
  $s_array[] = $s->id;
}
?>
<input value="Submit" name="submit" type="submit" >
<input type='hidden' name='s_array' value='<?php echo implode(', ', $s_array); ?>' />

OnSubmit After
<?php
// Replace the parts in CAPITALS with your version
$fromName = "YOURFROMNAME";
$fromEmail="YOURFROMEMAIL";
$subject = "YOUR SUBJECT";
$html_message = "<div>DEAR $name,<br><br>YOUR TEXT. U CAN USE HTML CODE HERE. <br>THE LINK TO REGISTER : <a href='##mailcode##'>CLICK TO REGISTER</a><br><br>MORE TEXT. <br><br>KIND REGARDS,<br><br>YOURDOMAIN.TLD</div>";
$mailcode = "http://www.DOMAIN.TLD/index.php?option=com_ccnewsletter&task=activate&code=";

// do not change below here

$s_array = JRequest::getString('s_array', '', 'post');
$db =& JFactory::getDBO();

$query = "
  SELECT id, name, email
    FROM `#__ccnewsletter_subscribers` 
    WHERE `id` IN ($s_array) ;";
$db->setQuery($query);
$subscribers = $db->loadObjectList();
echo '<div>$subscribers: '.print_r($subscribers, true).'</div>';
jimport('joomla.mail.helper');
$mail =& JFactory::getMailer();
$sent = array();
foreach ( $subscribers as $s ) {
  $mailcode .= md5($s->id);
  $html_message = str_replace('##mailcode##', $mailcode, $html_message);

  $mail->addRecipient( $s->email );
  $mail->setSender( array( $fromEmail, $fromName ) );
  $mail->addReplyTo( array( $fromEmail, $fromName ) );
  $mail->setSubject( $subject );
  $mail->setBody( $html_message );
  $mail->IsHTML(true);
  $sent[] = $mail->Send();
}
echo '<div>$sent: '.print_r($sent, true).'</div>';
?>
I made one structural change. This version passes the list of subscribers in a hidden field to avoid the possibility that someone else is added in the database between the form being displayed and the submit button being clicked (unlikely but possible).
dannyh 24 Mar, 2009
Hi Bob,

Thank you, that's really cool. I'll try to expand it more based on the code you created.

Danny
LouisTran 08 Apr, 2011
Hello,

I just followed this Howto but after click to test this form, i got this error:

$subscribers:

These users will be emailed:

Warning: Invalid argument supplied for foreach() in /home/baoanws1/public_html/demo/components/com_chronocontact/chronocontact.html.php(184) : eval()'d code on line 12

Is this an error from CF or??
GreyHead 08 Apr, 2011
Hi LouisTran ,

You are getting this because there are no subscribers being found from the query. It should be better coded to prevent this error. I can fix that if needed.

In your case are there entries in the database table that should be being found?

Bob
LouisTran 09 Apr, 2011
Hi Bob,

Thanks for ur reply, pls fix it, im trying to make Newsletter with CF🙂
GreyHead 09 Apr, 2011
Hi LouisTran,

Form HTML:
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT id, name, email, enabled
    FROM `#__ccnewsletter_subscribers`
    WHERE `enabled` = '0' ";
$db->setQuery($query);
$subscribers = $db->loadObjectList();
if ( count($subscribers) ) {
  //echo '<div>$subscribers: '.print_r($subscribers, true).'</div>';
  echo "<div>These users will be emailed:</div>";
  $s_array = array();
  foreach ( $subscribers as $s ) {
    echo "<div>".$s->id." | ".$s->name." | ".$s->email."</div>";
    $s_array[] = $s->id;
  }
?>
<input value="Submit" name="submit" type="submit" >
<input type='hidden' name='s_array' value='<?php echo implode(', ', $s_array); ?>' />
<?php
} else {
  echo "Sorry, no subscribers were found";
}
?>

Bob
kungbest 25 Apr, 2011
Hi!GreyHead

I like your code it's cleanly and easy to understand.Thanks for sharing I will use this for my site🙂
smartsak 08 Aug, 2011
Hi Bob,

I understood what this mean
$code = md5($id) ;
.

Thanks
Shahzad
Francol 24 Aug, 2011
Is there an extension that cell phone spy would allow me to send a batch of emails with user passwords in the clear?
GreyHead 24 Aug, 2011
Hi Francol,

No, Joomla! doesn't keep any 'clear' record of user passwords.

Bob
MarleneS 02 Oct, 2012

This version passes the list of subscribers in a hidden field to avoid the possibility that someone else is added in the database between the form being displayed and the submit button being clicked (unlikely but possible). You can check this site for more information.



Hi

Can you please explain to me how someone else may be added between the form being displayed and the submit button being clicked? And why is this a problem? Is it a security issue?
GreyHead 02 Oct, 2012
Hi MarleneS,

This is an old thread but I think my concern was mostly top keep he housekeeping tidy. Whenever you use a database table if there is a time delay between reading the data and acting on it then it is possible that the data will have changed in the meantime. This can be a big problem with very busy transactional sites; or a very minor problem if the site has low traffic.

Better to plan for it though and decide how to handle the change. If you extract the data and pass it in the form then you fix the list at the point the table is first read; alternatively you could read the table at the point of action but then the list might be different to the one that had been displayed earlier.

Bob
This topic is locked and no more replies can be posted.