35

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

remio
  • 1,242
  • 2
  • 15
  • 36
janak
  • 353
  • 1
  • 3
  • 4

3 Answers3

58

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.

First of all smtp.gmail.com is not a mailserver which accepts mail (from other mailservers), but rather allow Gmail users to login and hence send or check email. If we want to find out the Gmail mailservers that accepts mails from other mailservers. We can run the following cmd on the terminal:

dig mx gmail.com +short

output:

10 alt1.gmail-smtp-in.l.google.com.
40 alt4.gmail-smtp-in.l.google.com.
5 gmail-smtp-in.l.google.com.
30 alt3.gmail-smtp-in.l.google.com.
20 alt2.gmail-smtp-in.l.google.com. 

Since gmail-smtp-in.l.google.com. has the lowest value of 5 we use it as the preferred mailserver

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.

Exactly one uses smtp.gmail.com to login and send/check emails to/from their respective accounts, therefore we require user credentials. However we don't need credentials on sending emails to its mail server i.e gmail-smtp-in.l.google.com (Example below)

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:

As I have made myself clear from discussion above we don't need Gmail credentials to connect to Gmail mail servers, however if we connect to Gmail mail servers using personal computers we can get away with sending a few emails, but to send more emails we need to build domain reputation and accountability using DKIM, SPF etc (Thats a whole different spectrum).

The following python script sends email to a gmail account without authentication.

import smtplib

fromaddr = 'sending@example.com'
toaddrs  = ['receiving@gmail.com']
# string inside msg below must have "Subject: <subject line>\n"
# for a subject to be sent, and "To: " for the recipient to be shown in the email
msg = '''To: receiving@gmail.com
    Subject: Subject line here\n
    The body goes here
    .
'''

msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
# The actual mail send
server = smtplib.SMTP('gmail-smtp-in.l.google.com:25')
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit()  

Or try the following Telnet snippet

telnet gmail-smtp-in.l.google.com 25

HELO sendingdomain.com

MAIL FROM:<user@sending.com>

RCPT TO:<playingwithtelnet@gmail.com>

DATA
From: <user@sending.com>
To: <playingwithtelnet@gmail.com>
Subject: Just a test email

The body of the mail goes here.          
.

QUIT
lobi
  • 344
  • 1
  • 4
  • 16
MaK
  • 1,648
  • 1
  • 16
  • 6
  • here "25" represent what? @MaK – Mohideen bin Mohammed Nov 30 '15 at 09:14
  • 1
    25 is the SMTP port, used for communications with mail servers. https://en.m.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol – MaK Dec 01 '15 at 11:48
  • yes @Mak but is it static? or dynamic ? is their any port available? – Mohideen bin Mohammed Dec 08 '15 at 14:30
  • 1
    @MohideenibnMohammed not exactly sure about your question. 25 is the common port for SMTP protocol it is static in that context. – MaK Dec 09 '15 at 17:14
  • Great answer. The OP should mark this as accepted (if he is still active). Thanks for the post! –  Feb 18 '18 at 02:37
  • Most ISPs block connections on 25, so this most likely won't work unless you specifically request the port be opened. – lux Aug 03 '18 at 18:39
  • Is this answer still valid in 2018.? I got this error when trying in Thonny `"STARTTLS extension not supported by server.") smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.` – Hrvoje T Jan 18 '19 at 11:50
  • As of December 2020, this doesn't work. Server returns: 5.7.1 [109.60.92.247] The IP you're using to send mail is not authorized to 5.7.1 send email directly to our servers. Please use the SMTP relay at your 5.7.1 service provider instead. Learn more at 5.7.1 https://support.google.com/mail/?p=NotAuthorizedError p23si193140edw.81 - gsmtp – user3225309 Dec 02 '20 at 15:29
  • It works! The variable "msg" in the python script should add "From: ..." and indent to the beginning of the line – mfmain Mar 10 '21 at 02:38
  • 2
    @user3225309 It does not work if your ip address is listed in spamhaus database: [SpamHaus](https://www.spamhaus.org/pbl/). You can get your ip removed by following the instruction there. I know because I faced the same issue a week back. Got my ip removed and now it's working fine. :) Not sure will work forever but I just wanted to learn few things and it's done. Check this out : https://stackoverflow.com/a/20235665/5482966 – Mathews Mathai Mar 29 '21 at 11:15
  • @MathewsMathai Even if you are right (I haven't checked), you cannot be sure if your (home, company...) address is blacklisted or not. As you said, even if it works now, your cannot be sure it will work tomorrow. Anyways, thanks for the info you have provided. – user3225309 Apr 02 '21 at 20:01
  • @user3225309 It does stop working again after you play around sending a few emails because you get listed again in the SpamHaus DB. What you could do is use some temporary mail service like GuerillaMail or something like mailtrap/mailspons which is designed for testing smtp and mailing services. Or even better, you could use an existing web host with smtp service. I have been playing around on localhost and my ip is obviously not authorized to send mails as such. – Mathews Mathai Apr 02 '21 at 21:05
  • GuerillaMail, interesting idea. – user3225309 Apr 12 '21 at 17:24
  • This no longer works, if you telnet to alt1.gmail-smtp-in.l.google.com with port 25 it just gets stuck on trying with no response. Same thing with port 587 – Almenon Oct 14 '22 at 04:44
12

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.

sevko
  • 1,402
  • 1
  • 18
  • 37
maxsocl
  • 484
  • 3
  • 9
0

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.

SkyFox
  • 1,805
  • 4
  • 22
  • 33