Sei sulla pagina 1di 22

SENDING EMAILS USING PHP

Sending E-mails
 Sending email messages is very common for a web
application, for example:
 Sending a welcome email when a user create a new account on your
website
 Sending newsletters to your registered users
 Getting user feedback or comment through the website's contact from
and many more

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


Sending E-mails
Php.ini Configuration

Web-based Systems | Misbhauddin 4


Sending Plain Text E-mails
 The simplest way to send an email with PHP is to send a text
email.
<?php
$to = “Ahmed@gmail.com";
$subject = "Welcome to Arid";
$message = "Hi ABC, You are welcome to Join Arid University.";
// Setting Headers
$headers = From: naveed.qamar@hotmail.com ;
if(mail($to, $subject, $message, headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Sending HTML E-mails
 When you send a text e-mail using PHP then all the
content will be treated as simple text. Even if you will
include HTML tags in a text message, it will be
displayed as simple text and HTML tags will not be
formatted according to HTML syntax.
 PHP provides option to send an HTML message as
actual HTML message.

$headers = "Content-type: text/html; charset=UTF-8 \r\n";


Sending HTML E-mails
<?php
$to= "beeedoo@gmail.com";
$subject= "Welcome to Arid";
$message= "Hi XYZ,<br> You are welcome to Join Arid
University.<br>Regards, <br> Arid University";
// Setting Headers
$headers="From:naveed.qamar@hotmail.com”;
$headers.="Content-type: text/html; charset=UTF-8 \r\n";
// Sending email
if( mail($to, $subject, $message,$headers) ) {
echo 'Your mail has been sent successfully.';
} else {
echo 'Unable to send email. Please try again.';
}
?>
Using a Third-Party Library
• PHPMailer
– Probably the world's most popular code for
sending email from PHP!
– Integrated SMTP support
• Download
– https://github.com/Synchro/PHPMailer

Web-Based Systems - Misbhauddin 8


Including PHPMailer
• Use the require_once() command
– include() – [suppresses warnings if file does not exists]
– require() – [warns if file does not exist]
– require_once() – [similar to require but loads file
once]

• The file of concern


– class.phpmailer.php
– class.smtp.php

Web-Based Systems - Misbhauddin 9


Using PHPMailer
• Prerequisite Knowledge
– PHPMailer uses Object-Oriented in PHP
• Steps
– 1. Create a new Object
• $mail = new PHPMailer();
– 2. Access function for the newly created object
using ‘->’
• For example: $mail->ValidateAddress($email);

Web-Based Systems - Misbhauddin 10


Using PHPMailer – To/From

• $mail->From = 'from@example.com';
• $mail->FromName = 'Mailer';

Add a recipient - Name is optional


• $mail->AddAddress('josh@example.net', 'Josh Adams');
• $mail->AddReplyTo('info@example.com',
'Information');
• $mail->AddCC('cc@example.com');
• $mail->AddBCC('bcc@example.com');

Web-Based Systems - Misbhauddin 11


Using PHPMailer – Subject & Body
• $mail->Subject = 'Here is the subject';
• $mail->Body = $message;

If message is HTML
• $mail->IsHTML(true); // Set email format to HTML
• $mail->MsgHTML($message);
• $mail->AltBody = 'This is the body in plain text for non-
HTML mail clients';

Adding Attachments
• $mail->AddAttachment('/var/tmp/vcard.info');

Web-Based Systems - Misbhauddin 12


Using PHPMailer – Confirmation
if(!$mail->Send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';

Web-Based Systems - Misbhauddin 13


SMTP Server
• PHPMailer has its own SMTP Server which may not work on
localhost but will work once deployed on a server
• Other options
– Use a custom SMTP Server (Gmail)
– Steps:
• Define Your Username & Passowrd
– define('GUSER', 'you@gmail.com'); // GMail username
– define('GPWD', 'password'); // GMail password
• Functions
– $mail->IsSMTP(); // enable SMTP
– $mail->SMTPAuth = true; // authentication enabled
– $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
– $mail->Host = 'smtp.gmail.com';
– $mail->Port = 465;
– $mail->Username = GUSER;
– $mail->Password = GPWD;

Web-Based Systems - Misbhauddin 14


Issue with using Gmail Address

Web-Based Systems - Misbhauddin 15


Web-based Systems | Misbhauddin 16
Session Management
Session Management

Session using PHP is a way to store information in variables


that can be used in multiple pages or across the website
during that visit of a user. The session stores information on
the server side unlike cookies, that store information at the
user's computer.
Session Management (Cont.)
 A session creates a file in a temporary directory on the server
where registered session variables and their values are
stored. This data will be available to all pages on the site
during that visit.
 When a session is started following things happen
 PHP first creates a unique identifier for that particular session which is a
random string of 32 hexadecimal numbers such as
3c7foj34c3jj973hjkop2fc937e3443.
 A cookie called PHPSESSID is automatically sent to the user's computer to
store unique session identification string.
 A file is automatically created on the server in the designated temporary
directory and bears the name of the unique identifier prefixed by sess_ ie
sess_3c7foj34c3jj973hjkop2fc937e3443.
Session Management (Cont.)
 A PHP session is easily started by making a call to
the session_start() function. This function first checks if a
session is already started and if none is started then it starts
one.
 Call session_start() at the beginning of the page.
 Session variables are stored in associative array
called $_SESSION[]. These variables can be accessed during
lifetime of a session.

$_SESSION['counter']
Session Management (Cont.)
 A PHP session can be destroyed by session_destroy() function.
This function does not need any argument and a single call can
destroy all the session variables.
 If one want to destroy a single session variable then you can
use unset() function to unset a session variable.

unset($_SESSION['counter']);

session_destroy();
<?php Destroy PHP - Sessions
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION = array();
session_destroy();
echo "<strong>Step 5 - Destroy This Session </strong><br />";
if($_SESSION['name']){
echo "The session is still active";
} else {
echo "Ok, the session is no longer active! <br />";
echo "<a href=\"page1.php\"><< Go Back Step 1</a>";
}
?>

http://www.cs.kent.edu/~nruan/session_destroy.php

Potrebbero piacerti anche