def send_one_mail(request):
send_mail('The subject', 'The body to testing', 'userone#example.com',['usertwo#gmail.com'], fail_silently=False)
return HttpResponse("This mail has sent successfull")
And my settings.py is
EMAIL_USE_TLS = True
EMAIL_HOST = 'mail.example.com'
EMAIL_HOST_USER = 'userone#example.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = 'userone#example.com'
SERVER_EMAIL = 'userone#example.com'
I have a view (which works) as shown above, here userone#example.com is sender and usertwo#gmail.com
is the receiver.
How can i check and act according to errors in sending mail?
(I want to show "Some error occured" in such errors)
How can i determine if the usertwo#gmail.com(reciver) exists or not?
(if email can't be reached, i want to show the user that the email doesn't exist)
I am using postfix,dovecot etc. in server.
You can pick up some errors when you use fail_silently = False. Just wrap send_mail in try/except.
When i wanted more control over e-mail sending, then i stopped using django mailing completely and installed lamson instead (lamsonproject.org). You can basically create your own mail server with it, attach your django orm to it and provide detailed feedback about what has happened to your e-mails. If you insert some kind of downloadable content into those e-mails (like images), then you can even give hashes to images and verify this way if e-mail has been opened too. You could do that with django based email sending too. Lamson just gives bit more control over the what and how that goes on after you hit send button.
django-mail-queue
Anyway, i finally ended-up with using the below package.
pip install django-mail-queue
And here is the docs
https://django-mail-queue.readthedocs.org/en/latest/
Related
I'm using the django-sendgrid-v5 package to send mails in django. Everything works fine but the mail never reaches to the inbox, neither spam.
Here are my current configurations:
.env file:
EMAIL_BACKEND='sendgrid_backend.SendgridBackend'
SENDGRID_API_KEY='SG.the_rest_of_the_api_key'
settings.py file:
EMAIL_BACKEND = env('EMAIL_BACKEND')
SENDGRID_API_KEY = env('SENDGRID_API_KEY')
SENDGRID_SANDBOX_MODE_IN_DEBUG=False
and my mail function:
from django.core.mail import send_mail
send_mail( mail_subject, message, 'noreply.cpsb#nyeri.go.ke', [to_email], fail_silently=False )
On sending the email, I get no error, but still the mail doesn't get delivered.
What could I be missing?
I finally worked it through. The problem was Sender Authentication, Which By the way is not very elaborate in the documentation.
After generating an API key you're supposed to add some CNAME records to your DNS service provider for sendgrid to be authenticated to send emails.
With a lot of help from This medium article
So the problem was not on the code.
I am working on my website right now and am trying to send a confirmation email when a user creates an account. How can I do this in Python? Can I even do it in Python on my Flask app? I'm really confused right now and could just use some help. Thank you all so much :)
You can use flask_mail for this purpose
To make it work set parameters this way:
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'youremail#gmail.com' # your email
app.config['MAIL_PASSWORD'] = 'your-password' # your password
Also go to your google account security settings and enable 'Less secure app access’, but google doesn’t really like it and still sometimes you can get errors. In this case just try to play with thaws settings, turn things off and on again, at some point it should definitely work (I’m so convinced, cause this totally works for me)
You also can use the app-specific password in google settings.
Here is my properties in settings.py file:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'infobot9#gmail.com'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
and here is my send email method:
from django.core.mail import send_mail
def sendConfirmEmail(email, instance, code):
mail_subject = 'Confirmation code {}'.format(code)
message = render_to_string("msg.html", {
'user': instance,
'code': code
})
to_email = email
send_mail(mail_subject, message, 'infobot9#gmail.com', [to_email],
fail_silently=False)
My Django email sending methods work fine in my local host. After deploying it to Heroku I have allowed the login from unknown devices in my Gmail settings. Gmail does not allow the server login to my account and sends me a message:
spicious login attempt blocked
infobot9#gmail.com
Someone tried to log into your account using the password set for them. If it was not you, we recommend that you change your password as soon as possible.
Unknown Device
April 4, 11:39
Near this place: Dublin, Ireland
176.34.163.6 (IP address)
Should I set extra parameters in my settings.py file or I need change my Gmail account settings?
I urge you not to use Gmail for sending email in production. It's not designed for that, and as you've discovered there are measures in place to prevent it from being used as a spam relay. Even if you're sending legitimate email, Gmail is going to make things difficult for you.
Instead, use a service that's designed to send mail from hosted applications like SendGrid or Mailgun. These are both listed among Heroku's addons and both have free starter plans. Pick one and go through its getting sarted guide. Not only will this work better with small volumes of mail, it sets you up nicely for growth.
Allow less secure apps
Display Unlock Captcha
If you still want to use Gmail, #Pierre Monico's answer will work. I just wanted to make an edit. After allowing less secure apps to sign in to your account and Display Unlock Capatcha you should still keep two things in mind. First be sure to be logged into your browser through the account which you are using in your app to send email so that Capatch should be unlocked for that particular account and the second thing is that Google only allows Display Unlock Capatcha for Only 10 minutes . Thus if you want to use it again and again just keep Display Unlock Capatcha page open in your browser and keep it refreshing after sometime.
Also if you have 2 factor authentication enabled then these steps won't work. Those accounts have different procedure.
Above mentioned answers didn't work for me. So here's how I did it. Basically, you need to configure an app password.
Go to your Gmail account
Security > in 'Signing in to Google' section
Turn on 2-Step Verification
Set app password
and finally, need to configure settings.py or .env file(follow's the env),
EMAIL_HOST_USER=your_email#gmail.com
EMAIL_HOST_PASSWORD=generated_app_password
Tip
Using python-decouple makes it much easier to handle .env data
Consider the following settings.py snippet from https://github.com/anymail/django-anymail:
INSTALLED_APPS = [
# ...
"anymail",
# ...
]
ANYMAIL = {
# (exact settings here depend on your ESP...)
"MAILGUN_API_KEY": "<your Mailgun key>",
"MAILGUN_SENDER_DOMAIN": 'mg.example.com', # your Mailgun domain, if needed
}
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" # or sendgrid.EmailBackend, or...
DEFAULT_FROM_EMAIL = "you#example.com" # if you don't already have this in settings
In this example, both the MAILGUN_SENDER_DOMAIN and DEFAULT_FROM_EMAIL have the domain example.com.
We've recently made a change such that the two domains are different, and it seems that I am no longer able to send emails using the AnyMail backend. My theory is that MailGun 'enforces' the two domains to be the same, but it is not clear to me from the documentation whether this is indeed the case. Can anyone confirm whether this is required?
I figured out that the domains don't have to be the same. By looking at the logs in the MailGun console, I noticed that there was a 'Not delivering to previously bounced address' error:
I tried a different email address and it worked.
I'm trying to implement django-anymail with my site, and am having trouble.
Using these settings:
ANYMAIL = {
"MAILGUN_API_KEY": "key-hahahahahahahahhaha... no",
}
EMAIL_BACKEND = "anymail.backends.mailgun.MailgunBackend"
DEFAULT_FROM_EMAIL = "account-recovery#mg.mycooldomain.com"
This works fine:
send_mail(
"Test it!",
"A new user [%s] has signed up" % (username),
"Accounts <account-creation#mg.mycooldomain.com>",
["my_email#mycooldomain.com",]
)
The mail sends, it shows up in the mailgun logs, all is well.
But trying to use the in-built django registration password reset doesn't do anything.
The logs show no errors, all the templates are in the right place, it says the password reset worked but no emails show up in my inbox, nothing in the mailgun logs and not even anything in the spam box.
No emails show up at all. How can I make this work?
edit:
Also this works, but still no recovery:
from django.core.mail.message import EmailMessage
EmailMessage('hello','hello',recipients="my_email#gmail.com").send(False)
I figured out the answer, and it didn't come up elsewhere.
The call to send_mail worked as it forces an email to be sent.
However, password recovery has a lot of indirection, and one of the checks is if the user has_usable_password, which when you are working with dummy users during testing I forgot to do.
So, make sure your users have a password set before attempting to recover a password.