send.go

 1package gmail
 2
 3import (
 4	"fmt"
 5	"log"
 6	"net/smtp"
 7)
 8
 9func main(from_email string, password string, to_email []string, subject string, body string) {
10
11	// Requires a Google App Password for your Gmail account.
12	// You must generate an App Password.
13	//
14	// How to generate a Google App Password:
15	// 1. Go to your Google Account at https://myaccount.google.com/
16	// 2. Select "Security" from the left navigation panel.
17	// 3. Under "How you sign in to Google," click on "2-Step Verification" and ensure it is turned ON.
18	// 4. Go back to the Security page and click on "App passwords." You may need to sign in again.
19	// 5. At the bottom, click "Select app" and choose "Mail."
20	// 6. Click "Select device" and choose "Other (Custom name)."
21	// 7. Give it a name (e.g., "Go Email Script") and click "Generate."
22	// FIXME:  8. Copy the 16-character password and paste it into the config.
23
24
25	// Gmail SMTP server settings.
26	// DO NOT EDIT
27	smtpHost := "smtp.gmail.com"
28	smtpPort := "587"
29
30	// --- Email Content ---
31	// The message must be formatted according to RFC 822.
32	// The "To", "From", and "Subject" headers are separated from the body by a blank line.
33	message := []byte("To: " + to_email[0] + "\r\n" +
34		"Subject: " + subject + "\r\n" +
35		body + "\r\n")
36
37	// --- Authentication ---
38	// Create an authentication object.
39	auth := smtp.PlainAuth("", from_email, password, smtpHost)
40
41	// --- Sending the Email ---
42	// Connect to the SMTP server, authenticate, and send the email.
43	err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from_email, to_email, message)
44	if err != nil {
45		log.Fatalf("Failed to send email: %s", err)
46	}
47
48	fmt.Println("Email sent successfully!")
49}