sending emails in python weird behaviour - python

I am working on a piece of code that regularly sends emails from 4 different accounts, 2 of them are gmail accounts and the other 2 are yahoo accounts. When I started writing the code I was able to send all the emails from both the gmail accounts using the following piece of code:
def sendGmail(self, fromaddr, toaddr, username, password,
email_body, email_subject
):
# Build the email
msg = MIMEText(email_body)
msg['Subject'] = email_subject
msg['From'] = fromaddr
msg['To'] = toaddr
try:
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
print "email sent: %s" % fromaddr
except Exception as e:
print "Something went wrong when sending the email %s" % fromaddr
print e
As I said, this piece of code worked perfectly, now that I add the sendYahoomail(), which is a different method, I always get (530, 'Access denied') as an exception for using sendGmail(). I'm pretty sure it's got nothing to do with my Yahoo method, and I can login from the browser with the gmail credentials correctly.
What can possibly be wrong, or just Gmail doesn't want me to send through code ?

I tried your code above with my Gmail account (actually it's a Google Apps account, but that should be identical) and it works fine for me. I even tried an incorrect "From" header and also using the incorrect source address at the SMTP level - both times Gmail allowed me to send the email, but seemed to quietly fix up the header on egress. Still, you might like to double-check your from address matches your username. Also try using the full email address as the username if you're currently just using the part before the #.
I also tried an incorrect password and received error 535, which isn't the same as what you're seeing. Since I have 2-factor authentication enabled I also tried my real password instead of an application-specific one and that still gave a 535 error (but with the message "application-specific password required").
Is it possible that your ISP has set up something that's intercepting SMTP connections to Gmail? Seems unlikely, although my ISP once blocked access to Gmail on port 587 although port 465 still worked. Perhaps you could try using smtplib.SMTP_SSL on port 465 just in case and see if that gives you any more joy.
You could also try sending to addresses at different providers in case Gmail is rejecting the send for that reason (e.g. if the other provider has been got on to a spam blacklist, for example). Also, if it's possible your email looks like spam, try you code with a message subject and body that's close to an authentic email and see if that helps.

You might find this helpful: https://support.google.com/mail/answer/14257
Basically, Google has detected an attempt to sign in from a server that it considers unfamiliar, and has blocked it. The link above lets you attempt to unblock it.
Sometimes it has worked for me, and sometimes not.

Related

How to spoof 'to' and 'from' fields from actual email recipient and sender addresses

For my use case, I would like to manually set the displayed email addresses in the "to" and "from" field headers of the email, separate from the actual email recipient and sender. I am currently using the smtplib library in python and have managed to accomplish the desired effect with the "to" field and was looking to replicate it for the "from" field as well.
What I have so far:
EMAIL_ADDRESS_G = 'ayush.warikoo77#gmail.com'
from email.message import EmailMessage
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS_G, EMAIL_PASSWORD_G)
# What I would like to be displayed in the email
msg = EmailMessage()
msg["Subject"] = "Test"
msg["To"] = 'test#gmail.com' # shows up
msg['From'] = 'test#gmail.com' # does not show up
msg.set_content("Test body")
# Where I would like to be setting the actual email sender and recipient
smtp.send_message(msg, from_addr=EMAIL_ADDRESS_G, to_addrs=EMAIL_ADDRESS_G)
The above code produces the following:
As shown, the "to" field displays the desired set address, while the "from" field displays my actual email instead of "test#gmail.com". I believe it is being set when I call login with the account, but I am unsure if I can override it. Also happy to use another python email library, if it is not possible with smtplib.
Current --> Desired
To: test#gmail.com
From: ayush.warikoo77#gmail.com --> test#gmail.com
Actual Sender: ayush.warikoo77#gmail.com
Actual Reciever: ayush.warikoo77#gmail.com
Note that this would be used for archiving purposes, where a designated email client might actually be sending the emails, however, I would like the email to use the to and from fields of the message it is trying to document. So the desired displayed "from" field is separate from the actual sender.
Authenticated Gmail SMTP prevents you from spoofing the From header, presumably to prevent abuse.
For archiving purposes, using IMAP’s APPEND command will allow you to place whatever you like in your own mailbox (as it doesn’t count as sending email) and may be a better solution. (You will need to use an App Specific Password or OAUTH to login though).

Is it possible to force empty Reply-To header in mail?

When I send mail with header like this message['Reply-To'] = '' (Python), it work fine on localhost. When I click Reply in Outlook at that received mail, To field is empty. When I send the same mail from production via company SMTP server, the mail also contains empty Reply-To header, however If I click Reply in Outlook, the address from that the mail had been received is prefilled in To field.
Is there a bug in company SMTP or why does it work only in localhost?
Thank you.
In Reply-To empty, Outlook would default to the sender address. IMHO that is how it is supposed to work.

550 permanent failure when sending email to SMS number with smtplib

When using the smtplib library to send emails to phone numbers (ex. number#tmomail.net), my email gets blocked with the message 550 permanent failure for one or more recipients.
I can successfully send emails using the smtplib library to normal emails. I have used my script to send emails to my personal email.
I can also send emails to the phone number successfully using the manual gmail client. If I log into google manually, fill out the form, the email sends to the number perfectly fine.
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.email, self.password)
subject = 'test'
body = 'hello world'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(self.email, validNumber#carrier.com, msg)
Can someone tell me how to make the text go through unblocked the same way it does when it emails normal gmails?
I believe this issue is specific to T-Mobile numbers. Refer to the answer here:
https://support.t-mobile.com/thread/116618
"T-Mobile is now blocking traffic where the sending Domain is altered from the true sending address: IE support#customerservice.com, when the domain is actually is support#gmail.com. senders will receive a 550 bounceback error . To correct this issue, Senders will need to revert their sending address to their actual email address."

SMTPAuthenticationError when sending mail using gmail and python [duplicate]

This question already has answers here:
How to send an email with Gmail as provider using Python?
(16 answers)
SMTPAuthenticationError 5.7.14 Please log\n5.7.14 in via your web browser
(10 answers)
Closed 7 years ago.
when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me
gmail_user = "me#gmail.com"
gmail_pwd = "password"
TO = 'friend#gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
error:
server.login(gmail_user, gmail_pwd)
File "/usr/lib/python3.4/smtplib.py", line 639, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14 Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')
Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again.
Just make sure that you have typed your username and password correct
EDIT:
Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
Your code looks correct but sometimes google blocks an IP when you try to send a email from an unusual location. You can try to unblock it by visiting https://accounts.google.com/DisplayUnlockCaptcha from the IP and following the prompts.
Reference: https://support.google.com/accounts/answer/6009563
I have just sent an email with gmail through Python.
Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.
So, you may get a chance with:
server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)
As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.

Python server.sendmail() returns {}. Email not sent

I'm having a file.txt file which has many lines. I want that any one line is chosen at random and emailed. This is looped thrice.
import smtplib,datetime,random
def mail(em):
#print "yo"
fromAddress = "atadmorecouth#gmail.com"
toAddress = "atadmorecouth#gmail.com"
subject = "WOW " + str(datetime.datetime.now())
#print subject
msg = em
server=smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
password = "mypassword"
server.login(fromAddress, password)
server.sendmail(fromAddress, toAddress, msg)
#print server.sendmail(fromAddress, toAddress, msg)
for i in range(0,3):
em=str(random.choice(list(open('file.txt'))))
mail(em)
This code does not work. The login and everything is fine. The only problem I notice is that server.sendmail(fromAddress, toAddress, msg) returns an empty {}
What could be the possible fix to this? Whats the problem in first place?
I have run the mail() function in the terminal and that seems to work fine.
All the files are hosted here
First, as the docs say:
If this method does not raise an exception, it returns a dictionary, with one entry for each recipient that was refused.
So, the fact that you're getting back {} isn't a problem; it means that you had no errors.
Second, what you're sending is not a valid message. A message must contain headers, then a blank line, then the body. And the headers should contain at least From: and To:.
The smtplib library doesn't do this for you; as explained in the example:
Note that the headers to be included with the message must be included in the message as entered; this example doesn’t do any processing of the RFC 822 headers. In particular, the ‘To’ and ‘From’ addresses must be included in the message headers explicitly.
...
Note: In general, you will want to use the email package’s features to construct an email message, which you can then convert to a string and send via sendmail(); see email: Examples.
Or, in the sendmail docs:
Note: The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents. The SMTP does not modify the message headers in any way.
In this case (as in the example), it's simple enough to just craft the message manually:
msg = "From: {}\r\nTo: {}\r\n\r\n{}\r\n".format(fromAddress, toAddress, em)
Finally, you really should call server.quit() at the end of the mail function. While (at least in CPython) the server object will be destroyed as soon as the function ends, unlike many other objects in Python, SMTP objects do not call close when destroyed, much less quit. So, from the server's point of view, you're suddenly dropping connection. While the RFCs suggest that a server should work just fine without a QUIT, and most of them do, they also recommend that the client should always send it and wait for a response, so… why not do it?

Categories

Resources