@@ -538,8 +538,9 @@ func (e *Email) parseSender() (string, error) {
538
538
}
539
539
}
540
540
541
- // SendWithTLS sends an email with an optional TLS config.
542
- // This is helpful if you need to connect to a host that is used an untrusted
541
+ // SendWithTLS sends an email over tls with an optional TLS config.
542
+ //
543
+ // The TLS Config is helpful if you need to connect to a host that is used an untrusted
543
544
// certificate.
544
545
func (e * Email ) SendWithTLS (addr string , a smtp.Auth , t * tls.Config ) error {
545
546
// Merge the To, Cc, and Bcc fields
@@ -578,6 +579,75 @@ func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
578
579
if err = c .Hello ("localhost" ); err != nil {
579
580
return err
580
581
}
582
+
583
+ if a != nil {
584
+ if ok , _ := c .Extension ("AUTH" ); ok {
585
+ if err = c .Auth (a ); err != nil {
586
+ return err
587
+ }
588
+ }
589
+ }
590
+ if err = c .Mail (sender ); err != nil {
591
+ return err
592
+ }
593
+ for _ , addr := range to {
594
+ if err = c .Rcpt (addr ); err != nil {
595
+ return err
596
+ }
597
+ }
598
+ w , err := c .Data ()
599
+ if err != nil {
600
+ return err
601
+ }
602
+ _ , err = w .Write (raw )
603
+ if err != nil {
604
+ return err
605
+ }
606
+ err = w .Close ()
607
+ if err != nil {
608
+ return err
609
+ }
610
+ return c .Quit ()
611
+ }
612
+
613
+ // SendWithStartTLS sends an email over TLS using STARTTLS with an optional TLS config.
614
+ //
615
+ // The TLS Config is helpful if you need to connect to a host that is used an untrusted
616
+ // certificate.
617
+ func (e * Email ) SendWithStartTLS (addr string , a smtp.Auth , t * tls.Config ) error {
618
+ // Merge the To, Cc, and Bcc fields
619
+ to := make ([]string , 0 , len (e .To )+ len (e .Cc )+ len (e .Bcc ))
620
+ to = append (append (append (to , e .To ... ), e .Cc ... ), e .Bcc ... )
621
+ for i := 0 ; i < len (to ); i ++ {
622
+ addr , err := mail .ParseAddress (to [i ])
623
+ if err != nil {
624
+ return err
625
+ }
626
+ to [i ] = addr .Address
627
+ }
628
+ // Check to make sure there is at least one recipient and one "From" address
629
+ if e .From == "" || len (to ) == 0 {
630
+ return errors .New ("Must specify at least one From address and one To address" )
631
+ }
632
+ sender , err := e .parseSender ()
633
+ if err != nil {
634
+ return err
635
+ }
636
+ raw , err := e .Bytes ()
637
+ if err != nil {
638
+ return err
639
+ }
640
+
641
+ // Taken from the standard library
642
+ // https://github.com/golang/go/blob/master/src/net/smtp/smtp.go#L328
643
+ c , err := smtp .Dial (addr )
644
+ if err != nil {
645
+ return err
646
+ }
647
+ defer c .Close ()
648
+ if err = c .Hello ("localhost" ); err != nil {
649
+ return err
650
+ }
581
651
// Use TLS if available
582
652
if ok , _ := c .Extension ("STARTTLS" ); ok {
583
653
if err = c .StartTLS (t ); err != nil {
0 commit comments