Use Golang to send interview invitation messages in bulk

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

Recently, the company recruit app developers, daily CV up to hundreds, often need to send more than 10 people e-mail, email content basically similar, is to tell the other company's detailed address and bus routes, the difference is the name of the interviewer, the job candidates, the interview time and email address is different, Such a copy and paste to send e-mail is really a waste of time, especially for a programmer. Since as a programmer, it is necessary to have a programmer's sample, write a program to solve. I think is to invite interview personnel information into an Excel, through the program to read out and automatically send mail, of course, this function is not difficult, with C # also easy to achieve, but recently is learning some golang, just take this practice.

First of all, the e-mail function, this system comes with the package, quoted Net/smtp on the line, in addition to the Internet also has a relatively perfect example of writing, take to use on the line. The code to send the message is as follows

  

Package Libofmimport ("NET/SMTP"    "Strings")Const(HOST="smtp.****.com"server_addr="smtp.****.com:25"USER="test@****.com" //the mailbox that sent the messagePASSWORD ="123456"              //password to send mail mailbox) Type Emailstruct{ tostring " to"subjectstring "subject"msgstring "msg"Mailtypestring "HTML"}func Newemail (to, Subject, MSG, Mailtypestring) *Email {return&email{to:to, Subject:subject, msg:msg, Mailtype:mailtype}}func SendEmail (Email*Email) Error {auth:= SMTP. Plainauth ("", USER, PASSWORD, HOST) SendTo:= Strings. Split (Email.to,";") Done:= Make (chan error,1024x768)    varContent_Typestring    ifEmail.mailtype = ="HTML"{Content_Type="content-type:text/"+ Email.mailtype +"; Charset=utf-8"    } Else{Content_Type="Content-type:text/plain"+"; Charset=utf-8"} go func () {defer close (done) for_, V: =range SendTo {str:= Strings. Replace ("From :"+user+"~to:"-vm"~subject:"+email.subject+"~"+content_type+"~~","~","\ r \ n", -1) +email.msg ERR:=SMTP. SendMail (server_addr, Auth, USER, []string{v}, []byte(str),) done<-Err}} ()     forI: =0; I < Len (sendTo); i++ {        <-Done }returnNil}

Again is to add the ability to read the Excel file, fortunately, there is a third-party package github.com/tealeg/xlsx, referred to the project can be. But before the reference, install this third-party package first, because the package is on GitHub, so just open the command-line window, execute go get github.com/tealeg/xlsx on the line, of course, the premise is to install the git, You also need to add Git's execution directory to the system variable path, such as C:\Program Files\git\bin. The sample code is as follows

Package Mainimport ("FMT"    "github.com/tealeg/xlsx") Func main () {excelfilename:="/home/tealeg/foo.xlsx" //Excel file pathXlfile, err: =xlsx. OpenFile (Excelfilename)ifErr! =Nil {...}  for_, Sheet: =Range Xlfile.sheets { for_, Row: =Range Sheet. Rows { for_, Cell: =range Row. Cells {fmt. Printf ("%s\n", Cell. String ())//after reading the content, call the code that sends the message            }        }    }}    

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.