How to send e-mail with php?

Member

by rollin , in category: PHP General , a year ago

How to send e-mail with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 10 months 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 = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

// Additional headers
$headers = "From: sender@example.com
";
$headers .= "Reply-To: sender@example.com
";
$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 recipient@example.com and sender@example.com 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.

Related Threads:

How to send a secure e-mail using smtp?
How to send mail with smtp authentication in php?
How to configure php mail in xampp?
How to send mail using octobercms?
How to send mail through gmail with perl?
How to send smtp mail in cakephp?