Skip Navigation

[Resolved] Emails using Custom Field

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Bob 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2316565

Bob

This is a bit of a follow up to https://toolset.com/forums/topic/sending-bulk-emails/ which I found searching for a way to do this.

I have a library system and what I'd like to do is send an email to people with overdue books.

The email address is held in a Member post type, the book is an Item post type. The latter has a Due Date field and a MemberID for the person that borrowed it.

I have a view that lists all over due books based on the Due Date and shows the book details and the Members email address so I am able to pull all the info together - what I'd like to do is then automate the sending of an email to each member telling them their books are overdue.

Ideally I'd like to include all the relevant detail but for starters a simple 'You have books overdue" message may suffice.

Thanks in advance.

#2316677

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Thank you for getting in touch.

Based on what I see here, you will require some amount of custom coding to achieve this.

Unfortunately the custom code required to do this is not something we can provide as this would be out of the scope of what we can provide as support.

My recommendation here is that you contact one of the registered contractors to see if this is something that they can do for you.

You can get in touch with them by going to the link below.
https://toolset.com/contractors/

Thanks,
Shane

#2316697

Bob

OK Thanks Shane.
I'm happy to explore coding it myself if you can give any advice on the direction I should look I'd appreciate it.

#2316711

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Ok so i'm checking your response and saw some initial requirements here.
The email address is held in a Member post type, the book is an Item post type. The latter has a Due Date field and a MemberID for the person that borrowed it.

From this I gather that the Member and Item post types are in some form of Post relationship. This would mean you will need to dive into the relationship data in order to get the parent email information.

Take a look at this code below. This is a simple example of how you can get the Parent Post ID and use that to get any field data from the parent post. In this case they were getting the email of the author but you can use it to get the email from a custom field to add it to a notification.

add_filter('cred_notification_recipients', 'video_reply_notification', 10, 4);
function video_reply_notification ($recipients, $notification, $form_id, $post_id){
    if (179 == $form_id){
        $parent_post_id = toolset_get_related_post( $post_id, 'reply-to-video' ); //Dives into the relationship of the current post type of the form.
        $author_id = get_post_field( 'post_author', $parent_post_id ); 
        $post_email = get_the_author_meta( 'user_email', $author_id );
         
        $recipients[] = array(
            'to'=>'to',
            'address'=>$post_email,
            'name'=>'',
            'lastname'=>''
        );
    }
    return $recipients;
}

For more detailed explanation on this hook you can see the documentation below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

Now for the relationship information you notice they are using the toolset_get_related_post() function to get the parent post ID.

The documentation that correspond to this can be seen below.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

The last thing you will need to do is to have some way to trigger the email. There are 2 ways to do this. Our Toolset Form can trigger emails based on Post Expiration.

The expiration date information is stored in a custom field on the post so you will need to manually set this with some custom code.
https://toolset.com/forums/topic/set-the-post-expiration-date-within-a-form/

The thread above has an example of how this is done.

Please let me know if these resources were able to assist you.

Thanks,
Shane

#2316731

Bob

Cheers Shane.

I've got the code to collect the info, as I said I have a view that shows all the over due books and that includes my function to pull info from the related member.

What I haven't done before is tying that in with sending an email.

I'm not sure how I can use a form here as I'm only used to them for data input.

As an aside I'm getting a lot of 404 errors on forum topics recently (including https://toolset.com/forums/topic/set-the-post-expiration-date-within-a-form/) is that something you're aware of or indeed specific to me.

#2317737

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Given that it is in a view, my only advice here is that you will need to create some function that triggers the wp_mail() function for you to send out the emails.
https://developer.wordpress.org/reference/functions/wp_mail/

So if you have a button of some sort then you can find a way to call the function and this function will send out the mails for you.

Thanks,
Shane

#2317745

Bob

Cheers Shane. I'll close the ticket, I do appreciate your help considering it is outside of scope.