Lambda函数(Go语言)无法使用SES SMTP发送电子邮件。

huangapple go评论79阅读模式
英文:

Lambda Function(Go) can't send email with SES SMTP

问题

你好,我已经按照这里的说明尝试了一下,以使我的 Lambda 函数能够通过 SES SMTP 发送电子邮件:https://docs.aws.amazon.com/ses/latest/dg/send-email-set-up-vpc-endpoints.html

在我的 Lambda 函数中,我使用了 net/smtp 包,当我尝试发送电子邮件时,它给出了以下错误:

> Error sending email alert dial tcp: lookup email-smtp.us-east-1.amazonaws.com on ..*.1:53: no such host

以下是给我带来问题的发送电子邮件的代码。请注意,当我在本地运行此 Go 代码时,它可以正常工作。


user := os.Getenv("SMTP_USER")
password := os.Getenv("SMTP_PASSWORD")

to := []string{
	"testemail@gmail.com",
}

addr := "email-smtp.us-east-1.amazonaws.com:587"
host := "email-smtp.us-east-1.amazonaws.com"

msg := []byte("From: test@test.com\r\n" +
	"To: test@test.com\r\n" +
	"Subject: Test mail\r\n\r\n" +
	"Email Body \r\n")

auth := smtp.PlainAuth("", user, password, host)

err = smtp.SendMail(addr, auth, from, to, msg)```

<details>
<summary>英文:</summary>

Hello I&#39;ve followed the instructions here to try and get my lambda function able to send email via SES SMTP https://docs.aws.amazon.com/ses/latest/dg/send-email-set-up-vpc-endpoints.html

In my Lambda Function I use the net/smtp package and when I try to send the email it gives me this error

&gt; Error sending email alert dial tcp: lookup email-smtp.us-east-1.amazonaws.com on *.*.*.1:53: no such host

 
Here is the code for sending the email that is giving me issues. Note when I rung this Go code locally it works fine.
```from := &quot;test@test.com&quot;

	user := os.Getenv(&quot;SMTP_USER&quot;)
	password := os.Getenv(&quot;SMTP_PASSWORD&quot;)

	to := []string{
		&quot;testemail@gmail.com&quot;,
	}

	addr := &quot;email-smtp.us-east-1.amazonaws.com:587&quot;
	host := &quot;email-smtp.us-east-1.amazonaws.com&quot;

	msg := []byte(&quot;From: test@test.com\r\n&quot; +
		&quot;To: test@test.com\r\n&quot; +
		&quot;Subject: Test mail\r\n\r\n&quot; +
		&quot;Email Body \r\n&quot;)

	auth := smtp.PlainAuth(&quot;&quot;, user, password, host)

	err = smtp.SendMail(addr, auth, from, to, msg)```


</details>


# 答案1
**得分**: 0

我终于成功让SMTP工作了。原帖中的所有内容都是正确的,除了安全组部分。为了让它正常工作,我不得不设置我的安全组的入站和出站规则(不确定出站规则是否必要),将我的VPC子网中use1-az1的整个IP地址范围用作源地址/目标地址。

<details>
<summary>英文:</summary>

I was able to finally get the SMTP working. Everything in that document in the OP was correct except the security group part. In order to get it to work, I had to set the inbound and outbound rules of my security group (not entirely sure if outbound is necessary) to use the entire IP address range of the subnet in my VPC for use1-az1 for source/destination respectively.

</details>



huangapple
  • 本文由 发表于 2022年8月27日 06:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/73506858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定