Posted on by

Using the PDb_Template_Email Class

The Participants Database plugin includes a handy class for sending template-based emails. You can easily use this class to send custom emails from the plugin.

The way this is usually done is to use the “send” static method of the class. You will need to provide the method with the email configuration information and a data array for use in the template. In this post, I will explain how that is done.

The Email Template

If you are familiar with the Participants Database plugin, you will have seen the template the plugin uses for sending emails. These templates are found in the plugin settings where the content of the emails are defined. The template can include “placeholder tags” that tell the email function which data field to replace the tag with.

To demonstrate how this works, if you had a line in your template like this:

Welcome, [first_name] [last_name]!

The Template Email class would use the matching values from the data array to replace the tags in the template. So, if your data array contained:

$data = array( 'first_name' => 'Roland', 'last_name' => 'Barker');

In the email, it would become:

Welcome, Roland Barker!

You aren’t constrained to the fields in the Participants Database, you can use any tags you want to invent, just include the value in the $data array.

Configuring the Email

To send an email, you will need certain values such as who the email is going to and and who it’s from. These details are provided to the class in a $config array, which looks like this:

$config = array(
 'to' => $user_email,
 'from' => 'Your Website <info@yourwebsite.com>',
 'subject' => '[first_name], You Have Been Approved!',
 'template' => 'Welcome, [first_name] [last_name]!'
);

This is how we tell the class where to send the email, who it’s from, the subject, and the body of the email. The body is usually supplied as a template, that is why it is labeled ‘template.’

You’ll also notice that the subject contains a placeholder tag. The “subject” config item will have any placeholder tags replaced by the matching data.

The “from” element is optional: if you don’t include it, the plugin settings for the “from” name and address will be used.

This is fed to the PDb_Template_Email::send() method to configure the email you want to send. When you use this method, you will change the values in that configuration array to suit your needs.

Using the PDb_Template_Email::send() Method

We use the send() method to both configure and send the email in one action. You can call this method anywhere in your WP installation…so, in your functions.php file, or in any plugin you might have…anywhere. This code snippet shows how to do that:

// define the data to use
$data = array( 'first_name' => 'Roland', 'last_name' => 'Barker');
// set up email configuration values
$config = array(
 'to' => $user_email,
 'from' => 'Your Website <info@yourwebsite.com>',
 'subject' => '[first_name], You Have Been Approved!',
 'template' => 'Welcome, [first_name] [last_name]!'
);
// now, send the email
PDb_Template_Email::send($config, $data);

The email is sent as soon as the send method is called.

That’s really all there is to it. Once you understand how it works, you can easily add any custom emails you need.

For a practical application of this class, read this tutorial:

Sending an Email when a Record is Updated

15 thoughts on “Using the PDb_Template_Email Class

  1. Bingo!!! Just need to clean up the form a bit. It works great! Thank you thank you thank you!

  2. https://phhs69.com/phhs_1969_classmate_details/?pdb=73

    Hopefully this gets accepted. The system says it was spam ;)

    1. OK, it looks like your theme or visual editor is altering the HTML of the button and the event attachment is getting lost. Normally, the contact putton shows up as another field in the output of the [pdb_single] shortcode. If you are using a visual editor, you may need to experiment with different ways of placing the content so that it does not interfere with the operation of the contact button.

      If that fails, I suggest you contact support for the visual editor for help in placing the shortcode in such a way as to avoid the HTML of the contact button being altered.

      1. Thanks, that makes sense. I just need to figure out how to add the contact button to the shortcode. Research time :)

        1. The contact button is not added to the shortcode. It is a field, so whether it is shown or not is the same for the contact button field as it would be for any field in the record.

        2. OK, beginning to make more sense. I just need to look at it when I have a moment and see if I can figure it out. thanks for all your help! I’ll let you know if I have success :)

  3. Seems simple, but it doesn’t work. I added the email expansion kit and the contact button. then added “contact” to the database. Added the form “Contact” to the individual record. The contact button shows up, click it and it goes to my homepage but no form shows. So, instead of the contact button I added the form itself to the individual record. That shows up, I fill it out, submit the email, it says thanks…the email was sent But that’s it The email never arrives. Remaining hairs now being pulled out.

    1. OK, let’s see if we can solve your first problem, which indicates some kind of javascript conflict. When the button is clicked, the email form is supposed to appear as an overlay. There is probably some other plugin that is preventing the Contact Button javascript from working as it should.

      Best way to chase this down is to use your Developer Tools and look at the “Console” tab when opening the page and clicking on the contact button. Any fatal errors in there will probably prevent the contact form from appearing.

      Also: make sure you have an email template that is set up to send the message. I needs to have the “Contact Button Email” action set.

      1. thanks, I’ll check that later today. If there’s a pop-up, it might be blocked by one of my ad blockers!

      2. As far as I can see (after re-reading the instructions a dozen times), it doesn’t work. There is no overlay when the contact button is clicked, and it takes me to the homepage with no form. The console shows no errors.

      3. I did screenshots of the complete setup. Can i send them to you? Maybe you’ll see something I don’t see!

        1. There’s nothing in the setup that would cause what you’re seeing, so no point in sending those screenshots. Can you send me a link to the page with the contact buttons in place so I can test it? I may see something that helps understand the problem.

  4. Thanks, I’ll check it out.

  5. Fried brains trying to figure out how to send an email to a member. Example: from the list of 366 members, ‘John’ selects his friend ‘Joe’. Joe’s record is displayed and John wants to send him an email. How? The email should send from the website address to Joe’s email in his profile. “reply-to” would be set to John’s email address that he enters in the form. I can add a pdb_email form on the individual’s record display. But I can’t figure out how to do this.

    1. This is something that the Contact Button add-on can do. It’s one of those things that seems like it’s going to be simple, but the add-on takes care of the hard parts.

Leave a Reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

15 thoughts on “Using the PDb_Template_Email Class

  1. Bingo!!! Just need to clean up the form a bit. It works great! Thank you thank you thank you!

  2. https://phhs69.com/phhs_1969_classmate_details/?pdb=73

    Hopefully this gets accepted. The system says it was spam ;)

    1. OK, it looks like your theme or visual editor is altering the HTML of the button and the event attachment is getting lost. Normally, the contact putton shows up as another field in the output of the [pdb_single] shortcode. If you are using a visual editor, you may need to experiment with different ways of placing the content so that it does not interfere with the operation of the contact button.

      If that fails, I suggest you contact support for the visual editor for help in placing the shortcode in such a way as to avoid the HTML of the contact button being altered.

      1. Thanks, that makes sense. I just need to figure out how to add the contact button to the shortcode. Research time :)

        1. The contact button is not added to the shortcode. It is a field, so whether it is shown or not is the same for the contact button field as it would be for any field in the record.

        2. OK, beginning to make more sense. I just need to look at it when I have a moment and see if I can figure it out. thanks for all your help! I’ll let you know if I have success :)

  3. Seems simple, but it doesn’t work. I added the email expansion kit and the contact button. then added “contact” to the database. Added the form “Contact” to the individual record. The contact button shows up, click it and it goes to my homepage but no form shows. So, instead of the contact button I added the form itself to the individual record. That shows up, I fill it out, submit the email, it says thanks…the email was sent But that’s it The email never arrives. Remaining hairs now being pulled out.

    1. OK, let’s see if we can solve your first problem, which indicates some kind of javascript conflict. When the button is clicked, the email form is supposed to appear as an overlay. There is probably some other plugin that is preventing the Contact Button javascript from working as it should.

      Best way to chase this down is to use your Developer Tools and look at the “Console” tab when opening the page and clicking on the contact button. Any fatal errors in there will probably prevent the contact form from appearing.

      Also: make sure you have an email template that is set up to send the message. I needs to have the “Contact Button Email” action set.

      1. thanks, I’ll check that later today. If there’s a pop-up, it might be blocked by one of my ad blockers!

      2. As far as I can see (after re-reading the instructions a dozen times), it doesn’t work. There is no overlay when the contact button is clicked, and it takes me to the homepage with no form. The console shows no errors.

      3. I did screenshots of the complete setup. Can i send them to you? Maybe you’ll see something I don’t see!

        1. There’s nothing in the setup that would cause what you’re seeing, so no point in sending those screenshots. Can you send me a link to the page with the contact buttons in place so I can test it? I may see something that helps understand the problem.

  4. Thanks, I’ll check it out.

  5. Fried brains trying to figure out how to send an email to a member. Example: from the list of 366 members, ‘John’ selects his friend ‘Joe’. Joe’s record is displayed and John wants to send him an email. How? The email should send from the website address to Joe’s email in his profile. “reply-to” would be set to John’s email address that he enters in the form. I can add a pdb_email form on the individual’s record display. But I can’t figure out how to do this.

    1. This is something that the Contact Button add-on can do. It’s one of those things that seems like it’s going to be simple, but the add-on takes care of the hard parts.

Leave a Reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.