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

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?

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).

How to get the SMTP server response code in Django?

Goal:
Hi! How can I get the SMTP response codes through Django, similar to return values given from the smtplib module??? I want to use Django's api instead of having to use that, and I see nothing in Django's email docs.
I know send_mail(), send_mass_mail() , and EmailMessage.send_messages(...) all have return values that show the number of successfully delivered messages... But I want the response codes for all of them. The point is to get proof that an email is sent, bad email address, etc, like smtp response codes show.
And it's looking to me like I'd have to make a custom email backend for this, and subclass from BaseEmailBackend... It's using smtplib anyway.
Example code for smtplib's response code
import smtplib
obj = smtplib.SMTP('smtp.gmail.com', 587)
response = obj.ehlo()
print(response)
# Obviously this doesn't send a message yet, but I want response[0]
(250, b'smtp.gmail.com at your service, [111.222.33.44]\nSIZE 31234577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
If you just want to check if mail is sent you can use return value of send_mail() function
The return value will be the number of successfully delivered messages
(which can be 0 or 1 since it can only send one message).
Django does catch smtplib errors and returns 0 if any error occurred or returns exception if fail_silently is false ( default true)
from django.core.mail.backends.smtp.EmailBackend source _send():
try:
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
except smtplib.SMTPException:
if not self.fail_silently:
if not self.fail_silently:
raise
return False
return True
same thing on open()

python mails being sent out have a blank TO field

I have a python script that sends out emails. The emails are being sent out with no issues.
However, when I get the mails and open them up, their "to:" field do not show the recipients. It's just blank. I want to show all the recipients. And since I'm not using any "bcc" configuration in my script, I'm befuddled as to how this could be.
Here's the code I'm using:
#!/usr/bin/env python
you = [ "MyEmail1#MyEmail.com", "MyEmail2#MyEmail.com" ]
for eachrecord in fformatErrMessage:
# Preparing all variables #
msg = MIMEMultipart()
msg['Subject'] = subjectMsg
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
I have tried replacing the you with:
you = "MyEmail1#MyEmail.com"
thinking maybe the list is the problem - but that did not work.
When sending emails, servers do not look at the To:-Field. Instead, email servers have a separate way of transmitting recipient addresses, sometimes called the "envelope", which they use to route and deliver mail. The client never receives that envelope, it instead uses the To:-field in the message headers.
The sendmail() call simply sets the addresses of the SMTP envelope. If you want the To:-field to show up in the mail itself, you must set the appropriate header:
msg['To'] = "foo#bar.com"
This is, btw, how BCC works: The addresses on the envelope is simply not repeated in the message headers.

sending emails in python weird behaviour

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.

How do I get the response back from server after an email sending?

I want to get response from server with server status (eg. 200, 500) that says if message was delivered. How to do that?
>> s = smtplib.SMTP('...')
>> resp = s.sendmail('me#me.com', 'exist#email.com', 'message')
>> print resp
{}
>> resp = s.sendmail('me#me.com', 'does-not-exist-email#email.com', 'message')
>> print resp
{}
>> resp = s.sendmail('me#me.com', 'does-not-exist-domain#email000.com', 'message')
>> print resp
{}
o_O
Thanks.
You can't. SMTP does not support that behavior.
This is normal behavior of sendmail in case of successful sending. You can check this both in doc and smtp lib source. Empty dictionary as a response means that server accepted and sent message to each of recipients.
Regarding the status of delivery. SMPT protocol is simply unable to guaranty at this stage that message will be successfully delivered to the recipient. This means that first server may accept the message and push it forward, but one of the next mail-hop servers may simply drop it (for some reasons, of course, and first server will probably be informed about these reasons, but there is no possibility to inform you about all this stuff).
As they say about emailing - 'It is easy to send message, but it is much harder to get it delivered'.

Categories

Resources