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 } } }}