90% Discount Hostinger Coupon 1CODERX53
896+ Capstone Project is Available: Whatsapp: +91-7011258995, Email: sharecodepoint@gmail.com

Send Email In PHP using PHP Function, Simple E-Mail and Mail Form

Email is the most popular Internet service today. Plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP. So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email.


Note that to send an email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your PHP file to point to your email server.

PHP allows you to send e-mails directly from a script.

The PHP mail() Function 

The PHP mail() function is used to send emails from inside a script.

Syntax

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

ParameterDescription
toRequired. Specifies the receiver / receivers of the email
subjectRequired. Specifies the subject of the email.
Note: This parameter cannot contain any newline characters
messageRequired. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
headersOptional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
parametersOptional. Specifies an additional parameter to the sendmail program

Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the PHP file.

PHP Simple E-Mail 

The simplest way to send an email with PHP is to send a text email. 

In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:

<?php
$to = "emailexample@sharecodepoint.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@abc.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers))
{
echo "Mail Sent.";
}else{
echo "Unable to send email";
}
?>

PHP Mail Form 

With PHP, you can create a feedback form on your website. The example below sends a text message to a specified e-mail address:

<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", $subject,
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text'><br>
  Subject: <input name='subject' type='text'><br>
  Message:<br>
  <textarea name='message' rows='15' cols='40'>
  </textarea><br>
  <input type='submit'>
  </form>";
  }
?>

</body>
</html>

This is how the example above works: 

  • First, check if the email input field is filled out 
  • If it is not set (like when the page is first visited); output the HTML form 
  • If it is set (after the form is filled out); send the email from the form 
  • When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email

Note: This is the simplest way to send e-mail, but it is not secure.

Sharecodepoint

Sharecodepoint is the junction where every essential thing is shared for college students in the well-defined packets of codes. We are focused on providing you the best material package like Question papers, MCQ'S, and one NIGHT STUDY MATERIAL. facebook twitter youtube instagram

Post a Comment

Previous Post Next Post

Contact Form