dtv11049 2017-03-24 19:38
浏览 62
已采纳

GoLang:发送不带Mailjet库的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
" +
        "Subject: discount Gophers!
" +
        "
" +
        "This is the email body.
")
    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条回答 默认 最新

  • 普通网友 2017-03-24 20:29
    关注

    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
    ", k, v)
        }
        message += "
    Hi! 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办