DEV Community

Yusuf Turhan Papurcu for Golang

Posted on

23 1 1 1 1

📧 Sending E-Mail from Gmail Using Golang.

First setup your gmail account like this. You can access here from settings/all settings.

Image description

Open Forwarding and POP/IMAP. Enable IMAP and save changes.

Image description

Then open Google Account/Security. Open Application Passwords. (You need 2-Step Verification)

Image description

Select Post and Other device. I named “Golang Emails”. Then create it.

Image description

We get the application password. Let’s Code it!



package main

import (
    "log"
    "net/smtp"
)

func main() {
    send("hello bro, This just test.")
}

func send(body string) {
    from := "yusufturhanp@gmail.com"
    pass := "*****"
    to := "tryusuf97@gmail.com"

    msg := "From: " + from + "\n" +
        "To: " + to + "\n" +
        "Subject: Hello there\n\n" +
        body

    err := smtp.SendMail("smtp.gmail.com:587",
        smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
        from, []string{to}, []byte(msg))

    if err != nil {
        log.Printf("smtp error: %s", err)
        return
    }
    log.Println("Successfully sended to " + to)
}


Enter fullscreen mode Exit fullscreen mode

You must change from to sender mail, pass to Application Password and “to” to reciver email. Run and look your sended posts.

Image description

We sent the first email. And we set up the google account. You can now send as many emails as you want.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay