PHP provides a built-in `mail()` function that you can use to send emails directly from your scripts. This function is very useful, but there are some intricacies to it, and its simplicity means that for more advanced features you might want to use dedicated libraries. Let's dive in:
Before you start sending emails using the `mail()` function, ensure that your PHP environment is configured correctly to send emails. If you're using a local environment, you might need to set up a mail server or use SMTP. Your PHP configuration (usually in `php.ini`) should have these entries correctly set:
ini
[mail function]
; For Win32 only.
SMTP = mail.yourisp.com
smtp_port = 25
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On
Here's a simple example of how you can use the `mail()` function:
<?php
$to = "recipient@example.com";
$subject = "Test Mail";
$message = "This is a test email.";
$headers = "From: sender@example.com";
if(mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
?>
For a more sophisticated email, you can add additional headers:
$headers = "From: Sender Name \r\n";
$headers .= "Reply-To: reply@example.com\r\n";
$headers .= "CC: cc@example.com\r\n";
$headers .= "BCC: bcc@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
If you use the above MIME declaration, you can send HTML formatted emails:
$message = "<html><body>";
$message .= "Hello, World!";
$message .= "This is an HTML email.";
$message .= "</body>";
While this tutorial focuses on the built-in `mail()` function, it's important to mention alternatives. Here's a basic PHPMailer example:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'username@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Remember that for PHPMailer, you'll need to download or include it in your project via Composer or another method.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here