📜 ⬆️ ⬇️

A convenient way to do mailings on the basis of the site (with an example for Django)


I will tell you about the experience of using Your MailingList Provider (YMLP) service for organizing the distribution of one of our projects.

Actually, the problem is simple and fairly common - there is a user base of the site, the task is to organize the mailing list correctly and conveniently. Users should be able to unsubscribe from our list, we should send it out, and also (very desirable) to sample profile fields (for example, to distribute only to women older than 30) and to track statistics (for example, how many people opened the letter).

I note that, in view of the fact that our project is international, there were two services in our field of view that allow us to do such things - this is the already mentioned YMLP and NetAtlantic . The second service is noticeably less flexible and convenient, so we settled on its main competitor - YMLP (in fact, not so long ago NetAtlantic had a big plus - the presence of an API, but it also appeared with YMLP, so everything became clear as day).
')
So, YMLP allows you to do all of the above, plus more. Free account allows you to do newsletters on the basis of up to 1000 people You can create groups. You can store mailing templates. You can add arbitrary fields and sample them. You can create custom forms. There is an official implementation for working with API in PHP, but the mechanism of its work is so simple that it makes no difficulty to use any other programming language, for example, Python.

A very important feature is that you can make any number of subaccounts and customize their rights.

We have been using the service since last November, there are no complaints. Highly recommend.

So, the promised example for Django is a working script for integrating the user base of this framework with YMLP. Called by cron, synchronizing the base of the site with the base YMLP:

#!/usr/bin/env python2.6 <br/>
import os , sys , urllib <br/>
from random import randint<br/>
from datetime import datetime <br/>
<br/>
sys . path . append ( '/home/www-data/university' ) <br/>
os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>
<br/>
API_URL = "www.ymlp.com/api/" <br/>
<br/>
try :<br/>
from django. utils import simplejson as json<br/>
except ImportError :<br/>
import simplejson as json<br/>
<br/>
from accounts. models import UserProfile<br/>
from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>
<br/>
_groups = None <br/>
_fields = None <br/>
<br/>
class YMLPException ( Exception ) :<br/>
pass <br/>
<br/>
def _search_id ( dicts, search_key, search_value ) :<br/>
for d in dicts:<br/>
if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>
return d. get ( "ID" ) <br/>
<br/>
def get_fields_list ( ) :<br/>
global _fields<br/>
<br/>
if _fields is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_fields = result<br/>
return _fields<br/>
<br/>
def get_groups_list ( ) :<br/>
global _groups<br/>
<br/>
if _groups is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_groups = result<br/>
return _groups<br/>
<br/>
def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
fields_list = get_fields_list ( ) <br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
<br/>
for name,value in fields. items ( ) :<br/>
field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>
if field_id is not None :<br/>
if type ( value ) is unicode :<br/>
value = value. encode ( "utf-8" ) <br/>
params [ "Field" +field_id ] = value<br/>
<br/>
params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
<br/>
def unsubscribe_contact ( email ) :<br/>
params = { <br/>
"Email" : email ,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
def delete_contact ( email , group_name ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
if __name__ == "__main__" :<br/>
print datetime . now ( ) . isoformat ( " " ) <br/>
print "Exporting..." <br/>
count = 0 <br/>
<br/>
def _subscribe_profile ( profile , fields ) :<br/>
global count, YMLP_GROUP_NAME<br/>
add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>
sys . stdout . flush ( ) <br/>
count += 1 <br/>
<br/>
for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>
if profile . filled :<br/>
fields = { <br/>
"name" : profile . user . first_name ,<br/>
"lastname" : profile . user . last_name ,<br/>
"sex" : profile . gender ,<br/>
"city" : profile . city ,<br/>
"country" : profile . country ,<br/>
"reference" : profile . reference ,<br/>
"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>
} <br/>
else :<br/>
fields = { } <br/>
<br/>
done = False <br/>
first_try = True <br/>
<br/>
while not done:<br/>
try :<br/>
_subscribe_profile ( profile , fields ) <br/>
done = True <br/>
except YMLPException as inst:<br/>
print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>
if not first_try:<br/>
done = True <br/>
continue <br/>
if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>
print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>
try :<br/>
delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>
except YMLPException:<br/>
pass <br/>
first_try = False <br/>
continue <br/>
elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>
print "Setting the 'Subscribed' flag..." <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>
print "Removing the 'Subscription Accepted' flag..." <br/>
profile . subscription_accepted = False <br/>
profile . save ( ) <br/>
elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>
sys . exit ( - 1 ) <br/>
done = True <br/>
<br/>
print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>
print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>
print <br/>

#!/usr/bin/env python2.6 <br/>
import os , sys , urllib <br/>
from random import randint<br/>
from datetime import datetime <br/>
<br/>
sys . path . append ( '/home/www-data/university' ) <br/>
os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>
<br/>
API_URL = "www.ymlp.com/api/" <br/>
<br/>
try :<br/>
from django. utils import simplejson as json<br/>
except ImportError :<br/>
import simplejson as json<br/>
<br/>
from accounts. models import UserProfile<br/>
from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>
<br/>
_groups = None <br/>
_fields = None <br/>
<br/>
class YMLPException ( Exception ) :<br/>
pass <br/>
<br/>
def _search_id ( dicts, search_key, search_value ) :<br/>
for d in dicts:<br/>
if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>
return d. get ( "ID" ) <br/>
<br/>
def get_fields_list ( ) :<br/>
global _fields<br/>
<br/>
if _fields is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_fields = result<br/>
return _fields<br/>
<br/>
def get_groups_list ( ) :<br/>
global _groups<br/>
<br/>
if _groups is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_groups = result<br/>
return _groups<br/>
<br/>
def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
fields_list = get_fields_list ( ) <br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
<br/>
for name,value in fields. items ( ) :<br/>
field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>
if field_id is not None :<br/>
if type ( value ) is unicode :<br/>
value = value. encode ( "utf-8" ) <br/>
params [ "Field" +field_id ] = value<br/>
<br/>
params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
<br/>
def unsubscribe_contact ( email ) :<br/>
params = { <br/>
"Email" : email ,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
def delete_contact ( email , group_name ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
if __name__ == "__main__" :<br/>
print datetime . now ( ) . isoformat ( " " ) <br/>
print "Exporting..." <br/>
count = 0 <br/>
<br/>
def _subscribe_profile ( profile , fields ) :<br/>
global count, YMLP_GROUP_NAME<br/>
add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>
sys . stdout . flush ( ) <br/>
count += 1 <br/>
<br/>
for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>
if profile . filled :<br/>
fields = { <br/>
"name" : profile . user . first_name ,<br/>
"lastname" : profile . user . last_name ,<br/>
"sex" : profile . gender ,<br/>
"city" : profile . city ,<br/>
"country" : profile . country ,<br/>
"reference" : profile . reference ,<br/>
"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>
} <br/>
else :<br/>
fields = { } <br/>
<br/>
done = False <br/>
first_try = True <br/>
<br/>
while not done:<br/>
try :<br/>
_subscribe_profile ( profile , fields ) <br/>
done = True <br/>
except YMLPException as inst:<br/>
print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>
if not first_try:<br/>
done = True <br/>
continue <br/>
if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>
print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>
try :<br/>
delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>
except YMLPException:<br/>
pass <br/>
first_try = False <br/>
continue <br/>
elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>
print "Setting the 'Subscribed' flag..." <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>
print "Removing the 'Subscription Accepted' flag..." <br/>
profile . subscription_accepted = False <br/>
profile . save ( ) <br/>
elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>
sys . exit ( - 1 ) <br/>
done = True <br/>
<br/>
print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>
print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>
print <br/>

#!/usr/bin/env python2.6 <br/>
import os , sys , urllib <br/>
from random import randint<br/>
from datetime import datetime <br/>
<br/>
sys . path . append ( '/home/www-data/university' ) <br/>
os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>
<br/>
API_URL = "www.ymlp.com/api/" <br/>
<br/>
try :<br/>
from django. utils import simplejson as json<br/>
except ImportError :<br/>
import simplejson as json<br/>
<br/>
from accounts. models import UserProfile<br/>
from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>
<br/>
_groups = None <br/>
_fields = None <br/>
<br/>
class YMLPException ( Exception ) :<br/>
pass <br/>
<br/>
def _search_id ( dicts, search_key, search_value ) :<br/>
for d in dicts:<br/>
if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>
return d. get ( "ID" ) <br/>
<br/>
def get_fields_list ( ) :<br/>
global _fields<br/>
<br/>
if _fields is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_fields = result<br/>
return _fields<br/>
<br/>
def get_groups_list ( ) :<br/>
global _groups<br/>
<br/>
if _groups is None :<br/>
params = { <br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if type ( result ) is dict and result. has_key ( "Code" ) :<br/>
raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>
_groups = result<br/>
return _groups<br/>
<br/>
def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
fields_list = get_fields_list ( ) <br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
<br/>
for name,value in fields. items ( ) :<br/>
field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>
if field_id is not None :<br/>
if type ( value ) is unicode :<br/>
value = value. encode ( "utf-8" ) <br/>
params [ "Field" +field_id ] = value<br/>
<br/>
params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
<br/>
def unsubscribe_contact ( email ) :<br/>
params = { <br/>
"Email" : email ,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
def delete_contact ( email , group_name ) :<br/>
groups_list = get_groups_list ( ) <br/>
<br/>
group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>
if group_id is None :<br/>
raise YMLPException ( None , "Invalid group name" ) <br/>
<br/>
params = { <br/>
"Email" : email ,<br/>
"GroupID" : group_id,<br/>
"Key" : YMLP_API_KEY,<br/>
"Username" : YMLP_USERNAME,<br/>
"Output" : "JSON" ,<br/>
} <br/>
f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>
result = json. loads ( f. read ( ) ) <br/>
<br/>
if type ( result ) is dict :<br/>
if result. get ( "Code" ) == "0" :<br/>
return True <br/>
else :<br/>
raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>
raise YMLPException ( None , "" ) <br/>
<br/>
if __name__ == "__main__" :<br/>
print datetime . now ( ) . isoformat ( " " ) <br/>
print "Exporting..." <br/>
count = 0 <br/>
<br/>
def _subscribe_profile ( profile , fields ) :<br/>
global count, YMLP_GROUP_NAME<br/>
add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>
sys . stdout . flush ( ) <br/>
count += 1 <br/>
<br/>
for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>
if profile . filled :<br/>
fields = { <br/>
"name" : profile . user . first_name ,<br/>
"lastname" : profile . user . last_name ,<br/>
"sex" : profile . gender ,<br/>
"city" : profile . city ,<br/>
"country" : profile . country ,<br/>
"reference" : profile . reference ,<br/>
"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>
} <br/>
else :<br/>
fields = { } <br/>
<br/>
done = False <br/>
first_try = True <br/>
<br/>
while not done:<br/>
try :<br/>
_subscribe_profile ( profile , fields ) <br/>
done = True <br/>
except YMLPException as inst:<br/>
print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>
if not first_try:<br/>
done = True <br/>
continue <br/>
if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>
print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>
try :<br/>
delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>
except YMLPException:<br/>
pass <br/>
first_try = False <br/>
continue <br/>
elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>
print "Setting the 'Subscribed' flag..." <br/>
profile . subscribed = True <br/>
profile . save ( ) <br/>
elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>
print "Removing the 'Subscription Accepted' flag..." <br/>
profile . subscription_accepted = False <br/>
profile . save ( ) <br/>
elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>
sys . exit ( - 1 ) <br/>
done = True <br/>
<br/>
print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>
print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>
print <br/>



Successful to you mailings! :-)

Source: https://habr.com/ru/post/100964/


All Articles