Golang : Send email with attachment
In my previous tutorial on Go's send email and configure SMTP example, I left out a part where any decent email package should have - that is the - email attachment part.
So, how to attach files onto email in Golang ?
After trying to write a decent tutorial to demonstrate this...only then I realized that the codes can be super messy and unreadable. Therefore, I would rather settle for this simple to use third party package https://github.com/scorredoira/email to get this tutorial published. (I'm lazy)
Here it is :
package main
import (
"github.com/scorredoira/email"
"fmt"
"net/smtp"
"strconv"
)
type EmailConfig struct {
Username string
Password string
Host string
Port int
}
func main() {
// authentication configuration
smtpHost := "smtp.***.com" // change to your SMTP provider address
smtpPort := *** // change to your SMPT provider port number
smtpPass := "****" // change here
smtpUser := "******" // change here
emailConf := &EmailConfig{smtpUser, smtpPass, smtpHost, smtpPort}
emailauth := smtp.PlainAuth("", emailConf.Username, emailConf.Password, emailConf.Host)
sender := "******@mail.com" // change here
receivers := []string{
"*****@mail.com",
"****@mail.com",
} // change here
message := "Please see the email attachment for the images"
subject := "Attached my photos!"
emailContent := email.NewMessage(subject, message)
emailContent.From = sender
emailContent.To = receivers
files := []string{
"big.jpg",
"small.jpg",
} // change here to your own files
for _, filename := range files {
err := emailContent.Attach(filename)
if err != nil {
fmt.Println(err)
}
}
// send out the email
err := email.Send(smtpHost+":"+strconv.Itoa(emailConf.Port), //convert port number from int to string
emailauth,
emailContent)
if err != nil {
fmt.Println(err)
}
}
Executing this program will not generate any message unless there is error and if everything goes well, you should receive the email with attachment.
https://www.socketloop.com/tutorials/golang-send-email-and-smtp-configuration-example
See also : Golang : Send email and SMTP configuration example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+36.1k Golang : Convert(cast) int64 to string
+6.3k Golang : How to determine if request or crawl is from Google robots
+7.5k Golang : get the current working directory of a running program
+6.2k Golang : Break string into a slice of characters example
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+7.5k Golang : Example of how to detect which type of script a word belongs to
+12k Golang : Encrypt and decrypt data with x509 crypto
+7.2k Golang : Check to see if *File is a file or directory
+14.6k Golang : Basic authentication with .htpasswd file
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+19.7k Golang : Determine if directory is empty with os.File.Readdir() function
+9.9k Golang : Use regular expression to get all upper case or lower case characters example