I want to use only the SMTP MAIL FROM command and RCPT TO command without sending mail with Python's smtplib

Conclusion


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.

Details

It seems that smtplib.SMTP.mail () and smtplib.SMTP.rcpt () should be used ...

スクリーンショット 2020-01-19 11.01.22.png

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)

See implementation with inspect.getsource ()

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

I want to use only the SMTP MAIL FROM command and RCPT TO command without sending mail with Python's smtplib
Use AWS interpreter with Pycharm
I want to use only the SMTP MAIL FROM command and RCPT TO command without sending mail with Python's smtplib
Use the preview feature with aws-cli
Using cgo with the go command
Hit the top command with htop
I get a Python No module named'encodings' error with the aws command
I want to use the latest gcc without sudo privileges! !!
I want to use only the normalization process of SudachiPy
[Python] I want to use the -h option with argparse
I want to use VS Code and Spyder without anaconda! !! !!
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to connect remotely to another computer, and the nautilus command
I want to get information from fstab at the ssh connection destination and execute a command
I want to use jar from python
I want to send a signal only from the sub thread to the main thread
I tried to learn the angle from sin and cos with chainer
I tried to control the network bandwidth and delay with the tc command
I want to use MATLAB feval with python
Use PIL in Python to extract only the data you want from Exif
I want to use Temporary Directory with Python2
I want to use ceres solver from python
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
I want to use mkl with numpy and scipy under pyenv + poetry environment
I want to use the activation function Mish
[Python] I want to use only index when looping a list with a for statement
I want to extract only pods with the specified label using Label Selector in Client-go
I want to use Linux commands at the command prompt! Use Linux commands at the command prompt instead of Git Bash
I want to handle optimization with python and cplex
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
How to use the grep command and frequent samples
I want to automate ssh using the expect command!
I want to use the R dataset in python
I want to remove Python's Unresolved Import Warning with vsCode
I want to use R functions easily with ipython notebook
I want to automate ssh using the expect command! part2
I want to start a jupyter environment with one command
I want to change the Japanese flag to the Palau flag with Numpy
I want to calculate the allowable downtime from the operating rate
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
I tried to implement the mail sending function in Python
I want to know the features of Python and pip
I want to map the EDINET code and securities number
I want to extract an arbitrary URL from the character string of the html source with python
[Python] What is pip? Explain the command list and how to use it with actual examples
I want to record the execution time and keep a log.
I want to terminate python's multiprocessing Pool with ctrl + c (KeyboardInterrupt)
Repeat with While. Scripts to Tweet and search from the terminal
I want to know the weather with LINE bot feat.Heroku + Python
I want to output the beginning of the next month with Python
[EC2] How to install and download chromedriver from the command line
I want to use an external library with IBM Cloud Functions
I want to use both key and value of Python iterator
I want to acquire and list Japanese stock data without scraping
I tried sending an email from the Sakura server with flask-mail
The strongest way to use MeCab and CaboCha with Google Colab
I want to mess with ALB's default security group from CDK
For the time being, I want to convert files with ffmpeg !!
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I compared while reading the documentation to use Jinja2 with Django
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Let's use the NAOqi OS VM. I want to resolve dependencies with pip even in Pepper development