从Erlang发送邮件

gpfsuwkq  于 2022-12-08  发布在  Erlang
关注(0)|答案(3)|浏览(176)

我在ubuntu10.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
2q5ifsrm

2q5ifsrm1#

I will recommend using an existing smtp library in erlang. gen_smtp is one that I have used in the past. Sending email is as simple as:
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"}]).

crcmnpdw

crcmnpdw2#

1 You can try use Sendmail - https://en.wikipedia.org/wiki/Sendmail by Erlang, like here - https://github.com/richcarl/sendmail/blob/master/sendmail.erl
2 You can use https://github.com/Vagabond/gen_smtp
3 You can try implemented simple SMTP client use "smtp.gmail" by RFC https://www.rfc-editor.org/rfc/rfc5321 and try create something like:

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

More details - http://www.gar1t.com/presentations/2012-07-16-oscon/index.html#slide44

whlutmcx

whlutmcx3#

看起来“邮件”命令在您的系统中不可用。请参见eidogg. this tutorial了解如何安装它(或者自己谷歌一个)。

相关问题