sudoman
(Andrew E)
Setembro 10, 2018, 8:56pm
1
Hello Discourse,
Is there a preexisting code base out there for pushing emails to Discourse’s incoming email API?
Thanks,
Andrew
blake
(Blake Erickson)
Setembro 10, 2018, 11:29pm
2
From the output of rake routes You can send a POST request to
admin/email/handle_mail
Here is the controller method from handle_mail:
# frozen_string_literal: true
class Admin::EmailController < Admin::AdminController
def index
end
def server_settings
data = { delivery_method: delivery_method, settings: delivery_settings }
render_json_dump(data)
end
def test
params.require(:email_address)
begin
message = TestMailer.send_test(params[:email_address])
Email::Sender.new(message, :test_message).send
render json: { sent_test_email_message: I18n.t("admin.email.sent_test") }
rescue => e
render json: { errors: [e.message] }, status: :unprocessable_entity
end
end
def preview_digest
params.require(:last_seen_at)
params.require(:username)
user = User.find_by_username(params[:username])
raise Discourse::InvalidParameters unless user
renderer = Email::Renderer.new(UserNotifications.digest(user, since: params[:last_seen_at]))
render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text)
end
def advanced_test
params.require(:email)
receiver = Email::Receiver.new(params["email"])
text, elided, format = receiver.select_body
render json: success_json.merge!(text: text, elided: elided, format: format)
end
def send_digest
params.require(:last_seen_at)
params.require(:username)
params.require(:email)
user = User.find_by_username(params[:username])
raise Discourse::InvalidParameters unless user
message, skip_reason =
UserNotifications.public_send(
:digest,
user,
since: params[:last_seen_at],
skip_unsubscribe_links: true,
)
if message
message.to = params[:email]
begin
Email::Sender.new(message, :digest).send
render json: success_json
rescue => e
render json: { errors: [e.message] }, status: :unprocessable_entity
end
else
render json: { errors: skip_reason }
end
end
# TODO (martin) Remove this in 3.7.0, this endpoint has been broken for years
# and was used only in the public mail-receiver's fast rejection code,
# which is removed in https://github.com/discourse/mail-receiver/pull/33
def smtp_should_reject
render json: { reject: false }
end
def handle_mail
deprecated_email_param_used = false
if params[:email_encoded].present?
email_raw = Base64.strict_decode64(params[:email_encoded])
elsif params[:email].present?
deprecated_email_param_used = true
email_raw = params[:email]
else
raise ActionController::ParameterMissing.new("email_encoded or email")
end
# If it isn't valid UTF-8, reinterpret as ISO-8859-1 so it can be serialized to JSON.
email_raw = email_raw.dup.force_encoding("UTF-8")
email_raw = email_raw.encode("UTF-8", "ISO-8859-1") if !email_raw.valid_encoding?
Jobs.enqueue(:process_email, mail: email_raw, retry_on_rate_limit: true, source: "handle_mail")
if deprecated_email_param_used
warning =
"warning: the email parameter is deprecated. all POST requests to this route should be sent with a base64 strict encoded email_encoded parameter instead. email has been received and is queued for processing"
Discourse.deprecate(warning, drop_from: "3.3.0")
render plain: warning
else
render plain: "email has been received and is queued for processing"
end
end
private
def delivery_settings
action_mailer_settings.reject { |k, _| k == :password }.map { |k, v| { name: k, value: v } }
end
def delivery_method
ActionMailer::Base.delivery_method
end
def action_mailer_settings
ActionMailer::Base.public_send "#{delivery_method}_settings"
end
end
Para um usuário não técnico, preciso entender as capacidades de e-mail de entrada via API:
O que já fiz:
E-mails de entrada via polling POP3 para o domínio principal (não subdomínio)
E-mail de entrada funcional via módulo de correio postfix, mas isso só funciona para subdomínio — no meu caso, xxx@community.eleoptics.com — o que não podemos usar; precisamos do domínio principal.
Configuração atual:
E-mail de entrada: endereço de e-mail do Google “Grupos” do G Suite encaminhando para contas Gmail gratuitas (para evitar pagar por uma conta G Suite por endereço de e-mail). Polling POP3
E-mail de saída: Encaminhamento de Relay SMTP via GSuite validado pelo IP da minha instalação do Discourse no Google Cloud.
Isso funciona, mas as contas gratuitas de polling POP3 do Gmail são algo que gostaria de eliminar.
Planejamos usar a API de E-mail do Google Cloud Platform para gerenciar o fluxo de entrada e saída de e-mails, idealmente para implantações e serviços em maior escala.
Onde está a documentação que posso enviar aos meus funcionários de TI sobre como configurar o e-mail de entrada diretamente no Discourse?
Se isso já estiver vinculado na postagem acima, obrigado. Caso contrário, minha suposição é que seja necessária uma explicação mais contextualizada.
blake:
Aqui está o método do controlador de handle_mail :
github.com
params.require(:email)
user = User.find_by_username(params[:username])
message, skip_reason = UserNotifications.public_send(:digest, user,
since: params[:last_seen_at]
)
if message
message.to = params[:email]
begin
Email::Sender.new(message, :digest).send
render json: success_json
rescue => e
render json: { errors: [e.message] }, status: 422
end
else
render json: { errors: skip_reason }
end
end
def smtp_should_reject