The Email task provides a simple and reliable way to send emails asynchronously. It leverages WordPress's wp_mail() function and includes automatic retries if the email fails to send.
public function __construct(
string $to_email,
string $subject,
string $body,
array $headers = [],
array $attachments = []
)$to_email(string, required): Recipient's email address(es). Can be a single email or multiple comma-separated emails.$subject(string, required): Email subject line$body(string, required): Email body content (HTML or plain text)$headers(array, optional): Email headers (e.g., content type, reply-to)$attachments(array, optional): File paths to attach to the email
- Task Prefix:
shepherd_email_ - Max Retries: 4 additional attempts (5 total attempts)
- Retry Delay: 30 seconds between attempts
- Priority: 10 (default)
- Group:
shepherd_{prefix}_queue_default
use StellarWP\Shepherd\Tasks\Email;
use function StellarWP\Shepherd\shepherd;
// Simple text email
$email = new Email(
'user@example.com',
'Welcome to our service',
'Thank you for signing up!'
);
shepherd()->dispatch( $email );$email = new Email(
'user@example.com',
'Your Account Update',
'<h1>Account Updated</h1><p>Your account information has been successfully updated.</p>',
[
'Content-Type: text/html; charset=UTF-8',
'From: noreply@example.com',
'Reply-To: support@example.com'
]
);
shepherd()->dispatch( $email );// Send to multiple recipients
$email = new Email(
'user1@example.com, user2@example.com, admin@example.com',
'Team Update',
'Important update for all team members.'
);
shepherd()->dispatch( $email );
// With proper spacing (whitespace is automatically handled)
$email = new Email(
'user1@example.com,user2@example.com, user3@example.com',
'Newsletter',
'<h1>Weekly Newsletter</h1><p>Here are this week\'s updates...</p>',
[ 'Content-Type: text/html; charset=UTF-8' ]
);
shepherd()->dispatch( $email );$email = new Email(
'user@example.com',
'Your Weekly Report',
'<h1>Weekly Report</h1><p>Please find your report attached.</p>',
[ 'Content-Type: text/html; charset=UTF-8' ],
[
WP_CONTENT_DIR . '/uploads/reports/weekly-report.pdf',
WP_CONTENT_DIR . '/uploads/reports/summary.xlsx'
]
);
shepherd()->dispatch( $email );// Send email in 1 hour
$email = new Email(
'user@example.com',
'Reminder: Your appointment is tomorrow',
'This is a friendly reminder about your appointment.'
);
shepherd()->dispatch( $email, HOUR_IN_SECONDS );The Email task automatically handles failures and retries. Common scenarios:
- Automatically retries up to 4 times
- Uses 30-second delays between attempts
- Logs each attempt for debugging
- Task fails immediately (no retries for validation errors)
- Error logged for debugging
- Task fails if attachment files don't exist
- Check file paths before dispatching
The Email task fires a WordPress action after successfully sending:
add_action( 'shepherd_{prefix}_email_processed', function( $task ) {
// Track successful email sending
error_log( "Email sent to: {$task->get_args()[0]} with subject: {$task->get_args()[1]}" );
}, 10, 1 );Since the Email task uses wp_mail(), all WordPress email filters apply:
// Override email settings
add_filter( 'wp_mail_from', function() {
return 'noreply@mysite.com';
} );
add_filter( 'wp_mail_from_name', function() {
return 'My Site';
} );Email tasks are automatically logged with these events:
- created: Email task scheduled
- started: Email processing began
- finished: Email sent successfully
- failed: Email failed to send (after all retries)
- rescheduled: Email task rescheduled
- retrying: Retry attempt starting
use StellarWP\Shepherd\Contracts\Logger;
use StellarWP\Shepherd\Provider;
// Get task ID after dispatching
$task_id = shepherd()->get_last_scheduled_task_id();
// Retrieve logs
$logger = Provider::get_container()->get( Logger::class );
$logs = $logger->retrieve_logs( $task_id );
foreach ( $logs as $log ) {
echo "Type: {$log['type']}, Level: {$log['level']}, Date: {$log['date']}";
}$attachments = [];
$file_path = WP_CONTENT_DIR . '/uploads/report.pdf';
if ( file_exists( $file_path ) ) {
$attachments[] = $file_path;
}
$email = new Email( $to, $subject, $body, [], $attachments );$headers = [
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_option( 'admin_email' ),
'Reply-To: noreply@' . parse_url( home_url(), PHP_URL_HOST ),
];Be mindful of attachment sizes - large files may cause memory issues or timeouts.
You can extend the Email task to create your own tasks.
- Check WordPress email configuration
- Verify SMTP settings if using SMTP plugin
- Check email logs in Action Scheduler
- Review Shepherd task logs
- Verify file paths are absolute
- Check file permissions
- Ensure files exist before dispatching
- Avoid large attachments in high-volume scenarios
- Consider using external email services for bulk emails
- Monitor Action Scheduler queue length