Sending Emails using PHP

Sending emails using php can be done by the mail() function.

If you are looking for codeigniter mail function please click here.

In this article we will discuss sending emails using php. It can be done by the php mail() function. The mail() function requires three mandatory parameters: to address, subject of mail and the message . The additional parameters are headers and parameters.

PHP : mail.php

<?php
$to = "xxxxx@xxx.xxx";
$subject = "HTML email";
$from = 'xxxxx@xxx.xxx';
$message = '
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>30</td>
</tr>
</table>
</body>
</html>
';

// More headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
// Always set content-type when sending HTML email
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " .$from."\r\n"."X-Mailer: php";

mail($to,$subject,$message,$headers);
?>
  1. declare to address, subject, from address and message
  2. MIME Version (Multi-Purpose Internet Mail Extensions) – Defines version of the MIME protocol. It must have the parameter Value 1.0, which indicates that message is formatted using MIME.
  3. Content-type – the content type of email body. There are different types like text, html, video, audio
  4. send mail using the mail() function and pass parameters to, subject, message and headers.

Refer php mail function for more details.

Related Posts

forest fire

Forest Fire – Techgig, All testcases passed. Challenge #1

Forest Fire (100 Marks) Challenge 1: Forest fire You are camping in a forest area at night. You are living with the forest officers to experience their challenges…

the magic wand

The Magic Wand – TechGig PHP answer, all test cases passed. Challenge #2

The Magic Wand – TechGig PHP answer – all testcases passed The Magic Wand (100 Marks) You are a wizard who possesses the magic wand that can be…

PHP interview Questions and Answers

What is PHP? Answer: PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP code is embedded within…

install php

A Step-by-Step Guide on How to Install PHP

Install PHP : A Step-by-Step Guide on How to Install PHP Introduction: PHP is a widely used server-side scripting language that powers countless websites and web applications….

what is php

What is PHP? Understanding PHP: The Powerhouse of Web Development

Introduction: What is PHP? Understanding PHP: The Powerhouse of Web Development: In the world of web development, PHP stands tall as one of the most widely-used and…

php form

Complete Php form example

Here we are going to check how to create a php form and insert its values to database table. In this example we will be submitting the…

This Post Has One Comment

Leave a Reply

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