way for users to "subscribe" to email notification

tidusx18 25 Apr, 2009
Hi,

I thought it would be good to allow users (via front end) to be able to click a button and have their email added to a default email template of my (the website owners) choosing. This is something that would be GREAT for my site since right now I'm having to do it on a per request basis manually.๐Ÿ˜€

PS: this would be some sort of php script right?

Daniel-
GreyHead 26 Apr, 2009
Hi Daniel,

I don't quite understand this - can you give a bit more detail please?

Bob
tidusx18 26 Apr, 2009
Uuuum, actually this should be in the Chrono Forms area...:P

Either way, here's an example:

On my website, users (buyers) submit requests for price quotes for various products and services. Other users (suppliers) then view these requests online or download them into a CSV file (all with chrono forms and connectivity I might add๐Ÿ™‚ ). I use the feature in Chrono Forms which allows the form to be emailed to various people to allow the suppliers to receive the requests in their email. Since right now I add and remove their emails by hand, I thought it would be nice to have the whole process automated. The suppliers would have an option to "subscribe" to a specific category (I have a separate form for each category) and their email would be added to the list of emails the forms data (on submit) will get sent to. They would also have the option to remove their email.

I'm guessing that would be really complicated lol...just an idea...

Daniel-
GreyHead 26 Apr, 2009
Hi Daniel,

If you add a table with columns user_id & catagory_id, you can populate this with the subscriptions using a new ChronoForm and then use a database lookup to pull up the subscriptions for any category when you sent the emails.

A bit fiddly but not difficult.

Bob
tidusx18 26 Apr, 2009
Great! I'll give it a shot and let you know how it goes.

PS: It's amazing how some things can have such simple solutions, but still unknown to us lol.

Daniel-
tidusx18 05 May, 2009
Bob,

I understand the table and using a form to populate it, but how can I do the database look up?

The way I'm thinking it should work is...

-create a table with the user ID and category columns
-create a ChronoForm for users to "subscribe" to specific categories (in order to get submitted data emailed to them.)
-write a SELECT statement for each form in each category selecting the user emails according to the category name.
-create dynamic to emails for each form using the selected emails from the SQL statement.

Question 1: Where would I put the SELECT statement?
Question 2: How do I get the dynamic email box to use the selected emails?

Unless I'm completely wrong in my thinking...if so I would appreciate a brief example...lol๐Ÿ˜€

Thank you!

Daniel-
GreyHead 06 May, 2009
Hi Daniel,

Adding stuff to loads of forms makes for a lot of work.

The simplest way of doing this is probably to code a PHP file by hand and 'include' it into the onsubmit code for each of the forms.

This code would pick up the category info from the current form; do a database lookup to get a list of email names and addresses; then use a foreach loop to send each of them a standard email using the info from the form. None of this uses the ChronoForms stadard code though you might borrow some functions from ChronoForms.

I'm not an expert in SQL and would have to construct the code with a bit of trial and error but it would be something like
SELECT `u.name`, `u.email` 
  FROM `#_users u` 
  INNER JOIN `#_signup_table s` ON `u.id` = `s.user_id` 
  WHERE `s.cat_id` = '$cat_id';
In this case $cat_id is taken from your current form.

Bob
tidusx18 10 May, 2009
Hi Bob,

so here's what I have so far...I was hoping you could tell me if I'm even going in the right direction?

<?php
mysql_connect("my host", "my username", "my password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("my database name");

$result = mysql_query("SELECT "user_email" FROM the table name will go here WHERE "category" = "Printing"");

$row = mysql_fetch_array($result, MYSQL_NUM));

$to = $row->user_email;
$subject = "A new lead has been submitted"
$message = "submitted form data goes here--not sure how though"
$header = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


foreach($row as $value)
mail($to, $subject, $message, $header);

mysql_free_result($result);
?>


PS: I bought a license... 8) lol

Thanks,

Daniel-
GreyHead 10 May, 2009
Hi Daniel,

Looks OK - I've tweaked it a bit - mainly to use the Joomal database code for better security.
<?php
$message = "submitted form data goes here--not sure how though";
// some code here to build the message

$subject = "A new lead has been submitted";
$header = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$db =& JFactory::getDBO();
$query ="
  SELECT `user_email` 
    FROM `#__the table name will go here` 
    WHERE `category` = 'Printing' ; 
";
$db->setQuery($query);
$subscribers = $db->loadObjectList();

foreach ( $subscribers as $subscriber ) {
  mail($subscriber->user_email, $subject, $message, $header);
}
?>

If you put this in the OnSubmit after box then the submitted data will still be available in the $_REQUEST array and you can use this to build your message.

Bob
tidusx18 10 May, 2009
Bob,

Thanks for the adjustments. Everything works great. I am really happy that I was able to get this to work so quickly and easily. I thought it was going to take a lot longer and be much more work๐Ÿ˜€.

Daniel-
tidusx18 15 May, 2009
Bob,

Having a small problem...

I want to send a plain text email because outlook seems to have issues with reading the headers when using the mail() function, but I can't seem to get the syntax right for $_REQUEST to work.

Here's what I have right now...
<?php
$message = "
Contact Details

Name: $_REQUEST{First_name} $_REQUEST{Last_name}
Company name: $_REQUEST{Company_name}
Street address: $_REQUEST{Address}
City: $_REQUEST{City}
State: $_REQUEST{State}
Zip code: $_REQUEST{Zip_code}
Phone: $_REQUEST{Phone}
Other Phone: $_REQUEST{Other_phone}
Fax: $_REQUEST{Fax}
Email: $_REQUEST{Email}
Preferred contact method: $_REQUEST{Preferred_contact_method}
";

    $subject = "A new lead has been submitted";
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers = 'FROM: info@ineedapricequote.com' . "\r\n";

    $db =& JFactory::getDBO();
    $query ="
      SELECT `user_email`
        FROM `jos_chronoforms_email_notification`
        WHERE `category` = 'Printing' ;
    ";
    $db->setQuery($query);
    $subscribers = $db->loadObjectList();

    foreach ( $subscribers as $subscriber ) {
      mail($subscriber->user_email, $subject, $message, $headers);
    }
    ?>


I've tried different uses of quotes around $_REQUEST, but all give syntax errors. hope you can help.

Daniel-
nml375 15 May, 2009
Hi,
You should use brackets, not curly braces when indexing arrays such as the $_REQUEST superglobal:
<?php
$message = "
Contact Details

Name: $_REQUEST[First_name] $_REQUEST[Last_name]
Company name: $_REQUEST[Company_name]
...


I'd recommend using J!1.5 codespace though; have a look at the JRequest class (http://api.joomla.org/Joomla-Framework/Environment/JRequest.html), and it's member functions (such as getVar() ).

You don't need to use the sprintf approach, but I personally think it makes the code easier to manage, as it separates the "template" from the data retrieval.
<?php
$message = sprintf("
Contact Details

Name: %s %s
Company name: %s
...",
  JRequest::getString('First_name'),
  JRequest::getString('Last_name'),
  JRequest::getString('Company_name'),
  ...);

/Fredrik
tidusx18 18 May, 2009
Thanks. Using brackets works fine.

Daniel-
tidusx18 19 May, 2009
Okay, quick question...

I have some forms that were not working with arrays so max gave me this code which goes in the onsubmit after box...
<?php
    $post =& JRequest::get('post');
    if ( $paramsvalues->handlepostedarrays == 'Yes' ) {
      foreach ( $post as $k => $v ) {
        if ( is_array($post[$k]) ) {
          JRequest::setVar($k, implode(", ", $post[$k]), 'post');
        }
      }
    }
    ?>


Now, I'm using a code that emails people when someone submits something and that goes in the onsubmit after box, but how should I put both sets of code in there?

Thanks,

Daniel-
GreyHead 19 May, 2009
Hi tidusx18,

Nothing special, just one after the other.

Bob
tidusx18 19 May, 2009
Hi Bob,

Actualy I just removed the other code since the arrays are working correctly in RC5๐Ÿ˜€.

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