使用curl发送电子邮件

NSN*_*lan 38 linux email gmail command-line curl

如何使用curl命令行程序从Gmail帐户发送电子邮件?

我尝试过以下方法:

curl -n --ssl-reqd --mail-from "<sender@gmail.com>" --mail-rcpt "<receiver@server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt
Run Code Online (Sandbox Code Playgroud)

但是,当file.txt是电子邮件的内容时,当我运行此命令时,我收到以下错误:

curl: (67) Access denied: 530
Run Code Online (Sandbox Code Playgroud)

是否可以从个人服务器托管的帐户发送电子邮件,仍然使用curl?这会使身份验证过程更容易吗?

bam*_*bam 75

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
  --mail-from 'username@gmail.com' --mail-rcpt 'john@example.com' \
  --upload-file mail.txt --user 'username@gmail.com:password' --insecure
Run Code Online (Sandbox Code Playgroud)

mail.txt文件内容:

From: "User Name" <username@gmail.com>
To: "John Smith" <john@example.com>
Subject: This is a test

Hi John,
I’m sending this mail with curl thru my gmail account.
Bye!
Run Code Online (Sandbox Code Playgroud)

附加信息:

  1. 我正在使用curl版本7.21.6和SSL支持.

  2. --insecure交换机允许curl执行"不安全"的SSL连接和传输.有关详细信息,请参阅此在线资源此在线资源.

  3. 通过命令行参数传递帐户凭据被认为是一种糟糕的安全做法.以上示例仅用于演示目的.

  4. 您必须为不太安全的应用启用访问权限.

  • 谢啦。那完全成功了。任何关于更安全地执行此操作的建议。我想让我的服务器上的 cron 作业运行一个脚本,该脚本将在特定时间发送电子邮件。我认为 curl 命令是最简单的,但不是最安全的。我对 smtp 服务器等不是很熟悉,所以任何建议都将不胜感激。 (2认同)
  • 哦......我明白了......你可以把凭证放在.netrc中 (2认同)
  • @louigi600,我不会使用 .netrc 文件。但是,您可以使用`--netrc-file` 参数,然后使用bash 子shell 重定向。例如`curl --netrc-file &lt;(printf '%s' "${MY_CREDENTIALS}"` ... `bash` 将创建一个文件描述符来提供凭证数据,并且(我认为)数据不会甚至触摸磁盘。 (2认同)

sol*_*urn 6

如果您想以抄送或密送方式发送邮件:

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
  --mail-from 'username@gmail.com' --mail-rcpt 'john@example.com' \
  --mail-rcpt 'mary@gmail.com' --mail-rcpt 'eli@example.com' \
  --upload-file mail.txt --user 'username@gmail.com:password' --insecure
Run Code Online (Sandbox Code Playgroud)
From: "User Name" <username@gmail.com>
To: "John Smith" <john@example.com>
Cc: "Mary Smith" <mary@example.com>
Subject: This is a test

a BCC recipient eli is not specified in the data, just in the RCPT list.

Run Code Online (Sandbox Code Playgroud)