Skip to content
PHP Logo

How can the PHP code be modified to ensure that emails are sent to both the sender and recipient?

To ensure that emails are sent to both the sender and recipient, you can modify the PHP code to include both email addresses in the "to" field of the email headers. This way, the email will be sent to both addresses simultaneously.

php
// Set the recipient and sender email addresses
$recipient_email = 'recipient@example.com';
$sender_email = 'sender@example.com';

// Set the email subject and message
$subject = 'Email Subject';
$message = 'This is a test email message.';

// Set the additional headers for sender and recipient
$headers = 'From: ' . $sender_email . "\r\n" .
           'Reply-To: ' . $sender_email . "\r\n" .
           'CC: ' . $recipient_email;

// Send the email to both sender and recipient
$mail_sent = mail($recipient_email, $subject, $message, $headers);

if ($mail_sent) {
    echo 'Email sent successfully to both sender and recipient.';
} else {
    echo 'Email could not be sent.';
}