package smtp12import (3"bufio"4"bytes"5"net/textproto"6"strings"7)89type (10// SMTPMessage is a message to be sent over SMTP11// @example12// ```javascript13// const smtp = require('nuclei/smtp');14// const message = new smtp.SMTPMessage();15// message.From('[email protected]');16// ```17SMTPMessage struct {18from string19to []string20sub string21msg []byte22user string23pass string24}25)2627// From adds the from field to the message28// @example29// ```javascript30// const smtp = require('nuclei/smtp');31// const message = new smtp.SMTPMessage();32// message.From('[email protected]');33// ```34func (s *SMTPMessage) From(email string) *SMTPMessage {35s.from = email36return s37}3839// To adds the to field to the message40// @example41// ```javascript42// const smtp = require('nuclei/smtp');43// const message = new smtp.SMTPMessage();44// message.To('[email protected]');45// ```46func (s *SMTPMessage) To(email string) *SMTPMessage {47s.to = append(s.to, email)48return s49}5051// Subject adds the subject field to the message52// @example53// ```javascript54// const smtp = require('nuclei/smtp');55// const message = new smtp.SMTPMessage();56// message.Subject('hello');57// ```58func (s *SMTPMessage) Subject(sub string) *SMTPMessage {59s.sub = sub60return s61}6263// Body adds the message body to the message64// @example65// ```javascript66// const smtp = require('nuclei/smtp');67// const message = new smtp.SMTPMessage();68// message.Body('hello');69// ```70func (s *SMTPMessage) Body(msg []byte) *SMTPMessage {71s.msg = msg72return s73}7475// Auth when called authenticates using username and password before sending the message76// @example77// ```javascript78// const smtp = require('nuclei/smtp');79// const message = new smtp.SMTPMessage();80// message.Auth('username', 'password');81// ```82func (s *SMTPMessage) Auth(username, password string) *SMTPMessage {83s.user = username84s.pass = password85return s86}8788// String returns the string representation of the message89// @example90// ```javascript91// const smtp = require('nuclei/smtp');92// const message = new smtp.SMTPMessage();93// message.From('[email protected]');94// message.To('[email protected]');95// message.Subject('hello');96// message.Body('hello');97// log(message.String());98// ```99func (s *SMTPMessage) String() string {100var buff bytes.Buffer101tw := textproto.NewWriter(bufio.NewWriter(&buff))102_ = tw.PrintfLine("To: %s", strings.Join(s.to, ","))103if s.sub != "" {104_ = tw.PrintfLine("Subject: %s", s.sub)105}106_ = tw.PrintfLine("\r\n%s", s.msg)107return buff.String()108}109110111