mirror of
https://github.com/GRFreire/nthmail.git
synced 2026-01-09 04:49:39 +00:00
add support for cc and bcc
now it can handle when mails are carbon-copied fixes #1 fixes #2
This commit is contained in:
parent
f3d8e7052f
commit
a8860ea560
@ -40,13 +40,13 @@ type Session struct {
|
|||||||
domain string
|
domain string
|
||||||
}
|
}
|
||||||
|
|
||||||
func get_addr_domain(addr string) string {
|
func append_addrs_with_domain(addrs []string, domain string, with_domain *[]string) {
|
||||||
index := strings.Index(addr, "@")
|
for _, a := range addrs {
|
||||||
if index < 0 {
|
index := strings.Index(a, "@")
|
||||||
return ""
|
if index > 0 && a[index+1:] == domain {
|
||||||
|
*with_domain = append(*with_domain, a)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addr[index+1:]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) AuthPlain(username, password string) error {
|
func (session *Session) AuthPlain(username, password string) error {
|
||||||
@ -61,9 +61,6 @@ func (session *Session) Mail(from string, opts *smtp.MailOptions) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
|
func (session *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
|
||||||
if get_addr_domain(to) != session.domain {
|
|
||||||
return errors.New("To addr domain is not available in this server")
|
|
||||||
}
|
|
||||||
session.rcpt = to
|
session.rcpt = to
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -82,20 +79,28 @@ func (session *Session) Data(reader io.Reader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if get_addr_domain(mail_obj.To) != session.domain {
|
var addrs []string
|
||||||
return errors.New("To addr domain is not available in this server")
|
append_addrs_with_domain(mail_obj.To, session.domain, &addrs)
|
||||||
|
append_addrs_with_domain(mail_obj.Cc, session.domain, &addrs)
|
||||||
|
append_addrs_with_domain(mail_obj.Bcc, session.domain, &addrs)
|
||||||
|
|
||||||
|
if len(addrs) <= 0 {
|
||||||
|
return errors.New("Not a single addr from to, cc and cc has the domain available in this server")
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt, err := session.tx.Prepare("INSERT INTO mails (arrived_at, rcpt_addr, from_addr, subject, data) VALUES (?, ?, ?, ?, ?)")
|
for _, addr := range addrs {
|
||||||
if err != nil {
|
stmt, err := session.tx.Prepare("INSERT INTO mails (arrived_at, rcpt_addr, from_addr, subject, data) VALUES (?, ?, ?, ?, ?)")
|
||||||
println(err)
|
if err != nil {
|
||||||
return err
|
println(err)
|
||||||
}
|
return err
|
||||||
defer stmt.Close()
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
_, err = stmt.Exec(session.arrived_at, addr, mail_obj.From, mail_obj.Subject, bytes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
_, err = stmt.Exec(session.arrived_at, session.rcpt, mail_obj.From, mail_obj.Subject, bytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = session.tx.Commit()
|
err = session.tx.Commit()
|
||||||
|
|||||||
@ -38,8 +38,9 @@ type Mail_obj struct {
|
|||||||
Id int
|
Id int
|
||||||
From string
|
From string
|
||||||
Date time.Time
|
Date time.Time
|
||||||
To string
|
To []string
|
||||||
Bcc string
|
Cc []string
|
||||||
|
Bcc []string
|
||||||
Subject string
|
Subject string
|
||||||
|
|
||||||
Body []Mail_body
|
Body []Mail_body
|
||||||
@ -79,10 +80,26 @@ func Parse_mail(m_data []byte, header_only bool) (Mail_obj, error) {
|
|||||||
// HEADERS
|
// HEADERS
|
||||||
dec := new(mime.WordDecoder)
|
dec := new(mime.WordDecoder)
|
||||||
m.From, _ = dec.DecodeHeader(mail_msg.Header.Get("From"))
|
m.From, _ = dec.DecodeHeader(mail_msg.Header.Get("From"))
|
||||||
m.To, _ = dec.DecodeHeader(mail_msg.Header.Get("To"))
|
|
||||||
m.Bcc, _ = dec.DecodeHeader(mail_msg.Header.Get("Bcc"))
|
|
||||||
m.Subject, _ = dec.DecodeHeader(mail_msg.Header.Get("Subject"))
|
m.Subject, _ = dec.DecodeHeader(mail_msg.Header.Get("Subject"))
|
||||||
|
|
||||||
|
to_addrs, _ := mail_msg.Header.AddressList("To")
|
||||||
|
m.To = make([]string, len(to_addrs))
|
||||||
|
for i, a := range to_addrs {
|
||||||
|
m.To[i] = a.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
cc_addrs, _ := mail_msg.Header.AddressList("Cc")
|
||||||
|
m.Cc = make([]string, len(to_addrs))
|
||||||
|
for i, a := range cc_addrs {
|
||||||
|
m.Cc[i] = a.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
bcc_addrs, _ := mail_msg.Header.AddressList("Bcc")
|
||||||
|
m.Bcc = make([]string, len(to_addrs))
|
||||||
|
for i, a := range bcc_addrs {
|
||||||
|
m.Bcc[i] = a.Address
|
||||||
|
}
|
||||||
|
|
||||||
if header_only {
|
if header_only {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -159,7 +159,7 @@ func (sr ServerResouces) handleInbox(res http.ResponseWriter, req *http.Request)
|
|||||||
var mail_obj mail_utils.Mail_obj
|
var mail_obj mail_utils.Mail_obj
|
||||||
mail_obj.Id = m.Id
|
mail_obj.Id = m.Id
|
||||||
mail_obj.Date = time.Unix(m.Arrived_at, 0)
|
mail_obj.Date = time.Unix(m.Arrived_at, 0)
|
||||||
mail_obj.To = m.Rcpt_addr
|
mail_obj.To = []string{m.Rcpt_addr}
|
||||||
mail_obj.From = m.From_addr
|
mail_obj.From = m.From_addr
|
||||||
mail_obj.Subject = m.Subject
|
mail_obj.Subject = m.Subject
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user