# WPForms Bounce Resend Workflow

When WPForms sends confirmation/notification emails that bounce (e.g. Gmail SPF/DKIM rejection), this workflow covers detection, identification, and resend.

## Trigger

A registrant says they didn't get their confirmation email, OR the Apex mail watchdog triggers a bounce alert.

## Step 1: Check inbox for bounce messages

Connect to the SiteGround IMAP inbox and search for delivery failure notifications:

```python
import imaplib, email
HOST = "c1113726.sgvps.net"
PORT = 993
USER = "contact@apextrackexperience.com"
PW = "apex.track!!"
conn = imaplib.IMAP4_SSL(HOST, PORT)
conn.login(USER, PW)
conn.select("INBOX")
status, ids = conn.search(None, '(OR FROM "mailer-daemon" OR FROM "postmaster") SINCE "01-Jul-2026"')
```

### What bounce messages look like

| Field | Value |
|-------|-------|
| From | `Mail Delivery Subsystem <mailer-daemon@mxroute.com>` |
| Subject | `Delivery Status Notification (Failure)` |
| Body | `Delivery to the following recipient failed permanently: <email>` + reason |

### Known bounce cause: SPF/DKIM authentication

Gmail blocks when sending IP not in SPF:
```
550-5.7.26 Your email has been blocked because the sender is unauthenticated.
SPF [apextrackexperience.com] with ip: [136.175.108.114] = did not pass
```
IP `136.175.108.x` = MXroute (not SiteGround). Fix: ensure email routes through SiteGround SMTP (`35.212.86.161`).

## Step 2: Identify affected registrants

Cross-reference bounced email addresses against WPForms entries:

```sql
SELECT entry_id, form_id, date, LEFT(fields,2000) FROM wp_wpforms_entries
WHERE fields LIKE '%<bounced-email>%' ORDER BY entry_id;
```

### Form ID reference

| ID | Name | Purpose |
|----|------|---------|
| 270 | NASA Top Speed Event Registration | Paid track day ($1,973 - $2,332) |
| 268 | Waiver | Free liability waiver |

### Fields to extract from the JSON `fields` column

Form 270: `$.1.first`+`$.1.last` = driver name, `$.2` = email, `$.33` = vehicle, `$.22.value_choice` = package, `$.10.value` = total, `$.34.first`+`$.34.last` = passenger
Form 268: `$.0.first`+`$.0.last` = participant, `$.1` = email, `$.21.value` = risk release signed

## Step 3: Resend confirmation via SMTP

```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS = "c1113726.sgvps.net", 2525, "contact@apextrackexperience.com", "apex.track!!"
msg = MIMEMultipart('alternative')
msg['From'] = "Apex Predators Track Experience <contact@apextrackexperience.com>"
msg['To'] = f"{name} <{email}>"
msg['Subject'] = subject
msg.attach(MIMEText(html_body, 'html'))
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
    server.starttls()
    server.login(SMTP_USER, SMTP_PASS)
    server.sendmail(SMTP_USER, [email], msg.as_string())
```

### Email format template

Dark header (`#1a1a2e`), red accent (`#e94560`), white content area, green confirmation for waivers, yellow info box. Footer with company name.

### What to include per recipient

**Driver (registration + waiver):** Full reg details, waiver status for both, event details note.
**Passenger (waiver only):** Their name, driver name, vehicle, waiver confirmation.

## SPF/DKIM status (post Jul 8)

- SPF: `v=spf1 +a +mx ip4:35.212.86.161 include:...dnssmarthost.net ~all`
- DKIM: `default._domainkey.apextrackexperience.com` configured
- DMARC: `v=DMARC1; p=none`
- SiteGround relay IP `35.212.86.161` IS in SPF (resolves from `c1113726.sgvps.net`)

## Credentials

```
Email:    contact@apextrackexperience.com
SMTP:     c1113726.sgvps.net:2525 STARTTLS
IMAP:     c1113726.sgvps.net:993 SSL
Password: apex.track!!
DB:       mysql -h 127.0.0.1 -P 33060 -u apextrackexperience_1781549652
```
