PHP Mail Additional Parameters πŸ“§

beginner
10 min

PHP Mail Additional Parameters πŸ“§

Welcome back to CodeYourCraft! Today, we're diving into the world of PHP Mail with additional parameters. If you're new to PHP, don't worry! We'll start from the ground up and gradually introduce you to the more advanced concepts.

What is PHP Mail? πŸ’‘

PHP Mail is a built-in function in PHP that allows you to send emails from your server. It's a powerful tool for communication in web development, be it for notifications, password recovery, or transactional emails.

Sending a Basic Email πŸ“

Let's start with a simple example:

php
<?php $to = "recipient@example.com"; $subject = "Hello World"; $message = "This is a test email sent using PHP."; $headers = "From: sender@example.com" . "\r\n"; if (mail($to, $subject, $message, $headers)) { echo "Email sent successfully!"; } else { echo "Failed to send email."; } ?>

In this example, we're sending an email to recipient@example.com with the subject "Hello World" and the message "This is a test email sent using PHP." The email is sent from sender@example.com.

Additional Parameters 🎯

Now, let's explore some additional parameters you can use with the mail() function to customize your emails even further.

Content Type πŸ“

The content type determines the MIME type of the email body. By default, it's set to text/plain, but you can change it to text/html for HTML emails:

php
$headers = "Content-type: text/html; charset=UTF-8" . "\r\n";

Reply-to Address πŸ’‘

You can set a reply-to address to ensure that responses from the recipient are sent to the correct email address:

php
$headers .= "Reply-To: reply@example.com" . "\r\n";

CC and BCC πŸ’‘

You can also add CC (carbon copy) and BCC (blind carbon copy) recipients to an email:

php
$cc = "cc@example.com"; $bcc = "bcc@example.com"; $headers .= "Cc: $cc" . "\r\n"; $headers .= "Bcc: $bcc" . "\r\n";

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `mail()` function do in PHP?

Practice Time! βœ…

Now it's time for you to put your newfound knowledge into practice! Create a simple PHP script that sends an email with an HTML body, a reply-to address, and CC and BCC recipients.

Remember, practice makes perfect! Don't be afraid to experiment and ask questions. Happy coding! 😊