2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-18 17:57:55 -06:00
|
|
|
def process_popmail(popmail)
|
|
|
|
begin
|
|
|
|
mail_string = popmail.pop
|
|
|
|
Email::Receiver.new(mail_string).process
|
|
|
|
rescue StandardError
|
|
|
|
putc "!"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-20 22:37:17 -05:00
|
|
|
desc "use this task to import a mailbox into Discourse"
|
2016-01-18 17:57:55 -06:00
|
|
|
task "emails:import" => :environment do
|
|
|
|
begin
|
|
|
|
unless SiteSetting.email_in
|
|
|
|
puts "ERROR: you should enable the 'email_in' site setting before running this task"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
address = ENV["ADDRESS"].presence || "pop.gmail.com"
|
|
|
|
port = (ENV["PORT"].presence || 995).to_i
|
|
|
|
ssl = (ENV["SSL"].presence || "1") == "1"
|
|
|
|
username = ENV["USERNAME"].presence
|
|
|
|
password = ENV["PASSWORD"].presence
|
|
|
|
|
|
|
|
if username.blank?
|
|
|
|
puts "ERROR: expecting USERNAME=<username> rake emails:import"
|
|
|
|
exit(2)
|
|
|
|
elsif password.blank?
|
|
|
|
puts "ERROR: expecting PASSWORD=<password> rake emails:import"
|
|
|
|
exit(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
RateLimiter.disable
|
|
|
|
|
|
|
|
mails_left = 1
|
|
|
|
pop3 = Net::POP3.new(address, port)
|
2019-09-11 11:43:02 -05:00
|
|
|
pop3.enable_ssl(max_version: OpenSSL::SSL::TLS1_2_VERSION) if ssl
|
2016-01-18 17:57:55 -06:00
|
|
|
|
|
|
|
while mails_left > 0
|
|
|
|
pop3.start(username, password) do |pop|
|
|
|
|
pop.delete_all { |p| process_popmail(p) }
|
|
|
|
mails_left = pop.n_mails
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Done"
|
|
|
|
rescue Net::POPAuthenticationError
|
|
|
|
puts "AUTH EXCEPTION: please make sure your credentials are correct."
|
|
|
|
exit(10)
|
|
|
|
ensure
|
|
|
|
RateLimiter.enable
|
|
|
|
end
|
|
|
|
end
|
2018-05-13 07:15:15 -05:00
|
|
|
|
2018-07-30 23:45:59 -05:00
|
|
|
desc "Check if SMTP connection is successful and send test message"
|
2018-05-13 07:15:15 -05:00
|
|
|
task "emails:test", [:email] => [:environment] do |_, args|
|
|
|
|
email = args[:email]
|
2018-07-30 23:45:59 -05:00
|
|
|
message = "OK"
|
|
|
|
begin
|
2018-07-30 23:50:02 -05:00
|
|
|
smtp = Discourse::Application.config.action_mailer.smtp_settings
|
2018-07-30 23:45:59 -05:00
|
|
|
|
|
|
|
puts <<~TEXT if smtp[:address].match(/smtp\.gmail\.com/)
|
2018-07-30 23:50:02 -05:00
|
|
|
#{smtp}
|
|
|
|
============================== WARNING ==============================
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
Sending mail with Gmail is a violation of their terms of service.
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
Sending with G Suite might work, but it is not recommended. For information see:
|
2021-05-20 22:37:17 -05:00
|
|
|
https://meta.discourse.org/t/discourse-aws-ec2-g-suite-troubleshooting/62931?u=pfaffman
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
========================= CONTINUING TEST ============================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2021-02-10 09:42:21 -06:00
|
|
|
puts "Testing sending to #{email} using #{smtp[:address]}:#{smtp[:port]}, username:#{smtp[:user_name]} with #{smtp[:authentication]} auth."
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2022-01-03 16:30:48 -06:00
|
|
|
# We are not formatting the messages using EmailSettingsExceptionHandler here
|
|
|
|
# because we are doing custom messages in the rake task with more details.
|
|
|
|
EmailSettingsValidator.validate_smtp(
|
|
|
|
host: smtp[:address],
|
|
|
|
port: smtp[:port],
|
|
|
|
domain: smtp[:domain] || "localhost",
|
|
|
|
username: smtp[:user_name],
|
|
|
|
password: smtp[:password],
|
|
|
|
authentication: smtp[:authentication] || "plain",
|
|
|
|
)
|
2018-07-30 23:45:59 -05:00
|
|
|
rescue Exception => e
|
|
|
|
if e.to_s.match(/execution expired/)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
message = <<~TEXT
|
2018-07-30 23:50:02 -05:00
|
|
|
======================================== ERROR ========================================
|
2022-01-03 16:30:48 -06:00
|
|
|
Connection to port #{smtp[:port]} failed.
|
2018-07-30 23:50:02 -05:00
|
|
|
====================================== SOLUTION =======================================
|
|
|
|
The most likely problem is that your server has outgoing SMTP traffic blocked.
|
|
|
|
If you are using a service like Mailgun or Sendgrid, try using port 2525.
|
|
|
|
=======================================================================================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2019-01-16 20:41:10 -06:00
|
|
|
elsif e.to_s.match(/530.*STARTTLS/)
|
2021-05-20 20:43:47 -05:00
|
|
|
# We can't run a preliminary test with STARTTLS, we'll just try sending the test email.
|
2019-01-16 20:41:10 -06:00
|
|
|
message = "OK"
|
2018-07-30 23:45:59 -05:00
|
|
|
elsif e.to_s.match(/535/)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
message = <<~TEXT
|
2018-07-30 23:50:02 -05:00
|
|
|
======================================== ERROR ========================================
|
|
|
|
AUTHENTICATION FAILED
|
|
|
|
|
|
|
|
#{e}
|
|
|
|
|
|
|
|
====================================== SOLUTION =======================================
|
|
|
|
The most likely problem is that your SMTP username and/or Password is incorrect.
|
|
|
|
Check them and try again.
|
|
|
|
=======================================================================================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-07-30 23:45:59 -05:00
|
|
|
elsif e.to_s.match(/Connection refused/)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
message = <<~TEXT
|
2018-07-30 23:50:02 -05:00
|
|
|
======================================== ERROR ========================================
|
|
|
|
CONNECTION REFUSED
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
#{e}
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
====================================== SOLUTION =======================================
|
|
|
|
The most likely problem is that you have chosen the wrong port or a network problem is
|
|
|
|
blocking access from the Docker container.
|
2018-05-13 07:15:15 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
Check the port and your networking configuration.
|
|
|
|
=======================================================================================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-07-30 23:45:59 -05:00
|
|
|
elsif e.to_s.match(/service not known/)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
message = <<~TEXT
|
2018-07-30 23:50:02 -05:00
|
|
|
======================================== ERROR ========================================
|
|
|
|
SMTP SERVER NOT FOUND
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
#{e}
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
====================================== SOLUTION =======================================
|
|
|
|
The most likely problem is that the host name of your SMTP server is incorrect.
|
|
|
|
Check it and try again.
|
|
|
|
=======================================================================================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-07-30 23:45:59 -05:00
|
|
|
else
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
message = <<~TEXT
|
2018-07-30 23:50:02 -05:00
|
|
|
======================================== ERROR ========================================
|
|
|
|
UNEXPECTED ERROR
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
#{e}
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2018-07-30 23:50:02 -05:00
|
|
|
====================================== SOLUTION =======================================
|
|
|
|
This is not a common error. No recommended solution exists!
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2019-01-16 20:41:10 -06:00
|
|
|
Please report the exact error message above to https://meta.discourse.org/
|
|
|
|
(And a solution, if you find one!)
|
2018-07-30 23:50:02 -05:00
|
|
|
=======================================================================================
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-07-30 23:45:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if message == "OK"
|
|
|
|
puts "SMTP server connection successful."
|
|
|
|
else
|
|
|
|
puts message
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
puts "Sending to #{email}. . . "
|
2021-07-09 13:14:18 -05:00
|
|
|
email_log = Email::Sender.new(TestMailer.send_test(email), :test_message).send
|
|
|
|
case email_log
|
|
|
|
when SkippedEmailLog
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
puts <<~TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
Mail was not sent.
|
|
|
|
|
|
|
|
Reason: #{email_log.reason}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
when EmailLog
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
puts <<~TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
Mail accepted by SMTP server.
|
|
|
|
Message-ID: #{email_log.message_id}
|
|
|
|
|
|
|
|
If you do not receive the message, check your SPAM folder
|
|
|
|
or test again using a service like http://www.mail-tester.com/.
|
|
|
|
|
|
|
|
If the message is not delivered it is not a problem with Discourse.
|
|
|
|
Check the SMTP server logs for the above Message ID to see why it
|
|
|
|
failed to deliver the message.
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
when nil
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
puts <<~TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
Mail was not sent.
|
|
|
|
|
|
|
|
Verify the status of the `disable_emails` site setting.
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
else
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
puts <<~TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
SCRIPT BUG: Got back a #{email_log.class}
|
|
|
|
#{email_log.inspect}
|
|
|
|
|
|
|
|
Mail may or may not have been sent. Check the destination mailbox.
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2021-07-09 13:14:18 -05:00
|
|
|
end
|
2021-02-10 09:42:21 -06:00
|
|
|
rescue => error
|
2018-07-30 23:45:59 -05:00
|
|
|
puts "Sending mail failed."
|
2021-02-10 09:42:21 -06:00
|
|
|
puts error.message
|
2021-07-09 13:14:18 -05:00
|
|
|
end
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2021-07-09 13:14:18 -05:00
|
|
|
puts <<~TEXT if SiteSetting.disable_emails != "no"
|
2018-07-30 23:45:59 -05:00
|
|
|
|
2021-07-09 13:14:18 -05:00
|
|
|
### WARNING
|
|
|
|
The `disable_emails` site setting is currently set to #{SiteSetting.disable_emails}.
|
|
|
|
Consider changing it to 'no' before performing any further troubleshooting.
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
TEXT
|
2018-05-13 07:15:15 -05:00
|
|
|
end
|
2023-05-19 03:33:48 -05:00
|
|
|
|
|
|
|
desc "run this to fix users associated to emails mirrored from a mailman mailing list"
|
|
|
|
task "emails:fix_mailman_users" => :environment do
|
|
|
|
if !SiteSetting.enable_staged_users
|
|
|
|
puts "Please enable staged users first"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_user(email, name)
|
|
|
|
user = nil
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
unless user = User.find_by_email(email)
|
|
|
|
username = UserNameSuggester.sanitize_username(name) if name.present?
|
|
|
|
username = UserNameSuggester.suggest(username.presence || email)
|
|
|
|
name = name.presence || User.suggest_name(email)
|
|
|
|
|
|
|
|
begin
|
|
|
|
user = User.create!(email: email, username: username, name: name, staged: true)
|
|
|
|
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
IncomingEmail
|
|
|
|
.includes(:user, :post)
|
|
|
|
.where("raw LIKE '%X-Mailman-Version: %'")
|
|
|
|
.find_each do |ie|
|
|
|
|
next unless ie.post.present?
|
|
|
|
|
|
|
|
mail = Mail.new(ie.raw)
|
|
|
|
email, name = Email::Receiver.extract_email_address_and_name_from_mailman(mail)
|
|
|
|
|
|
|
|
if email.blank? || email == ie.user.email
|
|
|
|
putc "."
|
|
|
|
elsif new_owner = find_or_create_user(email, name)
|
|
|
|
PostOwnerChanger.new(
|
|
|
|
post_ids: [ie.post_id],
|
|
|
|
topic_id: ie.post.topic_id,
|
|
|
|
new_owner: new_owner,
|
|
|
|
acting_user: Discourse.system_user,
|
|
|
|
skip_revision: true,
|
|
|
|
).change_owner!
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "X"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|