Sending Bulk email in Django - django

I have to send bulk email in django, the email template will be will be customized and the some data in the template will be coming from db. i twas using django notification but it can only send email to the registered users. I have to send emails to the non-registered users. there will be five email template the user can select any one and the email has to be sent.
For ex. An invitation to the event to the group of non-registered users. user will enter email ids, and will do a bulk send. which django package can i use to achieve the same.

You can use django's default sending multiple email system. From here: https://docs.djangoproject.com/en/dev/topics/email/#sending-multiple-emails
You can try like this:
from django.core import mail
connection = mail.get_connection()
connection.open()
reciever_list= ['aa#bb.cc', 'dd#ee.ff'] #extend this list according to your requirement
email1 = mail.EmailMessage('Hello', 'Body goes here', 'from#example.com',
reciever_list, connection=connection)
email1.send()
connection.close()
For bulk email reference, you can check this so answer: How does one send an email to 10,000 users in Django?
Edit
From this stackoverflow answer, you can send emails with template. If you use django 1.7, html_message can be added as perameter of send_mail(). Details here.
By the way, for mass email handling, django has send_mass_mail() method.

Related

Django send_mail method : Include session userid in mail message

I was able to use send_mail method and it works without any problem.
What I am trying to achieve is to include session's username in mail message.
My views.py allow a certain authenticated user to create numbers. On successful addition of numbers, an email is triggered to the administrators, which at the moment does not include user's userid. So the administrators have no way of knowing which user created the numbers.
My attempt to get userid displayed in mail body below. I also tried another variant -
#send mail
subject= 'Numbers created by {request.user}'
message = 'This user {request.user} has created numbers. '
from_email= settings.EMAIL_HOST_USER
to_list = [settings.EMAIL_ADMIN]
Thanks #MohitC, I did miss f string format and plus, I was incorrectly using ```request.user method. username = request.user
subject= f" Numbers created by {username}"

Multiple emails configurations to send emails (if admin have multiple emails then admin need to choose )in Django using python

sending an email from one email is working fine, but if we want send
an email to user from multiple emails(i have 3 emails from that i need
to choose any one and receiver get email whichever i chose ) so please
help me I'm new to Django and Python
This is how to send multiple email in django.
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from#example.com',
['to#example.com', 'anotherTo#example.com'],
fail_silently=False,
)
Here's the django documentation. https://docs.djangoproject.com/en/2.2/topics/email/
I hope this help. Also beginner in django :)

django oscar: How to send an email to store admin for order placement notification [duplicate]

This question already has an answer here:
Django Oscar - email admin on order placed
(1 answer)
Closed 3 years ago.
I want to get a notification
email
to myself (As Admin) when there is new order placed on my django-oscar store.
There is a way that I am following to send emails to the admins, what I did was forked the order app and the went to the utils.py, went to the place_order function under OrderCreator() class, oscar sends signals for order placement the code from oscar's github is here, so what I did was put my send email code after this.
SO I created a communication event type with code ADMIN_ORDER_MAIL in my admin side and used that here to send mails.
My code is here:
# Send an email to admin on placing of order
ctx = {'order': order, 'user': order.user}
commtype_code = 'ADMIN_ORDER_MAIL'
try:
event_type = CommunicationEventType.objects.get(code=commtype_code)
# print("Event type typr: ", type(event_type))
# self.create_communication_event(order, event_type)
except Exception as e:
messages = CommunicationEventType.objects.get_and_render(code=commtype_code, context=ctx)
# print(e)
else:
messages = event_type.get_messages(ctx)
send_mail(
subject=messages['subject'],
message='',
html_message=messages['html'],
from_email='from email address',
recipient_list=['to email address'],
fail_silently=False,
)
I think that you can use django signals.
Django-Oscar has some signals defined for multiple actions, and one of them is associated to the action of order placed.
This signal provide information about the order made and the user who made the order. You can use this information to send the email.
I hope it will be useful for you!

sending activation emails-Django

I am creating an inactive user and want to send them email for activating there accounts like the one django-registration send when we create an account.
This is my views.py
user = User.objects.create_user(userName, userMail,userPass)
user.is_active=False
user.save()
You should review the topical guide on sending emails. Basically, you'll just use the components from django.core.mail to send an activation email with all the necessary information after you've created the user instance.
It's important that that email contains further information on how the user is supposed to activate their account. The way django-registration does it is that it has a separate model associated with the User instance that specified a unique identifier which would be used in the activation view to identify which user account is supposed to be activated, i.e. creating a GET request to http://foo/accounts/activate/550e8400-e29b-41d4-a716-446655440000 would activate the user account with the associated UUID.
There are some other intricate details that make django-registration a thorough and well-polished solution, in spite of being a bit dated (i.e. no class-based views), so I second #NCao in suggesting that you take enough time to review the sources from the official repository and ripoff and duplicate all the necessary bits.
Basically after a user signed up, you want to set user.is_active=False.
Then you send an URL with the user's information(for example, id) to the user's email.
When the user click the link, it will trigger an activation function. Within the activation function, it firstly extracts the user's information based on the URL (id). Then you can query the user object by calling user.objects.get(id=id). After that, you can set user.is_active=True and save user.
Here is the code to send email:
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
fromaddr='your email address' #(Gmail here)
username='your user name'
password='your password'
def send_email(toaddr,id):
text = "Hi!\nHow are you?\nHere is the link to activate your
account:\nhttp://127.0.0.1:8000/register_activate/activation/?id=%s" %(id)
part1 = MIMEText(text, 'plain')
msg = MIMEMultipart('alternative')
msg.attach(part1)
subject="Activate your account "
msg="""\From: %s\nTo: %s\nSubject: %s\n\n%s""" % (fromaddr,toaddr,subject,msg.as_string())
#Use gmail's smtp server to send email. However, you need to turn on the setting "lesssecureapps" following this link:
#https://www.google.com/settings/security/lesssecureapps
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr,[toaddr],msg)
server.quit()
You may also want to check this out: https://github.com/JunyiJ/django-register-activate
Hope it helps!

How to delay sending activation email in django_registration?

I am integrating django_paypal with my django_registration setup for which I have created a custom backend. I can't seem to find in the code where it sends the activation email. Is this located in the backend?
I want to wait and send the activation email until after they have completed a paypal checkout and I receive the IPN notification.
The RegistrationProfile.objects.create_inactive_user manager method has a send_email parameter, which defaults to True.
You just need to set send_email=False when you create a new user.
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site,
send_email=False)