I would like to send an email directly from a script to a Gmail email account, by connecting directly to smtp.gmail.com
.
However, I would prefer not to have the gmail password in the script. From what I have read, it appears that Gmail requires authentication before it will deliver any mail, including to its own users.
My question is, how is mail coming from another SMTP server ever delivered, since that SMTP server will not have Gmail credentials. Does Gmail only require authentication for "anonymous" senders, and since my script is running on a personal computer, it is subject to higher security? Here is the python script I am running:
import smtplib
import email
msg = email.message.Message()
msg["From"] = "user@gmail.com"
msg["To"] = "user@gmail.com"
msg["Subject"] = "Test message"
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.ehlo_or_helo_if_needed()
try:
failed = server.sendmail("user@gmail.com","user@gmail.com", msg.as_string())
server.close()
except Exception as e:
print(e)
When I run this script, the output is:
(530, b'5.5.1 Authentication Required. Learn more at
5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 fw5sm21125889wib.0', 'user@gmail.com')
My question is, how do external SMTP servers avoid this problem? And is whatever they do replicable in a local script, or does it require correct reverse DNS records, SPF records, etc.?
Thats a real good question, and i am replying inline.
I would like to send an email directly from a script to a Gmail email account, by connecting directly to smtp.gmail.com.
dig mx gmail.com +short
output:
However, I would prefer not to have the gmail password in the script. From what I have read, it appears that Gmail requires authentication before it will deliver any mail, including to its own users.
My question is, how is mail coming from another SMTP server ever delivered, since that SMTP server will not have Gmail credentials. Does Gmail only require authentication for "anonymous" senders, and since my script is running on a personal computer, it is subject to higher security? Here is the python script I am running:
The following python script sends email to a gmail account without authentication.
Or try the following Telnet snippet
You can't use gmail smtp-server without authentication. It's a google policy. You need to enter your account password. But there is another way. You can use GAE (Google App Engine) with Gmail API. In this way you can send messages directly.
You can use some external SMTP servers without authentication (or a local SMTP), but the sent message will be caught by Google's spam filter because the msg["From"] is @google.com, while the actual SMTP is not smtp.gmail.com.
Those SMTP servers also must have correct reverse zone in ISP's DNS and otherwise this smtp will be blocked by google.