I'm getting string representation of html code in email.. How can i get proper html email ?
message1 = (subject, 'Here is the message', from_email, recipient_list)
message2 = (subject, html_message, from_email, recipient_list)
message2.content_subtype = "html"
send_mass_mail((message1, message2), fail_silently=False)
This might help you...
msg = "Here is the message"
message1 = (subject, msg, from_email, recipient_list)
html_message = "<p>" + msg + "</p>"
message2 = (subject, html_message, from_email, recipient_list)
message2.content_subtype = "html"
send_mass_mail((message1, message2), fail_silently=False)
Here I declared a variable for the message msg which can be concatenated with the html tag you want (here it is <p> tag) using the concatenation operator +
Thank you!
Related
Anyone know how to solved my issue, Im working with DJango-email with multiple recipient. Sending email in multiple recipient accounts from my DB are working, but now I want to send email and the email:body are depending on the Data ID.
This are the email list,
Scenario: Plate_No: 123123 will be send to example_email1#gmail.com only and ABV112 will be send again to example_email2#gmail.com and so on. Only the Plate_no assign in email will send, can someone help me to work my problem. Thank you!
auto send email script:
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(), sent_email="No")
print(cstatus)
recipient_list = []
for recipient in cstatus:
recipient_list.append(recipient.email)
print(recipient_list)
plate = ""
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html', {'content':cstatus})
plain_message = strip_tags(html_message)
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, recipient_list, html_message=html_message, fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
You can use a for-loop on your cstatus queryset to send the emails to the recipents. Did not test it, but it should look something like this:
for item in cstatus:
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html'{'content':item.Plate_no})
plain_message = item.Plate_no
recipent_list = [item.email]
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, recipient_list, html_message=html_message, fail_silently=False)
item.update(sent_email="Yes")
According to what I understood regarding your query, this might what you need:
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(), sent_email="No")
print(cstatus)
recipient_list = {}
for recipient in cstatus:
recipient_list[recipient.plate_no] = recipient.email
print(recipient_list)
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html', {'content':carreg}) # or use plate for just plate_no
plain_message = strip_tags(html_message)
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, [recipient_list[plate]], html_message=html_message, fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
Or Use mass emailing in django:
link: https://docs.djangoproject.com/en/1.8/topics/email/#send-mass-mail
message1 = ('Subject here', 'Here is the message', 'from#example.com', ['first#example.com', 'other#example.com'])
message2 = ('Another Subject', 'Here is another message', 'from#example.com', ['second#test.com'])
send_mass_mail((message1, message2), fail_silently=False)
Add all the above message results in a tuple and add it in send_mass_mail. For eg.
datatuple = (
(subject, plain_message, from_email, to_email),
(subject, plain_message, from_email, to_email)
) # to_mail -> recipient_list[plate]
send_mass_mail(datatuple)
Let me know if I was wrong.
I'm using this code to send email and it works, but when I receive email the subject looks like this and also has no message:
" No subject"
afsagsag#gmail.com To: Subject: OMG Super Important Message Good morning, <afsagsag#gmail.com>
import smtplib
import carriers
def amtg():
gmail_user = 'sgfsfsfsf#gmail.com'
gmail_password = 'rtgassds'
sent_from = gmail_user
to = ['sgssfsd#gmail.com']
subject = 'OMG Super Important Message'
body = carriers.rsting + carriers.amtg1
email_text = """\
From: %s
To: %s
subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print ('Email sent!')
except:
print ('Something went wrong...')
I have a class sendmail and I am trying to call it in other classes.The argument will determine which email to send. The argument in the sendmail class will send mail according to parameters given to it from other classes where it is being called.However,when executing it, I get error message saying argument not defined.
Here is my code:
#!/usr/bin/python
import smtplib
class sendmail(argument):
TO = 'yourmail#gmail.com'
if argument=='PIR':
SUBJECT = 'PIR'
TEXT = 'Motion is detected'
gmail_sender = 'mymail#gmail.com'
gmail_passwd = 'mypwd'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join([
'TO: %s' % TO,
'From: %s' % gmail_sender ,
'Subject: %s' % SUBJECT ,
'',
TEXT
])
try:
server.sendmail(gmail_sender, [TO], BODY)
print 'email sent'
except:
print 'error'
server.quit()
I think what you're looking for is a static function.
import smtplib
class MailUtils:
#staticmethod
def sendmail(argument):
TO = 'yourmail#gmail.com'
if argument=='PIR':
SUBJECT = 'PIR'
TEXT = 'Motion is detected'
gmail_sender = 'mymail#gmail.com'
gmail_passwd = 'mypwd'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join([
'TO: %s' % TO,
'From: %s' % gmail_sender ,
'Subject: %s' % SUBJECT ,
'',
TEXT
])
try:
server.sendmail(gmail_sender, [TO], BODY)
print 'email sent'
except:
print 'error'
server.quit()
You would use it by doing this:
import MailUtils
MailUtils.sendmail(argument)
Note: As mentioned in the comments below, this approach works best if the class contains multiple related functions, not just a single one.
I am sending email with Django using Sendgrid. I have a variable message for the message that will send, however the message holds the value of a few other variables. I would like them to be on different lines to make the email easier to read. Here is what I have, although it is not working.
if form.is_valid():
name = form.cleaned_data.get('name')
phone = form.cleaned_data.get('phone')
email = form.cleaned_data.get('email')
party_size = form.cleaned_data.get('party_size')
form_message = form.cleaned_data.get('message')
listing_address = listing.address
message = name + "\n" + phone + "<br>" + email + "<br>" + party_size + "<br>" + listing_address
send_mail('New Lead', message, 'to email', ['email#gmail.com'], fail_silently=False)
The email is being sent as this:
garrett 1234234<br>g#d.com<br>2<br>address would be here
Although I would like this:
garrett
1234234
g#d.com
2
address would be here
The best way is to create email template & provide context to email template then use generate email content. Use this content(generated_html) in send_email
as parameter html_message
send_mail('New Lead', message, 'to email',
['email#gmail.com'], fail_silently=False, html_message=genenrated_html)
You can send HTML version of email with EmailMessage:
from django.core.mail import EmailMessage
message = name + "<br>" + phone + "<br>" + email + "<br>" + party_size + "<br>" + listing_address
msg = EmailMessage(subject, message, from_email, ['email#gmail.com'])
msg.content_subtype = "html" # Main content is now text/html
msg.send()
trata utilizando el siguiente codigo:
if form.is_valid():
name = form.cleaned_data.get('name')
phone = form.cleaned_data.get('phone')
email = form.cleaned_data.get('email')
party_size = form.cleaned_data.get('party_size')
form_message = form.cleaned_data.get('message')
listing_address = listing.address
message = "<html><body><p>" name + "</p><br><p>" + phone + "</p>
<br><p>" + email + "</p><br><p>" + party_size + "</p><br><p>" +
listing_address + "</p><br></body></html>"
msg = EmailMessage(subject, message, from_email,['email#gmail.com'])
msg.content_subtype = "html" # El contenido ahora sera text/html
send_mail('New Lead', message, 'to email', ['email#gmail.com'], fail_silently=False)
No need to change the django snippet that you have given here
In Sendgrid there is a setting to do this
In the settings page where you need to make the status as on. All your plain text will be converted / parsed to HTML view
I am using EmailMultiAlternatives to send mail to a list of recipients.
context = Context({'cc': cc, 'link': link})
html_template = loader.get_template('email_new.html')
text_template = loader.get_template('email_new.txt')
from_email = 'afeld <afeld#has_questions.com>'
recipient_list = ['%s <%s>' % (e.full_name(), e.email()) for e in cc.not_responded()]
cc_list = ['%s <%s>' % (e.full_name(), e.email()) for e in cc.notification_recipients()]
message = EmailMultiAlternatives(
subject,
text_template.render(context),
from_email,
to=recipient_list,
cc=cc_list
)
Is it possible to get the recipient of an email inside the body? Something that would look like in my email.html
dear {{ recipient }}
from afeld
How would I get {{ recipient }} from recipient_list?