How to send e-mail with php?

Member

by rollin , in category: PHP General , 6 months ago

How to send e-mail with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , a month ago

@rollin 

To send an email with PHP, you can use the mail() function. Here's an example code snippet that demonstrates how to send an email with PHP:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$to = "[email protected]";
$subject = "Test Email";
$message = "This is a test email.";

// Additional headers
$headers = "From: [email protected]
";
$headers .= "Reply-To: [email protected]
";
$headers .= "X-Mailer: PHP/" . phpversion();

// Send the email
$mail_sent = mail($to, $subject, $message, $headers);

if ($mail_sent) {
    echo "Email sent successfully.";
} else {
    echo "Email sending failed.";
}


In this example, you need to replace [email protected] and [email protected] with the actual email addresses of the recipient and sender. You can also customize the subject and message content of the email.


Please note that the mail() function requires a properly configured SMTP server to send emails. If you encounter any issues with sending emails using the mail() function, you may need to configure your PHP settings or use a third-party email service provider for sending emails.