# Fixing "agent sent the invoice PDF to the wrong customer: email field misalignment"

## TL;DR

Bind each invoice to its customer id and pull the email from the customer record at send time. Never carry emails in a parallel list, spreadsheet column, or array that can shift out of alignment. One misaligned row is all it takes to send someone else's invoice to the wrong inbox.

```text
agent sent the invoice PDF to the wrong customer: email field misalignment
```

## Steps

1. Rewrite the send step to take an invoice id, load the invoice, load its customer, and use the customer record's email.
   Expected: The recipient is derived from the invoice itself, not from a separate list.
2. Delete any code that zips invoices with emails from two separate sources.
   Expected: There is exactly one path from invoice to recipient.
3. Add a pre-send check: the email domain or customer name should match the invoice's billing details; flag mismatches.
   Expected: A misaligned send is caught before the email leaves.
4. Send a test batch to internal addresses first and verify each recipient matches its invoice.
   Expected: Every test email lands with the right invoice attached.

## Prevention

The root design rule: the invoice knows its customer. Any architecture where recipient addresses travel separately from invoices will eventually misalign after a sort, filter, or off-by-one. Keep them joined.

## Detection

Watch for bounce-backs on invoice emails and for customers replying "this isn't mine." Compare sent-email logs against invoice customer ids; any invoice whose sent-to address differs from the customer record's email is a hit.

## Recovery

Notify both affected customers promptly: tell the wrong recipient to delete the invoice, and send the correct invoice to the right customer. If the invoice contained sensitive data, treat it as a data exposure incident per your policy. Then fix the send path before the next batch.

## When this applies

- Invoice emails went to addresses that don't belong to the invoiced customer
- A batch send used a spreadsheet or list where rows shifted
- Customers report receiving someone else's invoice

## When it doesn't

- The email address on the customer record itself is wrong (fix the record, not the send logic)
- The invoice was shared via a link the customer forwarded themselves
- Delivery failed entirely (that is a mail-delivery problem, not misalignment)

## Compatibility

Stripe API (invoice customer and customer email), Adyen (invoice and shopper email), PayPal REST API v2 (invoicing with recipient info), plus any mail provider (SendGrid, SES, Postmark) used for the send step.

### invoice emailed to the wrong address from a csv batch

### customer received another customer's invoice PDF

### batch invoice send mixed up recipients

## Why it happens

Batch jobs love two parallel arrays: invoice ids from one query, emails from another. A sort applied to one but not the other, a filtered row, or a header offset shifts everything by one, and every invoice after the shift goes to the wrong person. It looks correct until someone complains.

## Edge cases

- Customers with multiple email addresses: define which one is the billing email and use only that
- Invoice forwarding: once sent correctly, a customer forwarding it themselves is not your bug
- BCC batches: never BCC multiple customers on one invoice email; send individually so a misalignment affects one recipient, not all

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_p40M1wEztc2dnU4Pf-kqvw
