49π
β
Is there a particular reason youβre using an additional function at all? Just build your csv in memory β you canβt avoid that if youβre attaching it to email β and send that.
assigned_leads = lead.objects.filter(assigned_to__in=usercompany).distinct()
csvfile = StringIO.StringIO()
csvwriter = csv.writer(csvfile)
for leads in assigned_leads:
csvwriter.writerow([leads.business_name, leads.first_name, leads.last_name, leads.email, leads.phone_number,leads.address, leads.city, leads.state, leads.zipcode, leads.submission_date, leads.time_frame, leads.comments])
message = EmailMessage("Hello","Your Leads","myemail@gmail.com",["myemail@gmail.com"])
message.attach('invoice.csv', csvfile.getvalue(), 'text/csv')
π€Peter DeGlopper
8π
Python 3 and DictWriter
example:
import csv
from io import StringIO
from django.core.mail import EmailMessage
rows = [{'col1': 'value1', 'col2': 'value2'}]
csvfile = StringIO()
fieldnames = list(rows[0].keys())
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
email = EmailMessage(
'Subject',
'Body',
'from@email.com',
['to@email.com'],
)
email.attach('file.csv', csvfile.getvalue(), 'text/csv')
email.send()
π€Josh
- [Django]-Django javascript files
- [Django]-Disconnect signals for models and reconnect in django
- [Django]-Is it safe to rename Django migrations file?
-2π
Neither are working :(
Approach 1:
msg = EmailMultiAlternatives(subject, body, from_email, [to])
msg.attach_alternative(file_content, "text/html")
msg.content_subtype = 'html'
msg.send()
Approach 2:
# email = EmailMessage(
# 'Report Subject',
# "body",
# 'xx@yy.com',
# ['zz@uu.com'],
# connection=connection,
# )
# email.attach('Report.html', body, 'text/html')
# email.send()
π€Kuldip Patel
- [Django]-How to send asynchronous email using django
- [Django]-Accessing Primary Key from URL in Django View Class
- [Django]-Redis Python β how to delete all keys according to a specific pattern In python, without python iterating
Source:stackexchange.com