PHP mailer PHP Send Email

PHP provides phpmailer features to you. One of the key features of PHP is that you can also send emails via PHP. Email is a simple and effective way to interact with customers.

Through PHP you can send HTML emails but not just text email. HTML code is used to make emails appealing in HTML emails. These types of emails show in your inbox as a webpage.

A live server is required to send email via PHP You can not send email from local host. Before sending an email, you should also check whether your php.ini file is configured to send emails or not.
To send an email, the SMTP server and sendmail_from attributes should be set in the [mail function] section of the php.ini file. The SMTP server tells your email server and sendmail_from tells the email id from which emails will be sent.

You can set these attributes by the ini_set () function. In this function, two arguments are passed, the first argument is the name of the attribute and the second argument is its value.

ini_set(“sendmail_from”,”email@example.com”);

PHPmailer Function – mail()

PHP provides you with built in mail () function to send emails. The general syntax of this function is being given below.

bool mail(string $to, string $subject, string $message, string $additional_header, string $additional parameters)

Five arguments are passed in the PHP mail () function. You can also pass these arguments directly in the mail () function or they can be passed in the mail () function by transferring variables into these variables.

The arguments passed in the PHP mail () function are being explained below.

TO

This is the receiver email id that is being sent to mail.

SUBJECT

This is the subject of the email to be sent.

MESSAGE

This is the message that you want to send via email.

PHPmailer – ADDITIONAL_HEADERS (OPTIONAL)

This argument is used to add additional headers. Additional headers are displayed below for the email header. With additional headers, you pass information such as From, CC and BCC.

ADDITIONAL_PARAMETERS (OPTIONAL)

Additional flags are passed to the program by mail sending this argument.

Example

<html>
<head>
<title>PHP Sending Email</title>
</head>
<body>
<h4>PHP Sending Email</h4>
<form action=”action_file.php” method=”post”>
From : <input type=”text” name=”From”><br /> <br />
To : <input type=”text” name=”To”><br /> <br />
Subject : <input type=”text” name=”Subject”> <br /> <br />
Message : <textarea name=”Message” rows=”4″ cols=”30″></textarea><br /> <br />
<input type=”submit” value=”Submit”>
</form>
</body>
</html> 

action_file.php

<?php
// Defining arguments of mail() function
$from = $_POST[‘From’];
$to = $_POST[‘To’];
$subject = $_POST[‘Subject’];
$message = $_POST[‘Message’];

// Setting attributes in php.ini files
ini_set(“sendmail_from”,$from);

// Sending email & checking if its sent
if(mail($to,$subject,$message))
{
echo “Mail sent successfully.”;
}
else
{
echo “Unable to send email. Some error occurred.”;
}

?>

In the above phpmailer example, the mail () function is called in the if statement. In this example, the variables have been passed by passing values directly to the mail () function. These variables will contain values which will be filled in the user form.

Note: If you try to send an email from the local host, you will be unable to connect mail server error. As I told you before, you need a live server to send an email. So you will need a live server to send email from PHP.