Email Campaigns and Users
How do we send out an email campaign to sitefinity users? I currently have users under the role of customers, separated into 3 types of customers. I would like to send each type of customer a different email campaign. So far, I have only been able to send an email to all users. Is there any way to filter the users in the email campaign. Any feedback would help. Thanks.
Hi Tin,
That's a tough one, the default functionality offers you to pull from a Dynamic list that can be a list of users belonging to any of the User providers, but this will get all users created for that Provider, without the option to filter them by role. However, it's possible to create a new mailing list and fill it with Sitefinity users that belong to a certain role through code. Please find below a code sample I've prepared for you which does exactly this.
public
void
CreateMailingListFromUsersInRole(
string
rolename,
string
mailingListName,
string
subject,
string
reminder,
string
defaultFromName,
string
defaultReplyToEmail,
bool
sendWelcome,
bool
sendGoodbye)
var subscribersManager = NewslettersManager.GetManager();
var roleManager = RoleManager.GetManager();
//Get the ID of the specified role
var myroleID = roleManager.GetRole(rolename).Id;
//Get all users in that Role
var selectedUsersList = roleManager.GetUsersInRole(myroleID);
//Create a new Subscribers List and set its properties
var mailingList = subscribersManager.CreateMailingList();
mailingList.Title = mailingListName;
mailingList.DefaultSubject = subject;
mailingList.SubscriptionReminder = reminder;
mailingList.DefaultFromName = defaultFromName;
mailingList.DefaultReplyToEmail = defaultReplyToEmail;
mailingList.SendGoodByeMessage = sendGoodbye;
mailingList.SendWelcomeMessage = sendWelcome;
subscribersManager.SaveChanges();
foreach
(var myUser
in
selectedUsersList)
//Get the user DisplayName (Firstname+Lastname)
var displayName = UserProfilesHelper.GetUserDisplayName(myUser.Id);
//Create a new Subscriber and set his/her properties
var subscriber = subscribersManager.CreateSubscriber(
true
);
subscriber.FirstName = displayName.Split(
' '
).First();
subscriber.LastName = displayName.Split(
' '
).Last();
subscriber.Email = myUser.Email;
//Add the new Subscriber to the created Mailing List
mailingList.Subscribers.Add(subscriber);
subscribersManager.SaveChanges();
Trying to avoid recreating mailing list each time (since it is not a dynamic list).
How about creating new or overriding existing UsersDynamicListProvider?
This should be more flexible approach. Please let me know if it is possible and throw some code example too. Thanks!