win*_*yer 3 elixir phoenix-framework
我想从Bamboo发送一个混合任务的电子邮件.这是任务的代码:
defmodule Mix.Tasks.SendEmails.Reminder do
use Mix.Task
import Mix.Ecto
import Ecto.Query
def run(_args) do
Foobar.welcome_email |> MyApp.Mailer.deliver_now
end
end
defmodule Foobar do
import Bamboo.Email
def welcome_email do
new_email
|> to("foo@example.com")
|> from("me@example.com")
|> subject("Welcome!!!")
|> html_body("<strong>Welcome</strong>")
|> text_body("welcome")
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行这个混合任务时,我得到以下日志输出:
21:20:26.829 [debug] Sending email with Bamboo.LocalAdapter:
%Bamboo.Email{assigns: %{}, bcc: [], cc: [], from: {nil,
"me@example.com"}, headers: %{}, html_body: "<strong>Welcome</strong>",
private: %{}, subject: "Welcome!!!", text_body: "welcome", to:
[nil: "foo@example.com"]}
** (exit) exited in: GenServer.call(Bamboo.SentEmail, {:update,
#Function<2.18788267/1 in Bamboo.SentEmail.push/1>}, 5000)
** (EXIT) no process: the process is not alive or there's no
process currently associated with the given name, possibly
because its application isn't started
(elixir) lib/gen_server.ex:729: GenServer.call/3
lib/bamboo/sent_email.ex:109: Bamboo.SentEmail.push/1
lib/bamboo/mailer.ex:121: Bamboo.Mailer.deliver_now/3
lib/mix/tasks/send_emails.reminder.ex:30: anonymous fn/2 in Mix.Tasks.SendEmails.Reminder.run/1
(elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
lib/mix/tasks/send_emails.reminder.ex:20: Mix.Tasks.SendEmails.Reminder.run/1
(mix) lib/mix/task.ex:294: Mix.Task.run_task/3
(mix) lib/mix/cli.ex:58: Mix.CLI.run_task/2
(elixir) lib/code.ex:370: Code.require_file/2
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?Bamboo在凤凰应用中运行良好.我无法让它在混合任务中运行.
发生此错误是因为bamboo未启动.
在您的情况下,使用Application.ensure_all_started/2启动特定的应用程序:
Application.ensure_all_started(:bamboo)
Run Code Online (Sandbox Code Playgroud)
在某些情况下,您可能希望启动所有应用程序:
Mix.Tasks.App.Start将启动mix.exs配置中定义的所有应用程序:
defmodule Mix.Tasks.SendEmails.Reminder do
use Mix.Task
import Mix.Ecto
import Ecto.Query
def run(_args) do
Mix.Tasks.App.Start.run([]) # This will start all apps
Foobar.welcome_email |> MyApp.Mailer.deliver_now
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
499 次 |
| 最近记录: |