Created
April 2, 2022 06:52
-
-
Save toshvelaga/67ec3edb6e33781b84a881d91b4f4896 to your computer and use it in GitHub Desktop.
send emails using golang and aws lambda
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 | |
import ( | |
"fmt" | |
"net/smtp" | |
"os" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
func HandleRequest() { | |
// Sender data. | |
from := os.Getenv("EMAIL") | |
password := os.Getenv("EMAIL_PASSWORD") | |
// Receiver email address. | |
// Change this to your email... | |
to := []string{ | |
"toshvelaga@gmail.com", | |
} | |
// smtp server configuration. | |
smtpHost := "smtp.gmail.com" | |
smtpPort := "587" | |
// Message. | |
message := []byte( | |
"Subject: This is a test email\r\n" + "\r\n" + "Here is a test message\r\n") | |
// Authentication. | |
auth := smtp.PlainAuth("", from, password, smtpHost) | |
// Sending email. | |
emailErr := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message) | |
if emailErr != nil { | |
fmt.Println(emailErr) | |
return | |
} | |
fmt.Printf("Email successfully sent to %s", to[0]) | |
} | |
func main() { | |
lambda.Start(HandleRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment