Golang: Send a message using a custom template

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

https://medium.com/@itsHabib/sending-emails-with-go-using-a-custom-template-ae863b65a859
author : Michael Habib
Translator : http://oopsguy.com

A few weeks ago, I started working on a portfolio portfolio site. Although I can only use React to complete the entire site, I decided to use Go to create an API server that can handle certain tasks, such as sending an email, which I believe is a good practice. One of my pages is a contact page that currently looks like this:


Contact me

I want to send a message using a Gmail account that is specifically requested for this contact form. In addition to the fact that I used Javascript to send e-mails, I didn't have a particular choice of Go. But why not try Go? I think Go is great.

Step one: Create the necessary files

You only need two files, an HTML file and a go file. They can be placed in the same directory. Just make sure you can refer to the HTML file correctly.


Note: You don't even need an HTML file, but you must write HTML as a string in the go file. I assume that your Go environment is set up correctly. If not, please refer to here: https://golang.org/doc/code.html

Step two: Create a template


The template is a very simple HTML, and you can customize it. Ignore first . Name, we'll come back later.

Step Three: Go!

1. Add the necessary packages


When you read, you should know why we need these.

2. Sign in to Gmail


Lines 14th to 16th: I get the user name and password for my Gmail account from the environment variables I set up earlier. Use smtp the package PlaneAuth method to log in with the user name, password, and hostname of the email server.

3. Parsing templates and setting up email


Lines 20th to 26th: First create a template by parsing the HTML, and then store it in a variable t (later). Then declare a bytes.Buffer subject, header, and body (custom HTML) to hold the email. I found that using bytes.Buffer types is more flexible than other types used in other online solutions. fmtin the package Sprintf is a convenient way to return a formatted string. You can use it to return a string that contains the email subject and header. The string is then converted to a byte array, and the method is used to write to the Write byte buffer. So far, the byte buffer contains the subject and header. Let's add a little bit more now. Note: Like HTTP, you must add two lines of new rows after the header.

4. Fill in the data template and send email


Lines 28th to 37th: then we need to execute the template, and the first parameter must implement the io.Writer interface. Because the bytes.Buffer type implements the io.Writer interface, we can body use it by passing in a pointer to the variable. Why we use body pointers instead of this value is about how Go implements interfaces, and you can get more information in the documentation. The second parameter contains the template (HTML) data that we want to pass in. Remember in the HTML . Name ? That's how we refer to the data that executes the template. .is our structure, which Name refers to attributes. Don't be confused about struct syntax, I just declare the struct in one statement and initialize it. Using variables in HTML is just one of the things you can do in the Go template.

Finally, use the SendMail method to bring up the host and port, the previously created Auth type, the from field string, and a string array containing the address to send the email to. As far as I know, the from field string does nothing. Maybe it's useful to other email servers?

If you want smtp more information about the package, this is a link to the document. You can also read about text/template the contents of the package here. I know we used the html/template package, but in Godocs, the implementation details of the template are in text/template the document.

Translator note

The field specified at the end of the text from is the sender in the email.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.