How to send mail using simple php script


 

Sending an email using a simple PHP script is relatively straightforward. You can use the mail() function in PHP to send email messages. Here's a basic example of how to send an email using a simple PHP script:

php
<?php // Set the recipient's email address $to = "[email protected]"; // Set the subject of the email $subject = "Hello from Your Website"; // Set the message content $message = "This is a test email sent from a PHP script."; // Set additional headers $headers = "From: [email protected]\r\n"; $headers .= "Reply-To: [email protected]\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; // Send the email $mailSent = mail($to, $subject, $message, $headers); // Check if the email was sent successfully if ($mailSent) { echo "Email sent successfully!"; } else { echo "Email could not be sent. Please check your email settings."; } ?>

In this script:

  1. Set the recipient's email address ($to) to the email address where you want to send the message.

  2. Set the subject of the email ($subject) to describe the purpose of the email.

  3. Set the content of the message ($message) with the text you want to send in the email.

  4. Define additional headers ($headers) to specify the sender's email address and set the content type to plain text. You can customize these headers as needed.

  5. Use the mail() function to send the email. This function takes four parameters: recipient's email address, subject, message, and headers.

  6. Check if the email was sent successfully using a conditional statement. The mail() function returns true if the email was sent successfully and false if there was an issue.

Please note that the mail() function relies on the email configuration of your web server. Ensure that your server is properly configured to send emails. In a production environment, you may also consider using a more robust email library, such as PHPMailer, for sending emails with better features and reliability.

Viewers
Read Also

No comments:

Post a Comment

SEARCH