2

我在 ubuntu 10.10 工作,我使用 Erlang。
我的目标是编写代码以便从 Erlang 发送邮件。

这是我的代码:

-module(mailer).

-compile(export_all).


send(Destination, Subject, Body) ->
    D = string:join(lists:map( fun(Addr) -> binary_to_list(Addr) end, Destination ), " " ),
    S = io_lib:format("~p",[binary_to_list(Subject)]),
    B = io_lib:format("~p",[binary_to_list(Body)]),
    os:cmd("echo "" ++ B ++ "" | mail -s "" ++ S ++ "" " ++ D).

并执行我尝试的发送功能:

Erlang R13B03 (erts-5.7.4) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> mailer:send([<<"testFrom@mail.com">>, <<"testto@yahoo.fr">>], <<"hello">>, <<"Hello guys">>..                        
"/bin/sh: mail: not found\n"

如您所见,我有此错误:

/bin/sh: mail: not found
4

3 回答 3

8

我会推荐在 erlang 中使用现有的 smtp 库。gen_smtp是我过去使用过的。发送电子邮件很简单:

gen_smtp_client:send({"whatever@test.com", ["andrew@hijacked.us"], "Subject: testing\r\nFrom: Andrew Thompson \r\nTo: Some Dude \r\n\r\nThis is the email body"}, [{relay, "smtp.gmail.com"}, {username, "me@gmail.com"}, {password, "mypassword"}]).

于 2013-01-26T12:15:08.873 回答
4

1 您可以尝试使用Sendmail - Erlang 的https://en.wikipedia.org/wiki/Sendmail,就像这里一样 - https://github.com/richcarl/sendmail/blob/master/sendmail.erl

2 你可以使用https://github.com/Vagabond/gen_smtp

3 您可以尝试使用 RFC https://www.rfc-editor.org/rfc/rfc5321实现的简单 SMTP 客户端使用“smtp.gmail”并尝试创建类似:

connect() ->
  {ok, Socket} = ssl:connect("smtp.gmail.com", 465, [{active, false}], 1000),
  recv(Socket),
  send(Socket, "HELO localhost"),
  send(Socket, "AUTH LOGIN"),
  send(Socket, binary_to_list(base64:encode("me@gmail.com"))),
  send(Socket, binary_to_list(base64:encode("letmein"))),
  send(Socket, "MAIL FROM: <me@gmail.com>"),
  send(Socket, "RCPT TO: <you@mail.com>"),
  send(Socket, "DATA"),
  send_no_receive(Socket, "From: <me@gmail.com>"),
  send_no_receive(Socket, "To: <you@mail.com>"),
  send_no_receive(Socket, "Date: Tue, 20 Jun 2012 20:34:43 +0000"),
  send_no_receive(Socket, "Subject: Hi!"),
  send_no_receive(Socket, ""),
  send_no_receive(Socket, "This was sent from Erlang. So simple!"),
  send_no_receive(Socket, ""),
  send(Socket, "."),
  send(Socket, "QUIT"),
  ssl:close(Socket).

更多细节 - http://www.gar1t.com/presentations/2012-07-16-oscon/index.html#slide44

于 2018-06-30T00:18:44.790 回答
2

您的系统中似乎没有“邮件”命令。请参阅本教程,了解如何安装它(或您自己的谷歌)。

于 2013-01-26T11:29:24.687 回答