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

@@ -59,22 +59,31 @@ class UsersController < ApplicationController
end
def create
@user = authorize User.new(last_ip_addr: CurrentUser.ip_addr, **permitted_attributes(User))
@user = authorize User.new(
last_ip_addr: CurrentUser.ip_addr,
name: params[:user][:name],
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation]
)
if !Danbooru.config.enable_recaptcha? || verify_recaptcha(model: @user)
@user.save
if @user.errors.empty?
session[:user_id] = @user.id
UserMailer.welcome_user(@user).deliver_later
else
flash[:notice] = "Sign up failed: #{@user.errors.full_messages.join("; ")}"
end
set_current_user
respond_with(@user)
else
flash[:notice] = "Sign up failed"
redirect_to new_user_path
if params[:user][:email].present?
@user.email_address = EmailAddress.new(address: params[:user][:email])
end
if Danbooru.config.enable_recaptcha? && !verify_recaptcha(model: @user)
flash[:notice] = "Sign up failed"
elsif @user.email_address&.invalid?(:deliverable)
flash[:notice] = "Sign up failed: email address is invalid or doesn't exist"
@user.errors[:base] << @user.email_address.errors.full_messages.join("; ")
elsif !@user.save
flash[:notice] = "Sign up failed: #{@user.errors.full_messages.join("; ")}"
else
session[:user_id] = @user.id
UserMailer.welcome_user(@user).deliver_later
set_current_user
end
respond_with(@user)
end
def update