Send Email in Php

Wikitechy | 4181 Views | php | 13 Jun 2016

 

  • The mail() function is used to send emails directly from a script.
  • For the mail functions to be available in PHP, it requires an installed and working email system where the program to be used is defined by the configuration settings in the php.ini file.

Example1 

Script for sending mail:

<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('wikitechy@example.com', 'My Subject', $message);
?>

Example2 

Script for Sending mail with extra headers:

The addition of basic headers, tells the MUA the from and Reply-To addresses.

Syntax:

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

Parameter

Description

to

Mandatory. Define the receiver / receiver of the email

subject

Mandatory. Specifies the subject of the email and in this subject we cannot define any new line.

Message

Mandatory. Defines the message to be sent. Each line should be separated with a   LF - line feed (\n). Lines should not exceed 70 characters.

headers

Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).

parameters

Optional. Specifies an additional parameter to the send mail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address while using sendmail with the -f sendmail option)

 












Sample Script for sending mail using mail () function:

<?php
$to      = 'venkat@example.com';
$subject = 'the subject';
$message = 'welcome to wikitechy';
$headers = 'From: wikitechy@example.com' . "\r\n" .
    'Reply-To: wikitech@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Example3:

Sending mail with an additional command line parameter

  • The additional_parameters parameter can be used to pass an additional parameter for the program configured to use while sending mail using the sendmail_path.
<?php
mail('venkat@example.com', 'the subject', 'the message', null,
   '-fwebmaster@example.com');
?>

Example4:

Sending HTML email

In php using the mail () function we can send a HTML email 

<?php
// multiple recipients
$to  = 'jln@example.com' . ', '; // note the comma
$to .= 'arun@example.com';
// subject
$subject = 'Wikitechy Event Meet';
// message
$message = '
<html>
    <head>
        <title>
Wikitechy Event Meet</title>
    </head>
<body>
    <p>
Here are the Wikitechy upcoming event meet in November</p>
    <table>
        <tr>
            <th>
Place</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
            <td>
Bangalore</td><td>3rd</td><td> November </td><td>2016</td>
        </tr>
        <tr>
            <td>
Chennai</td><td>17th</td><td> November </td><td>2016</td>
        </tr>
    </table>
</body>
</html>

';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: kavi <kavi@wikitechy.com>, arunprasath < arunprasath@wikitechy.com>' . "\r\n";
$headers .= 'From: Event Remainder <event@wikitechy.com>' . "\r\n";
$headers .= 'Cc: eventm@wikitechy.com ' . "\r\n";
$headers .= 'Bcc: eventc@wikitechy.com ' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>

Applies to:

  • PHP 3 and 4
  • PHP 5
  • PHP 6 and Unicode
  • PHP 7

Related tags:

  • PHP mail() Function
  • Sending Emails using PHP 
  • Send mail - PHP
  • Basic PHP mail() Function code to send emails from
  • How to send an email using PHP? 
  • Sending Nice HTML Email with PHP 
  • PHP mail()
  • send email in php using smtp
  • send html email in php
  • send email in php code
  • send email in php code with form




Workshop

Bug Bounty
Webinar

Join our Community

Advertise
<