Blog | Javad Nasrollahi

Blog | Javad Nasrollahi

Blog | Javad Nasrollahi | درباره ما

Diaries of a programmer

Blog | Javad Nasrollahi | لیست محصولات

In this blog, we’ll look at different methods to send an email with Go, First we will explore inbuilt smtp package, then we will move to use a popular package Gomail and finally we will send HTML emails using custom templates. Before You Get Started This tutorial assumes you have: A basic understanding of Go Language Latest GoLang version installed on your system A few minutes of your time. In this blog, we’ll look at different methods to send an email with Go, First, we will explore inbuilt smtp package, then we will move to use a popular package Gomail and finally, we will send HTML emails using custom templates. Package smtp smtp is an inbuilt package provided with Golang. It implements the Simple Mail Transfer Protocol and has multiple functionalities related to it. Here to send the email we will be using only two functions PlainAuth and SendMail from the package. Note: Click here for an overview on Go Functions PlainAuth: It uses the given username and password to authenticate to host and return an identity SendMail: It connects to the server at address, switches to TLS if possible, authenticates with the optional mechanism an if possible, and then sends an email to the sender. Below is the complete code to send a plain text email with smtp package in golang. package main import ( "fmt" "net/smtp" ) func main() { // Sender data. from := "[email protected]" password := "" // Receiver email address. to := []string{ "[email protected]", } // smtp server configuration. smtpHost := "smtp.gmail.com" smtpPort := "587" // Message. message := []byte("This is a test email message.") // Authentication. auth := smtp.PlainAuth("", from, password, smtpHost) // Sending email. err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message) if err != nil { fmt.Println(err) return } fmt.Println("Email Sent Successfully!") } In the above code example we have used smtp details of a Gmail account, you should update the smtp detail according to your email provider. Just to explain things easily, In the above snippet, we have written all the smtp and email credentials in the main function, Though in a production app you should always use env variables for configurations. You can check Viper to manage configurations in production apps. Package Gomail Below is the complete code to send a plain text email with Gomail package in golang. package main import ( "crypto/tls" "fmt" gomail "gopkg.in/mail.v2" ) func main() { m := gomail.NewMessage() // Set E-Mail sender m.SetHeader("From", "[email protected]") // Set E-Mail receivers m.SetHeader("To", "[email protected]") // Set E-Mail subject m.SetHeader("Subject", "Gomail test subject") // Set E-Mail body. You can set plain text or html with text/html m.SetBody("text/plain", "This is Gomail test body") // Settings for SMTP server d := gomail.NewDialer("smtp.gmail.com", 587, "[email protected]", "") // This is only needed when SSL/TLS certificate is not valid on server. // In production this should be set to false. d.TLSConfig = &tls.Config{InsecureSkipVerify: true} // Now send E-Mail if err := d.DialAndSend(m); err != nil { fmt.Println(err) panic(err) } return } Custom HTML Templates Now, let's send an HTML email with smtp package, for this, we need to create two files in the root folder. main.go: go code to parse HTML template and send it in email template.html : HTML template for emails Name: {{.Name}} Email: {{.Message}} We are using text/template package to parse HTML files and use it in smtp SendMail function. package main import ( "bytes" "fmt" "net/smtp" "text/template" ) func main() { // Sender data. from := "[email protected]" password := "" // Receiver email address. to := []string{ "[email protected]", } // smtp server configuration. smtpHost := "smtp.gmail.com" smtpPort := "587" // Authentication. auth := smtp.PlainAuth("", from, password, smtpHost) t, _ := template.ParseFiles("template.html") var body bytes.Buffer mimeHeaders := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" body.Write([]byte(fmt.Sprintf("Subject: This is a test subject \n%s\n\n", mimeHeaders))) t.Execute(&body, struct { Name string Message string }{ Name: "Puneet Singh", Message: "This is a test message in a HTML template", }) // Sending email. err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, body.Bytes()) if err != nil { fmt.Println(err) return } fmt.Println("Email Sent!") } Once done you need to run below command to send the emails go run main.go If you don't want to create your custom HTML emails, Hermes is a package that generates clean, responsive HTML e-mails for sending transactional e-mails. Now you can send beautiful emails to the customer by your golang application, You can found the complete code used in this blog on our Github Repo
The other answers are correct, in that chmod -R 755 will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files? I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example: To change all the directories to 755 (drwxr-xr-x): find /opt/lampp/htdocs -type d -exec chmod 755 {} \; To change all the files to 644 (-rw-r--r--): find /opt/lampp/htdocs -type f -exec chmod 644 {} \; Some splainin': (thanks @tobbez) chmod 755 {} specifies the command that will be executed by find for each directory chmod 644 {} specifies the command that will be executed by find for each file {} is replaced by the path ; the semicolon tells find that this is the end of the command it's supposed to execute \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find https://stackoverflow.com/a/11512211