A few weeks ago, I started working on a portfolio website. Although I can only use react to complete the whole website, I decided to use go to create an API server that can handle some tasks (such as sending email), which I believe is a good practice.
One of my pages is a contact page, which currently looks like this:
Step 1: 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 reference the HTML file correctly.
Be careful:You don’t even need an HTML file, but you have to write HTML as a string in the go file. I assume your go environment is set up correctly. If not, please refer to here: https://golang.org/doc/code.html
Step 2: create a template
The template is a very simple piece of HTML, but you can customize it. Ignore. Name first, we’ll be back later.
Step 3: go!
1. Add necessary packages
When you read, you should know why we need these.
2. Log in to Gmail
Lines 14 to 16: I get the user name and password of my Gmail account from the environment variable I set earlier. Use the planeauth method of the SMTP package to log in with the username, password, and host name of the email server.
3. Parse template and set email
Lines 20 to 26: first create a template by parsing the HTML, then store it in a variable t (later). A bytes. Buffer is then declared to hold the subject, header, and body of the email (custom HTML). I find that using the bytes.buffer type is more flexible than other types used in other online solutions. Sprint f in the FMT package is a convenient way to return formatted strings. You can use it to return a string containing the email subject and header. The string is then converted to a byte array and written to the byte buffer using the write method. So far, the byte buffer contains the subject and header. Let’s add a little more. Note: like HTTP, you must add two new lines after the header.
4. Fill in data template and send email
Lines 28 to 37: 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 use it by passing in a pointer to the body variable. Why we use a pointer to body instead of the value is related to how go implements the interface, and you can get more information in the documentation. The second parameter contains the template (HTML) data we want to pass in. Remember. Name in HTML? That’s how we reference the data of the execution template. . is our structure, and name is the property. Don’t be confused about the struct syntax, I just declare the struct in a statement and initialize it. Using variables in html is just one of the things you can do in a go template.
Finally, use the sendmail method to bring up the host and port, the previously created auth type, the from field string, and an array of strings containing the address to send the email. As far as I know, the from field string does nothing. Maybe it’s useful for other email servers?
If you want to learn more about the SMTP package, this is a link to the document. You can also read about the text / template package here. I know we used the HTML / template package, but in godocs, the implementation details of template are in the document of text / template.
The from field mentioned at the end of the article specifies the sender in email.
The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.