DEV Community

Yusuf Turhan Papurcu for Golang

Posted on

📧 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 (

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

func send(body string) {
    from := ""
    pass := "*****"
    to := ""

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

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

    if err != nil {
        log.Printf("smtp error: %s", err)
    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.

Top comments (0)