How to send email with Python tried two methods Method without username, password and other username and password but Google blocker
import smtplib
content = "anything"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("Your_Username", 'Your_Password')
server.sendmail('Your_Username', "usamnet000@gmail.com", content)
server.close()
You may have left the Gmail account with block less secure apps on. This means Gmail will reject email sent by any less secure programs, and block your email. I highly recommend this tutorial , which will go through everything from setting up your Gmail account for the program.
Try the send
method below:
import smtplib,ssl
def connect(username):
serverName = username[username.index("@")+1:username.index(".")]
while True:
try:
server = smtplib.SMTP(serverDict[serverName])
except smtplib.SMTPException as error:
print(error)
continue
try:
server.ehlo()
if server.has_extn("starttls"):
server.starttls()
server.ehlo()
except (smtplib.SMTPException,ssl.SSLError) as error:
print(error)
disconnect(server)
continue
break
return server
def disconnect(server):
try:
server.quit()
except smtplib.SMTPException as error:
print(error)
def send(username,password,message):
server = connect(username)
try:
server.login(username,password)
except smtplib.SMTPException as error:
print(error)
else:
server.sendmail(username,password,message)
disconnect(server)
serverDict = {
"gmail" :"smtp.gmail.com",
"hotmail":"smtp.live.com",
"yahoo" :"smtp.mail.yahoo.com"
}
settings.py
...
...
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'pruebadjangomail@gmail.com'
EMAIL_HOST_PASSWORD = '********'
EMAIL_PORT = 587
I'd used a Django signal to send massive emails, like this
signals.py
context = {'event': instance,
'alarm': instance.alarm,
'user': user,
'device': instance.device,
'var': instance.variables,
'content_type': instance.content_type.all()}
plain_text = get_template('mail.txt') # Plain text template
text_content = plain_text.render(context)
subject = 'Event Alert: ' + instance.__str__()
from_email = 'noreply@localhost.com'
to = email
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
try:
if sub['staff_template'] is not None:
htmly = get_template(sub['staff_template']) # Define the HTML template
html_content = htmly.render(context) # Rendering the templates with context information
elif sub['staff_template_text'] != "":
htmly = Template(sub['staff_template_text'])
html_content = htmly.render(Context(context))
elif sub['user_template'] is not None:
htmly = get_template(sub['user_template']) # Define the HTML template
html_content = htmly.render(context) # Rendering the templates with context information
elif sub['user_template_text'] != "":
htmly = Template(sub['user_template_text'])
html_content = htmly.render(Context(context))
msg.attach_alternative(html_content, 'text/html')
msg.send()
except:
msg.send()
print('Mail send to %s' % email)
I suggest to use Django tools to do that, here the docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.