password_reset does not pick from_email when using sendgrid - django

I have my email setup for entire django project and it works fine. When it comes to reset password in the following url:
http://127.0.0.1:8000/rest-auth/password_reset/
and after submitting the email, it throws:
SMTPDataError at /rest-auth/password_reset/
(550, b'The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved.
when I digged into the issue I noticed that this view doesn't catch from_email at all:
If I manually enter the email here, everything works fine. Also if I use gmail it works fine.
I am wondering what is gone wrong that email is not read!

That's not a bug, but implemented intentionally in Django.
Django maintains a settings.py parameters termed DEFAULT_FROM_EMAIL, which is applied by default if the from_email you've mentioned is None (in EmailMessage class construction).
Note that the email itself is sent by the PasswordResetForm class (forms.py), and it will only be sent out if the receipt is an active user (implementedin the get_users function).

Related

Setting up SendGrid using Django - User input "from_email"

All the Google searching in the world hasn't answered my question, so hoping someone can help me out.
I have a Django based "Contact Me" webpage, which uses a form and the Django send_mail function.
My app is hosted through Heroku.
When I try to submit an email on my form, the form tries send_mail(subject, message, from_email, ["mypersonal#email.com"]) But I get the following error:
(550, b'The from address does not match a verified Sender Identity.
Mail cannot be sent until this error is resolved. Visit
https://sendgrid.com/docs/for-developers/sending-email/sender-identity/
to see the Sender Identity requirements')
This seems to imply to me that the "from_email" needs to be MY verified email on SendGrid, and not the email of the person trying to contact me?
What am I missing? My goal is to have user submissions be sent to my #gmail.com address
You need to get your 'from email' validated. You can find the documentation on sendgrid it self.
You can create a sender identity by doing a POST request on https://api.sendgrid.com/v3/senders using your API key. You can find more details on sender apis here:
https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/sender_identities.html

Sending Bulk email in 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.

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)

Emails, a different 'reply to' address than sender address

I have a contact form on a website (a general form: name, email, subject, message) in which mails are sent using google apps smtp to the admins.
Currently if an administrator wants to reply to the mail directly selecting the reply option, the person's reply's To field will be filled by the sender's address automatically.
What I wan't to ask is, Is there any standardized way to pass on some additional info with the mail which would define any reply to the mail should go to this address instead of the sender's?
It does seems that there is a little chance for this option as it may lead to some problems due to spammers (They may define a custom reply field in their mail and a general user might not look where they are replying).
So as an alternative what I thought is to find a way to create a filter with sender's account which figures out the reply email address from the format and forwards the mail (Doesn't seems like a good solution and I have no idea how to achieve this).
I have tagged django, though this is not directly related with this, as I will finally implement this through django.
There are in fact standardized headers to specify response headers: http://cr.yp.to/immhf/response.html.
As far as implementing this in Django is concerned, the documentation contains an example:
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello', # email subject
'Body goes here', # email body
'from#example.com', # sender address
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
headers={'Reply-To': 'another#example.com'},
)
This solved my problem.
Reply-To is a standard SMTP header.
I can't find a good reference for it at the moment, but it is mentioned in the Wikipedia article on Email.
Edit: Found it: RFC 5322, section 3.6.2
The RFC says you can specify multiple emails and that is what I was looking for. Came up with this:
from django.core.mail import EmailMessage
headers = {'Reply-To': 'email#one.com;email#two.com'}
msg = EmailMessage(subject, html_content, EMAIL_HOST_USER, email_list, headers=headers)
msg.content_subtype = "html"
msg.send()
Works like a charm. Note: EMAIL_HOST_USER is imported from your settings file as per Django doc email setup. More on this here, search for 'reply-to': https://docs.djangoproject.com/en/dev/topics/email/
Here is also how reply-to can be used
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello',
'Body goes here',
'from#example.com',
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
reply_to=['another#example.com'],
headers={'Message-ID': 'foo'},
)
Read more at docs docs.djangoproject