I wrote a simple script to send an email via the Gmail SMTP server. Here the code:
import smtplib
msg = 'Hello World!'
server = smtplib.SMTP('smtp.gmail.com', 587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('username#googlemail.com','password')
server.sendmail('username#googlemail.com','username#googlemail.com', msg)
server.close()
Sometimes it works and sometimes I get an error. What is the reason for it? I have configured Gmail for accepting less secure apps. If the error occurs I get the following message:
Traceback (most recent call last):
File "email.py", line 31, in <module>
server.login('username#googlemail.com','password')
File "/usr/lib/python3.5/smtplib.py", line 729, in login
raise last_exception
File "/usr/lib/python3.5/smtplib.py", line 720, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.5/smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvQ\n5.7.14 DBMYWMukfjghdjfkghfjkhjkfhgjkdhgdfjkghekjghekjgndjkSm5lAOfEpP2Nt\n5.7.14 QihtNp5izjfghjjkjhgbhjbGHJVHJVjhvhjbhjbhjDZwhJFV-FiyvI-OGW\n5.7.14 jcpmHcQAcOR8e8G0zOfdugjhfdjd-sdfiugdsjfdsrthdfewrzjhg-shgv2HxmgWpZg3Z\n5.7.14 4G1ENiAlgiEnrkXyRbTG3frjZZdPg> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 g40sm24698383wrg.19 - gsmtp')
I couldn't find any help on the internet. Many thanks in advance for every advice.
The answer recommends enabling access for less secure apps first. If that does not work you could try visiting the link, which is supplied on that page: https://www.google.com/accounts/DisplayUnlockCaptcha
This may enable access.
Related
I got a problem with my python program. I Have entered the correct information about my email and password the program and the error is shown below
import smtplib, SSL
email="belginjarosh46#gmail.com"
password="my password"
port = 465
context = ssl.create_default_context()
server =smtplib.SMTP_SSL("smtp.gmail.com",port,context=context)
server.login(email,password)
message=" Hi I Am Belgin Android "
sender_mail=email,
receiver_email="belginjarosh46#gmail.com"
server.sendmail(sender_mail,receiver_email,message)
Error:
Traceback (most recent call last):
File "c:/Users/Belgin/Desktop/Python Coding/email_sending.py", line 10, in <module>
server.login(email,password)
File "G:\Python\lib\smtplib.py", line 734, in login
raise last_exception
File "G:\Python\lib\smtplib.py", line 723, in login
(code, resp) = self.auth(
File "G:\Python\lib\smtplib.py", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f21sm2590687pfn.71 - gsmtp')
To sum it up - I guess the problem was that Google does not allow you to log in with your default user password, especially (but not only) when using 2nd factor authentication.
So for using the password for 3rd party apps it requires creating app specific passwords.
I'm working on mail application and encountering a big problem with Python imaplib.
Here is the code that I try to use to login to mail server.
M = imaplib.IMAP4(self.server,int(self.port))
M.login(self.user, self.password)
I'm using "port 143", "useSLL = false", no secure.
And here is the message that I receive when trying to login to server.
DEBUG:root:Login failed.
Traceback (most recent call last):
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 347, in getMail
M.login(self.user, self.password)
File "/opt/splunk/lib/python2.7/imaplib.py", line 520, in login
raise self.error(dat[-1])
error: Login failed.
None
Traceback (most recent call last):
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 724, in <module>
parseArgs()
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 716, in parseArgs
imapProc.getMail()
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 352, in getMail
raise LoginError('Could not log into server: %s with password provided' % self.server)
__main__.LoginError: Could not log into server: (my server) with password provided
p/s: I have another mail application which get emails throw imap on ruby and it's working fine with port 143 no secure.
Anyone please help me to solve this problem. Thanks
Use this instead
M = imaplib.IMAP4_SSL(self.server,int(self.port))
M.login(self.user, self.password)
I am using imaplib.IMAP4_SSL instead of imaplib.IMAP4and this seems to work for me
I got a solution which work for both gmail and Outlook
def connect(self, username, password):
if self.tls:
self.server.starttls()
if(self.hostname == 'imap.outlook.com'):
imap_server = "outlook.office365.com"
self.server = self.transport(imap_server, self.port)
self.server = imaplib.IMAP4_SSL(imap_server)
(retcode, capabilities) = self.server.login(username,password)
self.server.select('inbox')
else:
typ, msg = self.server.login(username, password)
if self.folder:
self.server.select(self.folder)
else:
self.server.select()
I wrote a simple script to send an email via the Gmail SMTP server. Here the code:
import smtplib
msg = 'Hello World!'
server = smtplib.SMTP('smtp.gmail.com', 587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('username#googlemail.com','password')
server.sendmail('username#googlemail.com','username#googlemail.com', msg)
server.close()
Sometimes it works and sometimes I get an error. What is the reason for it? I have configured Gmail for accepting less secure apps. If the error occurs I get the following message:
Traceback (most recent call last):
File "email.py", line 31, in <module>
server.login('username#googlemail.com','password')
File "/usr/lib/python3.5/smtplib.py", line 729, in login
raise last_exception
File "/usr/lib/python3.5/smtplib.py", line 720, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.5/smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvQ\n5.7.14 DBMYWMukfjghdjfkghfjkhjkfhgjkdhgdfjkghekjghekjgndjkSm5lAOfEpP2Nt\n5.7.14 QihtNp5izjfghjjkjhgbhjbGHJVHJVjhvhjbhjbhjDZwhJFV-FiyvI-OGW\n5.7.14 jcpmHcQAcOR8e8G0zOfdugjhfdjd-sdfiugdsjfdsrthdfewrzjhg-shgv2HxmgWpZg3Z\n5.7.14 4G1ENiAlgiEnrkXyRbTG3frjZZdPg> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 g40sm24698383wrg.19 - gsmtp')
I couldn't find any help on the internet. Many thanks in advance for every advice.
The answer recommends enabling access for less secure apps first. If that does not work you could try visiting the link, which is supplied on that page: https://www.google.com/accounts/DisplayUnlockCaptcha
This may enable access.
On the vps where I host a django website, I have set up a postfix mailserver to be used by django for email. The mailserver works. Using mutt on the vps command line, I can send and receive mail to/from other accounts like gmail etc.
Still, I am having trouble using postfix to send mail from django.
I followed the advice in https://stackoverflow.com/a/28143166/1009979, with settings...
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
When I try
>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'from#example.com', ['andrews.markw#gmail.com'])
I get
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/andrews/development/wilhelm-development/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/andrews/development/wilhelm-development/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/andrews/development/wilhelm-development/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages
sent = self._send(message)
File "/home/andrews/development/wilhelm-development/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 123, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python2.7/smtplib.py", line 733, in sendmail
raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {u'andrews.markw#gmail.com': (550, '5.7.1 <vps7718.webhosting.uk.com>: Helo command rejected: Host not found')}
When I google for advice about SMTPRecipientsRefused: or Helo command rejected: Host not found, no obvious solutions to my problems arise.
Is there something wrong with the settings?
I should mention that I have no general problem problem with django email. For example, I can send gmail emails from django (following settings and procedure described here https://stackoverflow.com/a/23402208/1009979).
Make sure you have permit_mynetworks in smtpd_helo_restrictions and smtpd_recipient_restrictions in /etc/postfix/main.cf
I have a Python program that collects about 11K entries from a CORBA database, then uses those entries to parse through about 1,0000 Mb of text files. This entire process normally takes 5-7 minutes. I log into the Exchange365 server before this starts (so that any unusual events can be emailed to the administrator) and then try to email the results to the 'normal' recipients. That last email is failing and the root cause seems to be that I am getting logged off of the Exchange server.
Adding a 'dummy' email about half way through the processing seems to eliminate the timeout, but I'd like to find a more programatic way to accomplish this. Following is a code snippet where I enable the email:
import smtp
import time
pretty_localtime = time.asctime()
smtpserver = smtplib.SMTP("smtp.office365.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
#
try:
smtpserver.login(office365_user,office365_pwd)
except:
msg = ("%s --- FATAL ERROR: Couldn't log into SMTP server.")%pretty_localtime
log_file.write("%s \n"%msg)
print msg
sys.exit()
Has anyone worked through this problem?
***edit** Python error added:
Traceback (most recent call last):
File "piu_billing.py", line 739, in <module>
log_file.write(email_msg)
File "piu_billing.py", line 102, in send_mail
smtpserver.sendmail(office365_user,to,themsg)
File "C:\Python27\lib\smtplib.py", line 719, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 471, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 334, in putcmd
self.send(str)
File "C:\Python27\lib\smtplib.py", line 324, in send
raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected