Wp_mail Multiple Recipients
December 16, 2024 ยท AI Conversations
### Syntax for `wp_mail()`:
wp_mail( $to, $subject, $message, $headers, $attachments );
### Sending to Multiple Email Addresses
$to = 'example1@example.com, example2@example.com, example3@example.com'; // Comma-separated emails
$subject = 'Test Email';
$message = 'This is a test email sent to multiple recipients.';
$headers = array('Content-Type: text/html; charset=UTF-8'); // Optional headers for HTML emails
wp_mail( $to, $subject, $message, $headers );
### Key Points:
1. **Comma Separation**:
– The email addresses in the `$to` parameter should be separated by commas.
– Example: `'email1@example.com, email2@example.com'`.
2. **Array Option (For Dynamic Addresses)**:
– If you're generating the email list dynamically, use an array and `implode()`:
$recipients = array('email1@example.com', 'email2@example.com', 'email3@example.com');
$to = implode(', ', $recipients);
wp_mail( $to, $subject, $message, $headers );
3. **BCC or CC**:
– To add CC (carbon copy) or BCC (blind carbon copy), use the `$headers` parameter:
$headers = array(
'Cc: ccemail@example.com',
'Bcc: bccemail@example.com'
);
wp_mail( $to, $subject, $message, $headers );
4. **HTML Emails**:
– Ensure your email content is formatted as HTML by setting the appropriate `Content-Type` in headers:
$headers = array('Content-Type: text/html; charset=UTF-8');
### Example With All Parameters
$to = 'recipient1@example.com, recipient2@example.com';
$subject = 'Meeting Reminder';
$message = '<h1>Reminder</h1><p>Don't forget the meeting tomorrow at 10 AM.</p>';
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'Cc: manager@example.com',
'Bcc: admin@example.com'
);
$attachments = array('/path/to/file1.pdf', '/path/to/file2.jpg'); // Optional
wp_mail( $to, $subject, $message, $headers, $attachments );
This will send the email to multiple recipients, optionally adding CC, BCC, and attachments.