Send normalizes recipients, composes, and sends an email via SMTP relay. Returns a ValidationError for caller errors (bad addresses, no visible recipients) and a plain error for transport failures.
(agent *identity.AgentIdentity, req SendRequest)
| 176 | // Returns a ValidationError for caller errors (bad addresses, no visible recipients) |
| 177 | // and a plain error for transport failures. |
| 178 | func (s *Sender) Send(agent *identity.AgentIdentity, req SendRequest) (*SendResult, error) { |
| 179 | agentAddr := strings.ToLower(agent.EmailAddress()) |
| 180 | agentAliases := []string{ |
| 181 | agentAddr, |
| 182 | strings.ToLower(fmt.Sprintf("agent@%s", s.fromDomain)), |
| 183 | } |
| 184 | |
| 185 | // Normalize and validate all addresses |
| 186 | to, err := normalizeAddrs(req.To) |
| 187 | if err != nil { |
| 188 | return nil, &ValidationError{Message: fmt.Sprintf("invalid To address: %v", err)} |
| 189 | } |
| 190 | cc, err := normalizeAddrs(req.CC) |
| 191 | if err != nil { |
| 192 | return nil, &ValidationError{Message: fmt.Sprintf("invalid CC address: %v", err)} |
| 193 | } |
| 194 | bcc, err := normalizeAddrs(req.BCC) |
| 195 | if err != nil { |
| 196 | return nil, &ValidationError{Message: fmt.Sprintf("invalid BCC address: %v", err)} |
| 197 | } |
| 198 | |
| 199 | // Remove agent's own addresses |
| 200 | to = removeAddrs(to, agentAliases) |
| 201 | cc = removeAddrs(cc, agentAliases) |
| 202 | bcc = removeAddrs(bcc, agentAliases) |
| 203 | |
| 204 | // Dedupe within each field |
| 205 | to = dedupe(to) |
| 206 | cc = dedupe(cc) |
| 207 | bcc = dedupe(bcc) |
| 208 | |
| 209 | // Cross-field dedupe: To > CC > BCC |
| 210 | cc = removeAddrs(cc, to) |
| 211 | bcc = removeAddrs(bcc, to) |
| 212 | bcc = removeAddrs(bcc, cc) |
| 213 | |
| 214 | // Visible-recipient check: at least one address in To or CC |
| 215 | if len(to) == 0 && len(cc) == 0 { |
| 216 | return nil, &ValidationError{Message: "no valid recipients"} |
| 217 | } |
| 218 | |
| 219 | // Build envelope recipients (To + CC + BCC) |
| 220 | envelope := make([]string, 0, len(to)+len(cc)+len(bcc)) |
| 221 | envelope = append(envelope, to...) |
| 222 | envelope = append(envelope, cc...) |
| 223 | envelope = append(envelope, bcc...) |
| 224 | |
| 225 | // Compose headers |
| 226 | displayName := agent.Name |
| 227 | if displayName == "" { |
| 228 | displayName = agent.EmailAddress() |
| 229 | } |
| 230 | // Resolve the sending-verified gate once (it hits the sending_status store), |
| 231 | // then derive both the header From and the envelope Return-Path from it. |
| 232 | own := s.useOwnAddressFrom(agent) |
| 233 | // Envelope MAIL FROM (Return-Path): the aligned custom MAIL FROM |
| 234 | // (bounce.<domain>) for a verified domain — SPF authenticates the From |
| 235 | // org-domain → no Gmail "via e2a" — else the e2a-owned relay address |
nothing calls this directly
no test coverage detected