Mailing is a very important functionality in any web application, I’ll share how to send mail using PHP mail() function.PHP mail()
function is used to send mail from the web server. You can send mail as plain text or with HTML element.
You can add HTML content and CSS styles with the message body of the mail. I am sharing a separate article for sending HTML content emails using PHP.
PHP mail()
function has features to send mail to multiple recipients using php email headers. You can add in mail header like From
, Cc
, and Bcc
using php mail headers.
You can also check other tutorial of php mail,
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
mail()
function Parameters are:Return Type: This method returns TRUE if mail was sent successfully and FALSE on Failure.
The behavior of the php mail functions is affected by settings in php.ini
, You can set the below configuration into the php.ini
file.
Name | Default | Description | Changeable |
---|---|---|---|
mail.add_x_header | “0” | Add X-PHP-Originating-Script that will include UID of the script followed by the filename. For PHP 5.3.0 and above | PHP_INI_PERDIR |
mail.log | NULL | The path to a log file that will log all mail() calls. Log include full path of script, line number, To address and headers. For PHP 5.3.0 and above | PHP_INI_PERDIR |
SMTP | “localhost” | Windows only: The DNS name or IP address of the SMTP server | PHP_INI_ALL |
smtp_port | “25” | Windows only: The SMTP port number. For PHP 4.3.0 and above | PHP_INI_ALL |
sendmail_from | NULL | Windows only: Specifies the “from” address to be used when sending mail from mail() | PHP_INI_ALL |
sendmail_path | “/usr/sbin/sendmail -t -i” | Specifies where the sendmail program can be found. This directive works also under Windows. If set, SMTP, smtp_port and sendmail_from are ignored | PHP_INI_SYSTEM |
<?php $to = "phpflow@gmail.com"; $sub = "Sample Mail"; $msg="Hello Geek! This is a sample email."; if (mail($to,$sub,$msg)) echo "Your Mail is sent successfully."; else echo "Your Mail is not sent. Try Again."; ?>
Output:
Your Mail is sent successfully.
<?php $to = "parxx@gmail.com"; $subject = "parxx"; $body = "Body of your message here you can use HTML too. e.g. <b> Bold "; $headers = "From: Peterrn"; $headers .= "Reply-To: info@yoursite.comrn"; $headers .= "Return-Path: info@yoursite.comrn"; $headers .= "X-Mailer: PHP5n"; $headers .= ‘MIME-Version: 1.0′ . "n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "rn"; if(mail($to,$subject,$body,$headers)) echo "Your Mail is sent successfully."; else echo "Your Mail is not sent. Try Again."; ?>
Output:
Your Mail is sent successfully.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More