Simplenews - Save additional info about subscriber
Hi,
In this article, I will be taking some time letting you know how I managed to save user's information (last name, first name, etc.) along with the email address when subscribing to a newsletter with the Simplenews module.
This method uses Webform (could work with the new Entity Form too I guess) and Rules. It actually needs the Webform Rules module to work with Webform where you wouldn't need any other module if you were to choose Entity Form instead.
Simplenews allows to link the email address it saves to a user account that has the same email address on the site. This functionnality is pretty useful and I wanted to rely on it.
For it to work though, the account with the given email must actually exist.
So the solution I went for was to create a user account for each anonymous user that suscribes to the newsletter so that the email address can be linked to the newly created account in the Simplenews admin.
These created-on-the-fly users won't need a password and won't allow anonymous users to log onto the site (actually the account won't even be activated) and it gives us the possibility to save any data we want along with the email address.
The idea is to create a new form with the fields you want, then use Rules to create the user and add his email address to the newsletter mailing list.
Creating the webform is pretty easy, for example, just add two text fields for last and first names and an email field for the address.
The Rule is more complicated to build but not that hard either. You need to use the "After a webform has been submitted" event provided by the Webform Rules module. Add a condition "webform has name" to make sure the rule only triggers when our webform is submitted. And another condition as a PHP custom block (you'll need the core PHP filter actived) to check that no account already exist with this email address.
In this PHP block, we'll make use of the available $data variable and write something like :
<?php
$email = $data['components']['email']['value'][0];
?> <?php
if (user_load_by_mail($email) != NULL) {
return FALSE; // A user already exists with the given email so we don't want to create another one
} else {
return TRUE;
}
?>
<?php
echo $data['components']['firstname']['value'][0] . ' ' . $data['components']['lastname']['value'][0] ;
?>
<?php
echo $data['components']['email']['value'][0] ;
?>
Comments
Sebastian Velan... (not verified)
Sat, 02/15/2014 - 17:31
I did what you said and all worked ok, but I was wondering if you can give a
clue for do what I want.
Is possible to segment a campaign? It would be very nice and useful to be
able to select recipients according to city , zipcode, profession.
For example: select all the recivients in my list that are "lawyers" and live in "New York"
King Regards
Sebastian Velandia Häße
Nicolas Bouteille
Sat, 02/15/2014 - 17:41
I would personally go with Rules. First create one new simplenews members list for each campaign you want to do. Then create a Rules Component that takes a Simplenews registration as input and looks if the criteria are met (in your example if it is a Lawyer in New York) and if yes adds the member to the newly created list. You can use the Rules component to manually check all you already registered members with bulk operations. And you can also use this Component inside a Rules rule that would trigger whenever a new registration is made.
Hope this helps.
Sebastian Velan... (not verified)
Sat, 02/15/2014 - 19:56
Look there are 20 professions and 50 cities!! so I have to have 1 least for one possible conbination?
50C20 = 4.7*10^13 differrent lists???
this is not a good solution due to today I can select lawyers in new york but tomorrow I can select medics in Seattle, or all the medics or all the lawyers! you understand right? there is not any known solution?
For example in mailchimp you can create segmentation conditions for each campaign you are gonna send, it cannot be done with Simplenews or another news letter drupal module?
Nicolas Bouteille
Sun, 02/16/2014 - 23:25
I personally don't know any existing solution to your problem. But here's another suggestion in case you don't find it either:
If you don't want to have to create one rule component per campaign... you could approach this differently :
Create a View that lists all simplenews registrations and add to it some exposed filters that allow you to filter by Zip-code, profession, anything... then add a Rules link (https://drupal.org/project/rules_link) for example that would trigger a custom Rule of yours, more general this time, that would take all the recipients of the filtered view and add them to a new List. That way you could easily create a new campaign list anytime you want. What do you think ?
By the way, you're mentioning Mailchimp... have you considered using https://drupal.org/project/mailchimp ?
Sebastian Velandia (not verified)
Thu, 02/20/2014 - 01:07
Yea the other solution is a good one!, you should make a new tutorial for create segmented campaigns with simplenews such as the above tutorial, shouldn't you?
I have tried mailchimp but the problem is that that is a paid service and my client has up to 1 million of suscribers =/
Kind Regards
Sebastian Velandia Häße
Nicolas Bouteille
Thu, 02/20/2014 - 13:14
I wish I had the time to write more articles but more importantly change the look and feel of my website with a nice design... maybe you can do it ! ;)
Daniele (not verified)
Tue, 05/13/2014 - 16:20
It didn't work for me.
I don't know what I have to put in the Value of "The form id of the submitted form" in the 1st step.
Can you help me?
Nicolas Bouteille
Tue, 05/13/2014 - 16:33
Sorry but it's been a while since I wrote this article. Can you give me some more details about the problem you're facing? Are you referring to the comments or only the content of the article?
Daniele (not verified)
Tue, 05/13/2014 - 16:45
Hi,
Sorry. I'm referring to the content of the article.
Here:
"You need to use the "After a webform has been submitted" event provided by the Webform Rules module. Add a condition "webform has name" to make sure the rule only triggers when our webform is submitted."
When I'm editing condition, I have to fill the field "value" which corresponds to "The form id of the submitted form". But, I don't know what is this. And this field is mandatory.
So, I have tryed somethings, but the email isn't subscribed on newsletter.
Nicolas Bouteille
Tue, 05/13/2014 - 17:01
Looks like what you're asking is basic Rules stuff... if you don't know how to use Rules I suggest you find a tutorial about this module first.
Regarding your current problem, the "After a webform has been submitted" event should provide you with a variable called "webform" that you can use in your conditions. You should be able to select this "webform" variable in the "webform has name" condition. If not, maybe you need to switch to data selection instead of manual entry. You probably don't know what I'm talking about right now so I'll try to explain a little bit more: when Rules ask you for "The form id of the submitted form" for example, you should be able to select some variables in a drop down. If you're just facing an empty text field for manual entry, there's a link below the field that allows you to switch to data selection
Daniele (not verified)
Thu, 05/15/2014 - 14:42
Hi, Nicolas
Thank you so much. It still didn't work, but it's because another problem.
You are right, I never used Rules. I'll try to know better.
Thank you.
Sebastien (not verified)
Tue, 07/08/2014 - 20:55
Nice works. It works flawlessly.
But now, I'd like to push it a little further. When someone suscribe and is already suscribed, I'd like to show them another page where it says "Email is already in out list. Do you want to unsuscribe?". I am not too much used to work with Rules, and I guess a lot of people aren't used to. I will be working on this, as soon as I get something orking I will post it there. In the meantime if someone has a solution I'd listen to it :-)
Gabriel Lago (not verified)
Thu, 07/24/2014 - 19:30
Really nice, many thanks for sharing!
I needed in the form also a checkbox to accept the subscription. I share my workaround, it may help to someone else ;-)
1- Create the checkbox in the web form form
2- Add another condition to the rule that verifies if the checkbox has been set on. I´m not sure it if is this the best approach, it works for me...
I check if the checkbox array has something on it to also avoid an offset warning if the checkbox wasn´t been set.
Regards, Gabriel