Php

How to Send Mail Using PHP 7

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.

What’s mail() function in 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,

Related Post

The syntax:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Where mail() function Parameters are:

  • to: This is used for the Receiver’s email id or receiver’s email ids of the mail.
  • subject: Subject of the email to be sent.
  • message: Message to be sent.
  • additional_headers: This will use to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

Return Type: This method returns TRUE if mail was sent successfully and FALSE on Failure.

Runtime Configuration of PHP mail

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

Example 1: Sending mail Using PHP

<?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.

Example 2: Sending Mail Using PHP with Extra Parameters:

<?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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

4 months ago

What is the Purpose of php_eol in PHP?

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

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories