从erlang发送邮件

时间:2013-01-26 10:59:52

标签: email ubuntu erlang

我在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).

并执行send函数我尝试:

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\n"

3 个答案:

答案 0 :(得分: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"}]).

答案 1 :(得分:3)

1您可以尝试使用Erlang的 Sendmail -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://tools.ietf.org/html/rfc5321使用“ smtp.gmail”实现简单的SMTP客户端,并尝试创建以下内容:

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

答案 2 :(得分:1)

系统中似乎没有“mail”命令。参见例如this tutorial关于如何安装它(或谷歌一个)。