Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer

I need an SMTP server to check the implementation of Rails in ActionMailer, but it is difficult to set up a server, so I set up a dummy SMTP server in Python.

Set up a dummy SMTP server

Reference: How to set up a simple SMTP server that can be tested locally in Python --Qiita

Start as follows with reference to the above page

$ python -m smtpd -n -c DebuggingServer localhost:8025

Check the operation of the SMTP server with telnet

For the time being, connect to the local SMTP server with telnet and try talking to the SMTP server.

Reference: Send email via telnet

For the host to connect to, specify the port number of the server you just started as the localhost port number.

$ telnet localhost 8025
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 localhost.localdomain Python SMTP proxy version 0.2

Enter the HELO command and enter (the ">" at the beginning of the line represents the input line and is not actually entered)

>HELO localhost
250 localhost.localdomain

Enter the sender (from :)

>MAIL FROM: [email protected]
250 Ok

Enter the destination (to :)

>RCPT TO: [email protected]
250 Ok

Enter the DATA command and enter the email body. Entering only the "." Line completes the text entry.

>DATA
354 End data with <CR><LF>.<CR><LF>
>Hello world.
>.
250 Ok

Exit the SMTP server with the QUIT command.

>QUIT
221 Bye
Connection closed by foreign host.

Communication is OK when the log of the transmission contents is displayed on the screen of the terminal that started the SMTP server by executing the above procedure.

Try sending an email from the Rails side

Describe the ActionMailer settings in config / application.rb as shown below

config/application.rb


config.action_mailer.smtp_settings = {
   address: "localhost",
   port: 8025,
   domain: "localhost"
}

Create app / mailer / test_mailer.rb and define the test mail sending method as follows

app/mailer/test_mailer.rb


class TestMailer < ActionMailer::Mailer
  default from: "[email protected]",
            to: "[email protected]"
  
  def test
    mail(subject: "test") do |format|
      format.text { render text: "This is test mail." }
    end
  end
end

When the above preparations are complete, launch the rails console and send a test email as shown below.

> TestMailer.test.deliver_now

As a result of the above input, check the terminal output of the SMTP server and read "This is test mail."

reference

Recommended Posts

Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Set up a test SMTP server in Python.
Set up a simple SMTP server in Python
Send mail with mailx to a dummy SMTP server set up with python.
Set up a simple HTTPS server in Python 3
How to set up a simple SMTP server that can be tested locally in Python
A Python script that allows you to check the status of the server from your browser
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Check the behavior of destructor in Python
Check the in-memory bytes of a floating point number float in Python
Set up a local web server in 30 seconds using python 3's http.server
I made a program to check the size of a file in Python
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Create a Python image in Django without a dummy image file and test the image upload
Get the caller of a function in Python
Make a copy of the list in Python
Set up a UDP server in C language
Create a Django project and application in a Python virtual environment and start the server
Output in the form of a python array
A discussion of the strengths and weaknesses of Python
[Python3] Take a screenshot of a web page on the server and crop it further
Get the value of a specific key in a list from the dictionary type in the list with Python
Build API server for checking the operation of front implementation with python3 and Flask
The result of making a map album of Italy honeymoon in Python and sharing it
[Python] Change the text color and background color of a specific keyword in print output
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
Set up an FTP server that can be created and destroyed immediately (in Python)
How to check in Python if one of the elements of a list is in another list
[Python] Representing the number of complaints from life insurance companies in a bar graph
Find a guideline for the number of processes / threads to set in the application server
Check the processing time and the number of calls for each process in python (cProfile)
Check if the string is a number in python
The story of launching a Minecraft server from Discord
Python points from the perspective of a C programmer
Set up a free server on AWS in 30 minutes
A reminder about the implementation of recommendations in Python
Check the asymptotic nature of the probability distribution in Python
[Vagrant] Set up a simple API server with python
A server that returns the number of people in front of the camera with bottle.py and OpenCV
When it is troublesome to set up an SMTP server locally when sending mail with Python.
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
A library that monitors the life and death of other machines by pinging from Python
Check the argument type annotation when executing a function in Python and make an error
Get the value of a specific key up to the specified index in the dictionary list in Python
How to quickly count the frequency of appearance of characters from a character string in Python?
Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python
Let's statically check and format the code of E2E automatic test written in Python [VS Code]
Find out the apparent width of a string in python
Different from the import type of python. from A import B meaning
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
Build a Python environment and transfer data to the server
Python: Create a dictionary from a list of keys and values
[Tips] Problems and solutions in the development of python + kivy
What beginners learned from the basics of variables in python
A set of script files that do wordcloud in Python3
Find the eigenvalues of a real symmetric matrix in Python
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
Create an instance of a predefined class from a string in Python