How to send email without smtp in PHP?

by tressie.damore , in category: PHP General , 9 months ago

How to send email without smtp in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 3 months ago

@tressie.damore 

To send an email without using SMTP in PHP, you can use the mail function.


Here is a simple example of how to use the mail function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
$to = "recipient@example.com";
$subject = "Test email using PHP mail function";
$message = "This is a test message.";
$headers = "From: sender@example.com";

if (mail($to, $subject, $message, $headers)) {
    echo "Email was sent successfully.";
} else {
    echo "There was an error sending the email.";
}
?>


This example will send an email to "recipient@example.com" with the subject "Test email using PHP mail function" and the message "This is a test message." The email will appear to be sent from "sender@example.com".


Keep in mind that the mail function may not work on all servers, and it may be necessary to use an SMTP server to send emails. Additionally, emails sent using the mail function may be more likely to be flagged as spam by email providers.