import smtplib
server = smtplib.SMTP('gmail-smtp-in.l.google.com.', 25) #Destination server,port number
server.ehlo()
server.mail('[email protected]') #Address specified in MAIL FROM
server.rcpt('[email protected]') #Address specified in RCPT TO
~
You can use smtplib.SMTP.mail ()
and smtplib.SMTP.rcpt ()
like this.
The smtplib documentation ↑ states that if you want to use the SMTP MAIL FROM
and RCPT TO
commands, you can use the mail ()
and rcpt ()
methods.
However, the details of the mail ()
and rcpt ()
methods such as what value should be passed to the argument are not written.
[smtplib --- SMTP Protocol Client — Python 3.8.1 documentation https://docs.python.org/ja/3/library/smtplib.html ] (https://docs.python.org/ja/3/library/smtplib.html)
import inspect
import smtplib
mail = inspect.getsource(smtplib.SMTP.mail)
rcpt = inspect.getsource(smtplib.SMTP.rcpt)
Get the source code strings of smtplib.SMTP.mail () and smtplib.SMTP.rcpt () like this.
It seems to be implemented like this ↓ It's kind of funny that the standard library ignores PEP8. (x.lower () =='smtputf8' part of mail (). E225: missing whitespace around operator)
python:smtplib.SMTP.mail()
def mail(self, sender, options=()):
"""SMTP 'mail' command -- begins mail xfer session.
This method may raise the following exceptions:
SMTPNotSupportedError The options parameter includes 'SMTPUTF8'
but the SMTPUTF8 extension is not supported by
the server.
"""
optionlist = ''
if options and self.does_esmtp:
if any(x.lower()=='smtputf8' for x in options):
if self.has_extn('smtputf8'):
self.command_encoding = 'utf-8'
else:
raise SMTPNotSupportedError(
'SMTPUTF8 not supported by server')
optionlist = ' ' + ' '.join(options)
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
return self.getreply()
python:smtplib.SMTP.rcpt()
def rcpt(self, recip, options=()):
"""SMTP 'rcpt' command -- indicates 1 recipient for this mail."""
optionlist = ''
if options and self.does_esmtp:
optionlist = ' ' + ' '.join(options)
self.putcmd("rcpt", "TO:%s%s" % (quoteaddr(recip), optionlist))
return self.getreply()
So, it seems that you can simply pass the email address string you want to specify to each of the arguments of mail ()
and rcpt ()
(return to the beginning).
Recommended Posts