67

如何使用 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

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

curl: (67) Access denied: 530

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

4

4 回答 4

123
curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user 'username@gmail.com:password' \
  --mail-from 'username@gmail.com' \
  --mail-rcpt 'john@example.com' \
  --upload-file mail.txt

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!

附加信息:

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

  2. 您不需要使用该--insecure开关,它会阻止curl执行 SSL 连接验证。有关详细信息,请参阅此在线资源

  3. 通过命令行参数传递帐户凭据被认为是一种不好的安全做法。使用--netrc-file. 请参阅文档

  4. 您必须为不太安全的应用程序或较新的应用程序密码打开访问权限

于 2013-04-17T20:35:23.200 回答
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
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.

于 2020-01-02T09:02:14.787 回答
2

像这样创建一个简单的email.conf文件

Username:   hi@example.com
Password:   OKbNGRcjiV
POP/IMAP Server:    mail.example.com

并且简单地运行sendmail.sh,就像这样在使其可执行后( sudo chmod +x sendmail.sh)

./sendmail.sh

代码

#!/bin/bash

ARGS=$(xargs echo  $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' email.conf) < /dev/null)
set -- $ARGS "$@";  

declare -A email;
email['user']=$1
email['pass']=$2
email['smtp']=$3
email['port']='587';
email['rcpt']='your-email-address@gmail.com';


email_content='From: "The title" <'"${email['user']}"'>
To: "Gmail" <'"${email['rcpt']}"'>
Subject: from '"${email['user']}"' to Gmail
Date: '"$(date)"'

Hi Gmail,
'"${email['user']}"' is sending email to you and it should work.
Regards
';


echo "$email_content" | curl -s \
    --url "smtp://${email['smtp']}:${email['port']}" \
    --user "${email['user']}:${email['pass']}" \
    --mail-from "${email['user']}" \
    --mail-rcpt "${email['rcpt']}" \
    --upload-file - # email.txt


if [[ $? == 0 ]]; then
    echo;
    echo 'okay';
else
    echo "curl error code $?";
    man curl | grep "^ \+$? \+"
fi

更多的

于 2020-10-31T12:05:33.917 回答
0

请注意,mail.txt 的形式似乎很重要 / CRLF 用于 win,LF 用于 Linux,特殊字符等。

最后在挣扎了 2 个小时之后,它对我来说适用于 GMX(他们告诉他们的 SMPT 端口是 587 - 并且进一步以小写字母提示:“465 也可以与 SSL 一起使用”):

在 Linux 下(安装了 curl.tcz 的 Raspberry 3B+ 上的 TinyCore Linux):

curl --ssl-reqd --url 'smtps://mail.gmx.net:465' --user 'mymail@gmx.at:mymailPassword' --mail-from 'mymail@gmx.at' --mail-rcpt 'mymail@gmx.at' --upload-file mail.txt

在窗户下:

curl --ssl-reqd --url "smtps://mail.gmx.net:465" --user "mymail@gmx.at:mymailPassword" --mail-from "mymail@gmx.at" --mail-rcpt "mymail@gmx.at" --upload-file mail_win.txt

使用mail.txt:

From: "User Name" <mymail@gmx.at>
To: "John Smith" <mymail@gmx.at>
Subject: This is a test

Hi John,
Im sending this mail with curl thru my gmx account.
Bye!
于 2022-02-04T10:03:18.230 回答