Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Sending Emails

PHP mails:

PHP mail is the built in PHP function that is used to send emails from PHP scripts.

Syntax:

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

 

The mail function accepts the following parameters;

  • Email address ($to)

It specifies the receiver mail address.

  • Subject($subject)

Represents the subject of the mail.

  • Message($message)

Represents message of the mail to be sent.

  • CC or BC email addresses($additional_headers)

It is optional.It specifies the additional headers such as From, CC, BCC etc.

 Key points:

  1. It’s a cost effective way of notifying users on important events.
  2. It allows users contact you via email by providing a contact us form on the website that emails the provided content.
  3. Developers can use it to receive system errors by email.
  4. You can use it to email your newsletter subscribers.
  5. You can use it to send password reset links to users who forget their passwords.
  6. You can use it to email activation or confirmation links. This is useful when registering users and verifying         their email addresses.

 

PHP Mail Example

Code:
<?php
ini_set("sendmail_from", "[email protected]");
$to = "[email protected]";//change receiver address
$subject = "Welcome letter";
$message = "Welcome to our company";
$header = "From:[email protected] \r\n";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...!";
}else{
echo "Unable to send mail.";
}
?>

Note:

Remember this code will not work on the local server.
In order to run this code,you have to set up on the live server.
It will send an email to the specified receiver mail address.

 

Sending mail with attachment:

From the above few lines of code, you probably know how to send email with PHP, but it gets some tricky when you want to send an attachment with PHP email. So, today we will teach you how to send  email with an attachment using PHP mail.

Here’s the complete PHP code that sends PHP email with attachment. Just create a PHP file with code below and point your HTML form to the file, and don’t forget to change recipient address and email address in the code.

Code:
<html>
<head>
<title>Sending attachment using PHP</title>
</head>
<body>
<?php
$to = "[email protected]";
$subject = "This is subject";
$message = "This is test message.";
# Open a file
$file = fopen( "/tmp/test.txt", "r" );
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/tmp/test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));

# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:[email protected]\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject, "", $header );
if( $retval == true )
{
echo "Mail sent successfully..";
}
else
{
echo "Unable to sent mail.";
}
?>
</body>
</html>


Sending Email to Multiple Recipients:

You can send email to the multiple recipients at once with Cc and Bcc.

For this we can use  Cc and Bcc headers for sending email with attachment to multiple recipients at once in PHP.

Code:
<?php
// Cc email
$headers .= "\nCc: [email protected]";

// Bcc email
$headers .= "\nBcc: [email protected]";

ASSIGNMENT : Write a PHP scripts for sending mail with attachment. MARKS : 10  DURATION : 10 minutes

ASSIGNMENT : Write down the syntax for sending mails in PHP. MARKS : 5  DURATION : 5 minutes

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X