I'm trying to create a daemon which regularly sends me emails and I'm using the following snippet of code to do so.
import smtplib
from datetime import date
from email.mime.text import MIMEText
def send_email():
SMTP_PORT = 587
SMTP_SERVER = 'smtp.mail.com'
SMTP_USERNAME = 'EMAIL'
SMTP_PASSWORD = 'PASSWORD'
EMAIL_TO = ['email#hotmail.com']
EMAIL_FROM = 'EMAIL'
EMAIL_SUBJECT = 'Test!'
DATE_FORMAT = '%d/%m/%Y'
EMAIL_SPACE = ', '
DATA='Email Content'
msg = MIMEText(DATA)
msg['Subject'] = EMAIL_SUBJECT + ' %s' % (date.today().strftime(DATE_FORMAT))
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.starttls()
mail.login(SMTP_USERNAME, SMTP_PASSWORD)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()
However, after 5 emails, even though I've added the senders email to my contact list on outlook/hotmail, the daemon returns the following exception:
Sending email! 1/1
Traceback (most recent call last):
File "daemon.py", line 91, in <module>
send_email()
File "daemon.py", line 78, in send_email
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'email#hotmail.com': (550, 'Requested action not taken: mailbox unavailable\nFailure sending mail. Try again later')}
Can anyone advise on how to get around this? Does anyone know of an email service that has been tested which will allow more than 5 emails or how I configure the smtplib to not stop sending after 5 emails to the same address?
UPDATE: When I change the receiving email address from outlook to gmail (another one of my emails) - it returns the same exception even though I've sent no emails to the gmail SMTP server.
Related
I have a very simple Python script that I wrote to send out emails automatically. Here is the code for it:
import smtplib
From = "LorenzoTheGabenzo#gmx.com"
To = ["LorenzoTheGabenzo#gmx.com"]
with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login("LorenzoTheGabenzo#gmx.com", Password)
Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}\n\n{Body}"
smtp.sendmail(From, To, Message)
Whenever I run this code I get a very strange error telling me that this sender is an "unauthorized sender". Here is the error in full
File "test.py", line 17, in <module> smtp.sendmail(From, To, Message)
File "C:\Users\James\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 888, in sendmail888, in sendmail raise SMTPDataError(code, resp)smtplib.SMTPDataError: (554, b'Transaction failed\nUnauthorized sender address.')
I've already enabled SMTP access in the GMX settings and I'm unsure about what else to do now to fix this issue.
Note: I know that the variable password has not been defined. This is because I intentionally removed it before posting, it's defined in my original code.
GMX checks a messages header for a match between the "From" entry in the header and the actual sender. You provided a simple string as message, so there is no header, and hence the error by GMX. In order to fix this, you can use a message object from the email package.
import smtplib
from email.mime.text import MIMEText
Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}\n\n{Body}"
msg = MIMEText(Message)
msg['From'] = "LorenzoTheGabenzo#gmx.com"
msg['To'] = ["LorenzoTheGabenzo#gmx.com"]
with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login("LorenzoTheGabenzo#gmx.com", Password)
smtp.sendmail(msg['From'], msg['To'], msg)
I am trying to send an email. The code I am using is found below:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email_user = 'email#gmail.com'
email_password = 'password'
email_send = 'receiver#gmail.com'
subject = 'subject'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body,'plain'))
try:
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', port=587)
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_user,email_send,text)
server.quit()
print('successfully sent the mail')
except:
print("failed to send mail")
I get an error "failed to send mail" with the error message:
SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials t10sm6451859wra.16 - gsmtp')
The error occurs on the server.login() line, I am not able to login.
I checked other post and it says, it has to do with wrong credentials but my credentials are correct. I have check and double checked.
What could be the problem with this and how do I resolve it?
Send an email using python is easy, you may have encountered mistakes in your snippet code above and I am lazy to debug your code. But here might be the code you prefer.
The smtp.gmail.com,465 make your code secure. This snippet of code will send email to recipient through spam but lot number of emails you wish to send, will not be blocked by Gmail, you also can use bot to send email through recipient via this snippet of code.
Send Multiple Recipients
In the to line, just separate recipients by comma
to = [
'<recipient_email_address1>',
'<recipient_email_address2>',
'<recipient_email_address3>']
import smtplib
def sendEmailFunc():
gmail_user = '<your_sender_email_address_here>'
gmail_password = '<credential_password_of_above_email>'
sent_from = gmail_user
to = ['<recipient_email_address>']
subject = '<write_your_email_subject_here>'
body = "<write_your_email_body_here>"
email_text = """
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print ('====Email Sent Successfully====')
except:
print ('Something went wrong...')
NOTE
Please be careful with your indentation.
Hope this helpful for you.
Thank you.
I'm trying to send an email in Python
It's working without problem with gmail with this code :
import smtplib
sender = 'xxx#xxx'
receivers = ['xxx#gmail.com']
message = "hello"
try:
smtpObj = smtplib.SMTP('smtp.gmail.com:587')
smtpObj.starttls()
smtpObj.login('xxxx#gmail.com', 'my_password')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print("okay")
except:
print("notokay")
But when i use it with office 365, the email is send but the message is empty.
It's the same code but with 'smtp.office365.com:587' with my correct login and password.
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('hello')
msg['Subject'] = 'Urgent message'
msg['From'] = 'xxx#xxx'
msg['To'] = 'xxx#gmail.com'
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('xxx#gmail.com', 'my_password')
s.sendmail('xxx#xxx', 'xxx#gmail.com', msg.as_string())
s.quit()
Try the following, it might be because you need to create a MIMEText context for the formatting to be accepted by office365.
I'm trying to send a mail with from cgi web console using email input field but it fails with below error in apache logs
Traceback (most recent call last):
File "/opt/apache-dba/cgi-bin/main.py", line 132, in <module>
mail()
File "/opt/apache-dba/cgi-bin/main.py", line 129, in mail
s.sendmail(me, you, msg.as_string())
File "/usr/lib64/python2.7/smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'xxx#adp.com/': (501, '5.1.3 Bad recipient address syntax')}
But I'm able to run the same code and able to receive the mail from python shell
Below is the code, this looks like when I run the code from cgi it is trying to convert mailid from 'xxx#adp.com' to 'xxx#adp.com/' resulting syntax error.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Emailid = xxx#gmail.com
Link='http://google.com'
def mail():
me = "GETS.INDIA.DDS.TAM#gmail.com"
you = str(Emailid)
msg = MIMEMultipart('alternative')
msg['Subject'] = "Upgrade Status Link"
msg['From'] = me
msg['To'] = you
text = '%s' % str(Link)
part1 = MIMEText(text, 'plain')
msg.attach(part1)
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
mail()
any help will be greatly appreciated, Thank you
It looks like Emailid should be a string. Try surrounding your test email in quotes to make it a string.
Emailid = 'xxx#gmail.com'
I am new to Python. I have a case where I need to send email where the Bcc: list must be populated and To: list must be blank in order to hide the recipients identity.
I gave msg['TO']as '', None, [''], []. None of these worked.
I Googled, but found nothing related to my problem. I kept To: list blank in my code and some email ids in Bcc: list, then ran my code and got the following error:
Traceback (most recent call last):
File "C:\Users\script.py", line 453, in send_notification
smtp.sendmail(send_from, send_to, msg.as_string())
File "C:\Python27\lib\smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'': (555, '5.5.2 Syntax error. hb1sm19555493pbd.36 - gsmtp')}
Following is my code:
msg['From'] = send_from
msg['To'] = ''
msg['Subject'] = subject
msg['BCC'] = COMMASPACE.join(send_to)
I'm pretty sure all emails are required to have a To address. (Edit: Actually, they're not)
Whenever I receive emails that were sent to an anonymized list like what you're describing, the To and From addresses are generally both the same, which is a reasonable workaround. It looks pretty clean to the recipient:
To: some-student-org#my-university.edu
From: some-student-org#my-university.edu
Bcc: me
If you want to bcc using python's smtplib, don't include the bcc header, just include the bcc recipients in the call to smtplib.sendmail. For your specific example:
import smtplib
from email.mime.text import MIMEText
smtp_server = 'localhost'
send_from = 'your#email.com'
send_to = ['one#email.com', 'two#email.com']
msg_subject = 'test'
msg_text = 'hello'
msg = MIMEText(msg_text)
msg['Subject'] = msg_subject
msg['From'] = send_from
smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()