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.
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
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.
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."
Recommended Posts