Last active
October 28, 2020 11:07
-
-
Save rakhid16/cea04f28557102123d3d6f6d20072ae2 to your computer and use it in GitHub Desktop.
send email with golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// BUILT-IN LIBRARIES | |
import ( | |
"log" | |
"net/smtp" | |
) | |
func kirim(body, subject string) { | |
// AKUN GMAIL PENGIRIM - allow less secure apps : ON (google account setting) | |
surel_pengirim := "email@gmail.com" | |
kata_sandi := "sandi_rahasia" | |
// PENERIMA SUREL | |
penerima := "email@gmail.com" | |
pesan := "From: " + surel_pengirim + "\n" + | |
"To: " + penerima + "\n" + | |
"Subject: " + subject + "\n" + | |
body | |
// FUNGSI UNTUK MENGIRIM EMAIL MELALUI SMTP | |
err := smtp.SendMail("smtp.gmail.com:587", | |
smtp.PlainAuth("", surel_pengirim, kata_sandi, "smtp.gmail.com"), | |
surel_pengirim, []string{penerima}, []byte(pesan), | |
) | |
// GALAT PROGRAM AKAN TERCETAK JIKA ADA | |
if err != nil { | |
log.Print(err) | |
} | |
log.Print("Surel berahsil terkirim!") | |
} | |
func main() { | |
// FUNGSI INI MENERIMA DUA PARAMETER | |
kirim("Hello, world!", "Sapaan") | |
// PARAMETER PERTAMA BERUPA ISI DARI SUREL | |
// PARAMETER KEDUA ADALAH SUBJEK SUREL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment