Sending mail in PHP

Mail in PHP

PHP comes with a default function mail() that allows you to send mail directly from a PHP script.  Mail function present in the PHP 4, PHP 5 or PHP 7. PHP configured to send mail.
In case your PHP setup is not configured for sending email to the outside world, then please refer the below steps.

Configuring PHP for sending mail

In order to configure anything related to PHP you need to change `php.ini` file. You can find the file in folder `C:xamppphpphp.ini`

Changing php.ini file to add mail configuration.
  1. Open your php.ini file using notepad in admin node
  2. Search [mail function] in the file. It will be as shown below:
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = localhost
    ; http://php.net/smtp-port
    smtp_port = 25
    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = [email protected]
    ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path = /usr/sbin/sendmail -t -i
    ; Force the addition of the specified parameters to be passed as extra parameters
    ; to the sendmail binary. These parameters will always replace the value of
    ; the 5th parameter to mail(), even in safe mode.
    ;mail.force_extra_parameters =
    ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
    mail.add_x_header = On
    ; The path to a log file that will log all mail() calls. Log entries include
    ; the full path of the script, line number, To address and headers.
    ;mail.log =
  3. Add your mail server details to the file or incase you have one you can change it (mail server can be your own ie. local mail server or you can use any ESP as a mail server).
    For Window:
    – Check for `SMTP = localhost` and change it to your desired mail server (any ESP or localhost) no changes are required if you are using your own local server.
    – Or you can also use the smtp server of any Email Service Provider like Pepipost, Sendgrid, Mailgun, Sparkpost.
  4. Save/close the php.ini file
  5. The final step, don’t forget to restart your webserver/php-fpm.
    You can host a simple “info.php” on your webserver to check each and every configuration of your PHP using below 2 liner code:

    vim php_info.php
    
    <?php
    
    phpinfo();
    
    ?>
  6. Reload you webserver and php-fpm.
  7. Hit http://localhost/php_info.php on your web browser.

Mail in PHP

Syntax

mail (to,subject,message,headers,parameters);
  1. to

    String | Required
    The email address of the recipient.

  2. subject

    String | Required
    The subject line of the email to be sent.
    Note, the subject must comply with RFC 2047.

  3. message

    String | Required
    The content of the mail that you want to send. Each line of the email should be separated with a CRLF (rn) and each line should not exceed 70 characters.

  4. additional_headers

    mixed (String or Array) | Optional
    This parameter is used to pass any additional email headers like From, Cc and Bcc. Each additional headers should be separated with a CRLF (rn)

  5. additional_parameters

    String | Optional
    This additional_parameter can be used to pass additional flags to the Sendmail program as configured in the sendmail_path configuration setting. For example; you can use this parameter to set the envelope sender address when using Sendmail with -f option. PHP by default internally escape the values coming in this parameter with escapeshellcmd() to prevent any potential command execution.

Return Value

mail() function returns TRUE if the SMTP server successfully accepted the mail for delivery, else FALSE.

Getting TRUE doesn’t necessarily mean that the email is delivered to the recipient’s server. TRUE is just an indication that your mail has successfully submitted to the SMTP server’s queue for sending. Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. Sending mail in PHP

How to send an HTML mail

<?php
$to = "[email protected], [email protected]";
$subject = "This is a test HTML email";

$message = "
<html>
<head>
<title>This is a test HTML email</title>
</head>
<body>
<p>Test email. Please ignore.</p>
</body>
</html>
";

// It is mandatory to set the content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";

// More headers. From is required, rest other headers are optional
$headers .= 'From: <[email protected]>' . "rn";
$headers .= 'Cc: [email protected]' . "rn";

mail($to,$subject,$message,$headers);
?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.