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

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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)