CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to make a form that sends an email with tokens
Pages: [1] 2

Author Topic: How to make a form that sends an email with tokens  (Read 4694 times)

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
How to make a form that sends an email with tokens
June 03, 2011, 05:37:25 pm
Hi! I've been playing around with letting users edit their own profile information by including links like this in emails:

http://[test url]/civicrm/profile/edit?reset=1&gid=9&id={contact.contact_id}&{contact.checksum}

I would like to create a form for editing your personal information. It should ask for your email address, and after you enter it it, if you're already a contact in the database, if should send an email (maybe a saved template) with a couple profile edit links in it. How can I do this?

As far as I can tell you can only make emails get sent out if it's part of a mailing.

If I would need to program a custom Drupal module that interfaces with CiviCRM for this, can you give me some tips on what parts of the API I should use, or what functions I should call?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: How to make a form that sends an email with tokens
June 03, 2011, 09:44:41 pm

hey micah:

here are the approx steps:

1. Create a drupal form to collect email address. Add email validator to form element

2. On submit, check if email exists using contact_search API. if so, you have the contact ID, so construct below url.

You can use:

CRM_Utils_System::url - to construct the url
CRM_Contact_BAO_Contact_Utils::generateChecksum - to generate the checksum

You can use drupal mail api to send out the mail or

CRM_Utils_Mail::send

ping us on IRC if u need more details etc

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to make a form that sends an email with tokens
June 03, 2011, 10:06:29 pm
Hi,

Interesting idea.

Haven't tried, but api.Activity.Create could/should work:

Code: [Select]
$results=civicrm_api("Activity","create", array ('version' =>'3', 'source_contact_id' =>'1', 'subject' =>'Test email', 'activity_type_id' =>'3', 'target_contact_id' =>'1'));
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
July 05, 2011, 11:52:26 am
Thank you! This is very helpful, and I'm well on my way into implementing it. I'm trying to figure out how to use the contact_search API now. The normal API v3 doesn't seem to let me search for a contact by email address:

$results=civicrm_api("Contact","get", array ('version' =>'3'));

The only documentation I can find about the contact API in the wiki is here: http://wiki.civicrm.org/confluence/display/CRMDOC33/Contact+APIs

But it says this is outdated and there is no such page for CiviCRM 4. And additionally it says that the civicrm_contact_search function is deprecated. Do you have some example code for how to search for a contact that works with CiviCRM 4?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to make a form that sends an email with tokens
July 05, 2011, 12:08:57 pm
go to the latest version
http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs

This
$results=civicrm_api("Contact","get", array ('email' => 'john@doe.org','version' =>'3', return=>'sort_name,email,phone...'));


or if it's only about getting the contact id
$results=civicrm_api("Email","get", array ('email' => 'john@doe.org','version' =>'3'));

use the api explorer to help you find your way around :

http://civicrm.org/API_version_3

« Last Edit: July 05, 2011, 04:17:50 pm by xavier »
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
July 05, 2011, 04:21:57 pm
Thank you so much everyone! I have successfully gotten it working.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to make a form that sends an email with tokens
July 05, 2011, 04:46:48 pm
Cool, are you going to share the result? Seems to be a nice feature others might want to use too

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
July 05, 2011, 05:58:17 pm
Sure! Here's the code. I changed some of the function names, text, and email addresses to protect the innocent :). The module name in this code is "civicrm_custom". Also note that no matter what email address gets entered into the form the same message gets displayed. This is to prevent someone with a list of email addresses from learning which of those people donated to our organization. Also note that if the emailing sending fails, it doesn't doesn't display an error message. We don't want people to be able to learn who our donors are even if our email setup is broken. Instead, it uses the php mail() function to let us know that our email setup is down (which, in our case, is a remote SMTP server).

You might need to change the line that sets $query to use your own profile id. Right now it says gid=9, which is a custom profile on my site. Also note at the bottom it creates a new activity with activity_type_id 45. This is a new activity type I made for updating stored data.


Code: [Select]
<?php
// make a "/account" page
function civicrm_custom_menu() {
    
$items['account'] = array(
        
'page callback' => 'civicrm_custom_account_page',
        
'access callback' => true
    
);  
    return 
$items;
}

// the page renders the form
function civicrm_custom_account_page() {
    return 
drupal_get_form('civicrm_custom_form');
}

// form to change account information
function civicrm_custom_form($form_state) {
    
$form = array(
        
'h1' => array( '#markup' => "<h1>Update Contact Information and Preferences</h1>"),
        
'p' => array( '#markup' => "<p>Fill out the form below to update your contact information and mailing list settings. You will receive an email with a link to that you can follow to change your data.</p>"),
        
'email' => array(
            
'#type' => 'textfield',
            
'#title' => t('Email Address'),
            
'#size' => 30, 
            
'#maxlength' => 128,
            
'#description' => t('Enter the email address that you gave us')
        ),
        
'submit' => array(
            
'#type' => 'submit',
            
'#value' => t('Send me an link to update my information')
        )
    );
    return 
$form;
}

function 
civicrm_custom_form_validate($form, &$form_state) {
    if(
$form_state['values']['email'] == '') {
        
form_set_error('', t('You must enter an email address.'));
    } else if(
filter_var($form_state['values']['email'], FILTER_VALIDATE_EMAIL) == false) {
        
form_set_error('', t('You must enter a valid email address.'));
    }

}

function 
civicrm_custom_form_submit($form, &$form_state) {
    
$email = $form_state['values']['email'];

    
// everyone gets this message
    
drupal_set_message(t('If your email address is in our database, you will receive an email shortly with a link to update your contact information and preferences. If you do not receive an email and you are sure you have entered the correct email address, please contact us.'));

    
// initialize civicrm stuff
    
civicrm_initialize(true);
    require_once(
drupal_get_path('module', 'civicrm').'/../CRM/Contact/BAO/Contact/Utils.php');
    require_once(
drupal_get_path('module', 'civicrm').'/../CRM/Utils/Mail.php');

    
// try looking up the contact
    
$result = civicrm_api('Contact', 'get', array('email' => $email, 'version' => 3));
    foreach(
$result['values'] as $contact) {
        
// generate a url for this contact
        
$checksum = CRM_Contact_BAO_Contact_Utils::generateChecksum($contact['contact_id'], time(), 24); // 24 hour checksum
        
$query = "reset=1&gid=9&id=".$contact['contact_id']."&".$checksum;
        
$url = CRM_Utils_System::url('civicrm/profile/edit', $query, true, null, false);

        
// email the contact
        
$email = array(
            
'from' => 'My Organization <info@example.org>',
            
'toName' => $contact['display_name'],
            
'toEmail' => $contact['email'],
            
'subject' => 'Update your contact information and preferences',
            
'text' => "Someone, hopefully you, has requested to update the contact information and preferences that we have on file. If you have not done this, please ignore this email.\n\nUpdate your information here:\n".$url."\n\nThis URL will expire after 24 hours.\n"
        
);
        if(!
CRM_Utils_Mail::send($email)) {
            
// on fail, email webmaster@example.org
            
mail('webmaster@example.org', 'CiviCRM failed sending an update email to '.$contact['email'], 'Hey, go make sure emails are getting sent!');
        }

        
// add an activity saying they updated their info
        
$result = civicrm_api('Activity', 'create', array('version' => 3, 'activity_type_id' => 45, 'target_contact_id' => $contact['contact_id'], 'subject' => 'Update stored data email sent'));
    }
}

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: How to make a form that sends an email with tokens
July 05, 2011, 09:27:28 pm
is this mostly for users who don't have a Drupal User Account and thus can't just be told to login or request new password (basically the email validation) and then have that Profile you want them to access to be available as the Drupal User Edit profile?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
July 06, 2011, 10:53:52 am
Yes. The only people at our organization who have passwords to our website is staff, but we have tens of thousands of contacts in our database.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: How to make a form that sends an email with tokens
July 25, 2011, 08:29:31 pm
Hi,

We have this working & have generalised it slightly - (added an admin settings form where you can choose profileid & activity type - maybe others soon)

code is here

https://svn.fuzion.co.nz/repos/fuzion-code
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: How to make a form that sends an email with tokens
July 28, 2011, 02:03:06 pm
would this be an option for users with no Drupal account to get a link to get back to 'my dashboard' and/or send out 'membership tokens' so people can self-verify their membership?
also does it offer any options for letting people 'cancel' their own civi events?

just thinking through some of the things that frustrate contacts who don't have accounts.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
July 29, 2011, 11:03:19 am
I'm not sure about the dashboard or canceling events, but you can definitely use this to let people check on their membership status. I think the easiest way would be to check to see if they're a member and if so what their status is and include this information in the email that gets sent out. As far as other things, anything that can be accessibly to users by putting special token in an email should be usably with this form.

Erich Schulz

  • I post frequently
  • ***
  • Posts: 142
  • Karma: 5
    • When no-one understands what you are going on about its time to start a blog
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.somthing
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
January 24, 2013, 02:16:58 am
wow Michah's code looks extremely useful... is this approach still considered "good practice" or is there a better way?

Micah Lee

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 4
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
  • MySQL version: 5.1.49
  • PHP version: 5.3.3
Re: How to make a form that sends an email with tokens
January 24, 2013, 09:53:09 am
Quote from: Erich Schulz on January 24, 2013, 02:16:58 am
wow Michah's code looks extremely useful... is this approach still considered "good practice" or is there a better way?

I'm still using it.

Functionality like this hasn't been built into CiviCRM or anything, so yeah I think this is the best way to do it right now.

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to make a form that sends an email with tokens

This forum was archived on 2017-11-26.