GoLang:发送 Mailjet 邮件而不使用 Mailjet 库

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

GoLang: Send Mailjet email without Mailjet library

问题

我正在尝试使用我的 Mailjet 凭据从我的 Golang 应用程序发送电子邮件,但我想以正常的 Golang 方式来实现(是的,我知道他们的库是非常推荐的)。

我已经使用 Mailjet 库成功地发送电子邮件,但是我的老板提出了一个非常好的观点,即我们可能不会永远使用 Mailjet。如果我们切换到其他的电子邮件解决方案,我们不想重新编写所有的电子邮件代码,我们只想更改主机名和凭据。

我的打印机使用相同的主机名和凭据通过 Mailjet 发送电子邮件,但是我的 Golang 应用程序却不行!

我的代码是从golang smtp 库的 SendEmail 示例中采用的。

这是我的代码(当然没有包含我的凭据):

import (
    "bytes"
	"fmt"
    "net/smtp"
)
func SendTestEmail() (bool, error) {
    fmt.Println("Send Test Email: Enter")
    success := false
    hostname := "in-v3.mailjet.com"
	auth := smtp.PlainAuth("", username, password, hostname)
	to := []string{"me@example.com"}
	msg := []byte("To: me@example.com\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
    fmt.Println("Send Test Email: Sending Email")
	err := smtp.SendMail(hostname+":587", auth, "sender@example.com", to, msg)
	if err == nil {
        fmt.Println("Send Test Email: Email successfully sent!")
        success = true
    } else {
        fmt.Println("Send Test Email: Email failed to send", err)
    }
    fmt.Println("Send Test Email: Exit")
    return success, err
}

请注意,我使用的是端口 587。我不知道我的打印机是使用 587 还是 25,但它可以工作。使用端口 25 时我也无法工作。

真正奇怪的是 smtp.SendEmail 没有返回任何错误,但我仍然没有收到任何电子邮件(是的,我检查了我的垃圾邮件文件夹)!

另外,当我登录 Mailjet 时,我没有看到任何电子邮件被发送。但当我从打印机发送邮件时,我确实看到有一封邮件被发送了。

那么,我的电子邮件在哪里呢?

非常感谢您的帮助!谢谢!

英文:

I am trying to send emails from my golang application using my Mailjet credentials, but I am trying to do it the normal golang way (yes, I know that their library is highly encouraged).

I have the emails working fine using the Mailjet library, but my boss made a really good point that we might not stay with Mailjet forever. If we switch to a different email solution, we don't want to have to rewrite all of our email code, we just want to change our hostname and credentials.

My printer sends emails just find through Mailjet using the same hostname and credentials, but for some reason my golang app won't!

My code was adopted from the golang smtp library SendEmail example.

Here it is (without my credentials, of course):

import (
    "bytes"
	"fmt"
    "net/smtp"
)
func SendTestEmail() (bool, error) {
    fmt.Println("Send Test Email: Enter")
    success := false
    hostname := "in-v3.mailjet.com"
	auth := smtp.PlainAuth("", username, password, hostname)
	to := []string{"me@example.com"}
	msg := []byte("To: me@example.com\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
    fmt.Println("Send Test Email: Sending Email")
	err := smtp.SendMail(hostname+":587", auth, "sender@example.com", to, msg)
	if err == nil {
        fmt.Println("Send Test Email: Email successfully sent!")
        success = true
    } else {
        fmt.Println("Send Test Email: Email failed to send", err)
    }
    fmt.Println("Send Test Email: Exit")
    return success, err
}

Note that I am using port 587. I do not know if my printer is using 587 or 25, but it's working. I don't work when using port 25 either.

What is really weird is that smtp.SendEmail isn't returning any errors, but I still do not get any emails (yes, I am checking my junk folder)!

Also, when I log into Mailjet, I don't see that any emails were sent. I do see that an email was sent when I send something from the printer.

So, where is my email?!

Any help is greatly appreciated. Thanks!

答案1

得分: 6

首先,感谢您选择Mailjet作为您的电子邮件服务提供商!我在Mailjet负责API产品和开发者关系。

关于发送邮件,您选择使用SMTP是正确的。它是标准的、广泛支持的,并且易于切换(尽管我希望我们不会到达那个地步!)。当涉及到使用我们的API来处理业务流程时,我们的Go库会非常方便。

我对您的代码有几个问题/反馈:

  • 我猜想您在代码中使用的"sender@example.com"发件地址不是您在实际代码中使用的地址?无论如何,在使用之前,这个电子邮件地址必须在Mailjet一侧进行验证。请参阅我们的专用指南
  • 看起来您试图在消息中设置一些SMTP头,比如主题,但实际上应该单独处理。

以下是我使用SMTP的工作代码:

package main

import (
  "log"
  "net/smtp"
  "fmt"
)

func main() {
    auth := smtp.PlainAuth(
        "",
        "MAILJET_API_KEY",
        "MAILJET_API_SECRET",
        "in-v3.mailjet.com",
    )

    email := "foobar@test.com"

    header := make(map[string]string)
    header["From"] = email
    header["To"] = email
    header["Subject"] = "Hello Mailjet World!"
    header["X-Mailjet-Campaign"] = "test"

    message := ""
    for k, v := range header {
        message += fmt.Sprintf("%s: %s\r\n", k, v)
    }
    message += "\r\nHi! Thanks for using Mailjet."

    err := smtp.SendMail(
        "in-v3.mailjet.com:587",
        auth,
        email,
        []string{email},
        []byte(message),
    )
    if err != nil {
      log.Printf("Error: %s", err)
    } else {
      log.Printf("Mail sent!")
    }
}

希望对您有所帮助!使用Mailjet进行hAPI发送。

英文:

First of all, thanks for choosing Mailjet as your email service provider! I'm leading the API Product and Developers Relations at Mailjet.

When it comes to send, you're right with SMTP. It's standard, widely supported and easy to switch (even if I don't hope we'll get there!). Our Go library will become handy when it comes to deal with our API to manage business processes.

I have several questions / feedback looking at your code:

  • I guess the "sender@example.com" from address used is not the one you use in your real code? Anyway, this email must have been validated on Mailjet side beforehands. See our dedicated guide
  • Seems you try to set some SMTP headers like Subject within the message, when it should be handled separately

Here's a working code I'm using to work with SMTP:

package main

import (
  "log"
  "net/smtp"
  "fmt"
)

func main() {
    auth := smtp.PlainAuth(
        "",
        "MAILJET_API_KEY",
        "MAILJET_API_SECRET",
        "in-v3.mailjet.com",
    )

    email := "foobar@test.com"

    header := make(map[string]string)
    header["From"] = email
    header["To"] = email
    header["Subject"] = "Hello Mailjet World!"
    header["X-Mailjet-Campaign"] = "test"

    message := ""
    for k, v := range header {
        message += fmt.Sprintf("%s: %s\r\n", k, v)
    }
    message += "\r\nHi! Thanks for using Mailjet."

    err := smtp.SendMail(
        "in-v3.mailjet.com:587",
        auth,
        email,
        []string{email},
        []byte(message),
    )
    if err != nil {
      log.Printf("Error: %s", err)
    } else {
      log.Printf("Mail sent!")
    }
}

Hope it helps! hAPI sending with Mailjet

huangapple
  • 本文由 发表于 2017年3月25日 03:38:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/43007840.html
匿名

发表评论

匿名网友

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

确定