emails: validate that email addresses are deliverable.

Reject email addresses that known to be undeliverable during signup.
Some users signup with invalid email addresses, which causes the welcome
email (which contains the email confirmation link) to bounce. Too many
bounces hurt our ability to send mail.

We check that an email address is undeliverable by checking if the
domain has a mail server and if the server returns an invalid address
error when attempting to send mail. This isn't foolproof since some
servers don't return an error if the address doesn't exist. If the
checks fail we know the address is bad, but if the checks pass that
doesn't guarantee the address is good. However, this is still good
enough to filter out bad addresses for popular providers like Gmail and
Microsoft that do return nonexistent address errors.

The address existence check requires being able to connect to mail
servers over port 25. This may fail if your network blocks port 25,
which many home ISPs and hosting providers do by default.
This commit is contained in:
evazion
2020-03-23 19:06:44 -05:00
parent 27f10d53d6
commit e79910431f
8 changed files with 111 additions and 27 deletions

View File

@@ -114,29 +114,35 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
context "create action" do
should "create a user" do
user_params = { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1" }
post users_path, params: { user: user_params }
post users_path, params: { user: { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1" }}
assert_redirected_to User.last
assert_equal("xxx", User.last.name)
assert_equal(nil, User.last.email_address)
assert_no_emails
end
should "create a user with a valid email" do
user_params = { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1", email_address_attributes: { address: "test@gmail.com" } }
post users_path, params: { user: user_params }
post users_path, params: { user: { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1", email: "webmaster@danbooru.donmai.us" }}
assert_redirected_to User.last
assert_equal("xxx", User.last.name)
assert_equal("test@gmail.com", User.last.email_address.address)
assert_equal("webmaster@danbooru.donmai.us", User.last.email_address.address)
assert_enqueued_email_with UserMailer, :welcome_user, args: [User.last]
end
should "not create a user with an invalid email" do
user_params = { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1", email_address_attributes: { address: "test" } }
assert_no_difference(["User.count", "EmailAddress.count"]) do
post users_path, params: { user: { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1", email: "test" }}
assert_no_difference("User.count") do
post users_path, params: { user: user_params }
assert_response :success
assert_no_emails
end
end
should "not create a user with an undeliverable email address" do
assert_no_difference(["User.count", "EmailAddress.count"]) do
post users_path, params: { user: { name: "xxx", password: "xxxxx1", password_confirmation: "xxxxx1", email: "nobody@nothing.donmai.us" } }
assert_response :success
assert_no_emails