在混合任务中使用Bamboo发送电子邮件

时间:2017-01-22 20:41:18

标签: 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

当我运行此混合任务时,我得到以下日志输出:

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

我该如何解决这个问题? Bamboo在凤凰应用中运行良好。我无法让它在混合任务中运行。

1 个答案:

答案 0 :(得分:6)

发生此错误是因为<html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel='stylesheet' href='css/style2.css'> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src="https://code.jquery.com/jquery-1.10.2.js" ></script> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> </head> <body> <a id='id' href="" title="tooltip2">Desktops</a> <script> $(function () { $("#id").tooltip({ content: "<br><a href=\"item1\">Item1</a><br><a href=\"item2\">Item2</a><br><a href=\"item3\">Item3</a><br>", show: null, position: { my: "left top", at: "left bottom" }, track: false }); }); </script> </body> 未启动。

启动单个应用程序

在您的情况下,使用Application.ensure_all_started/2启动特定的应用程序:

bamboo

启动所有应用程序

在某些情况下,您可能想要启动所有应用程序: Mix.Tasks.App.Start将启动Application.ensure_all_started(:bamboo) 配置中定义的所有应用程序:

mix.exs