How to send an email with PHP
Sending an email with PHP is a common task that can be easily accomplished using the built-in mail() function.
Here’s a basic example:
$to = "recipient@example.com"; $subject = "Test email"; $message = "This is a test email"; $headers = "From: sender@example.com"; mail($to, $subject, $message, $headers);
In the above example, we define the recipient email address in the $to variable, the subject of the email in the $subject variable, and the message body in the $message variable. We also specify the sender’s email address in the $headers variable.
Once we have these variables defined, we simply call the mail() function, passing in the appropriate arguments. The function will then send the email.
Here’s another example that includes additional headers:
$to = "recipient@example.com"; $subject = "Test email"; $message = "This is a test email"; $headers = "From: sender@example.comrn"; $headers .= "Reply-To: sender@example.comrn"; $headers .= "Cc: another@example.comrn"; $headers .= "Bcc: hidden@example.comrn"; mail($to, $subject, $message, $headers);
In this example, we’re including the sender’s email address in the From header, as well as specifying a Reply-To email address, and including additional recipients in the Cc and Bcc headers.
For more information about the mail() function and its options, see the official PHP documentation.
Written by: Maria Jensen 17-06-2023 Written in: PHP tutorials
I found this tutorial really helpful! The step-by-step instructions made it easy to understand how to send an email with PHP . However, I did run into a small issue with the code provided. It would have been great if the author had mentioned how to handle attachments in the email. Overall, though, it’s a great resource for beginners like me. Keep up the good work!
This tutorial was so helpful! 😊 I’ve always struggled with sending emails using PHP, but this step-by-step guide made it super easy to understand . The code examples were perfect, and the explanations were clear and concise. Really appreciate the effort put into making this tutorial beginner-friendly. Can’t wait to try this out on my own! 👍
This tutorial was super helpful! I was struggling to figure out how to send emails with PHP, but this step-by-step explanation made it so much easier for me . Thank you so much for breaking it down in a way that’s easy to understand. Awesome job!
“This tutorial was super helpful for me! I had no idea how to send an email using PHP before, but now I feel confident enough to incorporate it into my projects . Thank you for breaking it down in such a clear and understandable way.”
This tutorial was super helpful! I was always confused about how to send emails with PHP, but now I feel confident to give it a try . Thanks for the clear explanations and step-by-step guide!
This tutorial was really helpful! I was struggling to figure out how to send emails with PHP, but this step-by-step guide made it so much easier . Thank you!
This tutorial was super helpful! I had been struggling with sending emails through PHP, but this guide made it so easy to understand . Thank you for breaking it down step by step!