Created
October 19, 2018 21:12
-
-
Save olivx/defcedd67f9a58504197ca33e9a0451f to your computer and use it in GitHub Desktop.
semd mail mass end track_email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# save message to DB | |
email_track = Message( | |
body=message_body, | |
sender=request.user, | |
recipient=cand.candidate, | |
job=obj, | |
uuid=uuid.uuid4() | |
) | |
_url = '{}{}?uuid={}'.format(settings.CAREER_PAGE_DOMAIN, reverse( | |
'track_message_email'), str(email_track.uuid)) | |
image_track = '<img src="' + _url + '">' | |
context = { | |
"candidate_name": firstname, | |
"candidate_email": email, | |
"message": message_body, | |
"image" : image_track | |
} | |
message_to_candidate.append(email_track) | |
html_content = render_to_string('emails/message-to-candidates.html', context=context, request=request) | |
data_context[cand.pk] = { | |
'html_message' : html_content, | |
'to_mail_list' : [cand.candidate.email] | |
} | |
send_mass_candidate_email(msg_title, candidates, data_context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load i18n %} | |
<table style="width:600px;"> | |
<tr> | |
<td align="left" style="padding:5px 20px 20px 20px;border-bottom:1px #ccc solid;"> | |
<p><font color="#333333" face="Arial" size="2">{% trans "Olá" %} {{ candidate_name }},</font></p> | |
<p><font color="#333333" face="Arial" size="2"> | |
{{ message|safe }} | |
</font></p> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="padding:5px 0 0 20px;"> | |
<font color="#333333" face="Arial" size="1"><i>*{% trans "Esta é uma mensagem automática. Não responda por favor" %}.</i></font> | |
</td> | |
</tr> | |
</table> | |
{{ image|safe }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_mass_candidate_email(subject, candidates, data): | |
''' | |
envidando email em massa para candidato | |
subject: string | |
candidates: Candidate queryset | |
data: dict | |
key: candidate pk | |
html_message: rendered message | |
to_mail_list: list string email | |
attach_list: list file object | |
estrutura do dict data | |
data = {'cand_pk' : { | |
'html_message': 'hmtl_renderizado' , | |
'to_mail_list' : ['email_1', 'email_2', 'email_3'] | |
'attach_list' : [object, object, object] | |
} | |
} | |
''' | |
# open email connection | |
connection = get_connection() | |
connection.open() | |
for cand in candidates: | |
cand_data = data[cand.pk] | |
html_content = cand_data['html_message'] | |
to_email = cand_data['to_mail_list'] | |
attach_list = cand_data.get('attach_list',[]) | |
msg = EmailMultiAlternatives( | |
subject, | |
html_content, | |
settings.SERVER_EMAIL, | |
to_email, | |
connection=connection | |
) | |
for attach in attach_list: | |
msg.attach(attach.name, attach.read()) | |
msg.attach_alternative(html_content, "text/html") | |
msg.send() | |
connection.close() # Cleanup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def track_message_email(request): | |
from StringIO import StringIO | |
import urllib | |
from PIL import Image | |
uuid = request.GET.get('uuid') | |
message_track = get_object_or_404(Message, uuid=uuid) | |
message_track.is_readed = True | |
message_track.read_at = timezone.now() | |
message_track.save() | |
image_path = 'https://s3.amazonaws.com/jobquiz/combo/images/image_trck.gif' | |
image = Image.open(StringIO(urllib.urlopen(image_path).read())) | |
return redirect(image_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment