SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
PHP Sending Emails
Krishna priya
April 21, 2011
C-DAC, Hyderabad
Sending Emails
PHP must be configured correctly in the php.ini file with the details of how
your system sends email. PHP makes use of mail() function to send an
email
Syntax: mail( to, subject, message, headers, parameters );
Parameter

Description

to

Required. Specifies the receiver / receivers of the email

subject

Required. Specifies the subject of the email.

message

Required. Defines the message to be sent. Each line should be separated
with a (n).

headers

Optional. Specifies additional headers, like From, Cc, and Bcc. The
additional headers should be separated with a CRLF (rn)

parameters

Optional. Specifies an additional parameter to the sendmail program
Debugging and Error Handling
General Programming Errors
1) Syntax errors 2)Runtime errors 3) Logical
errors
Syntactical errors are the most common and the easiest to
fix. You'll see them if, for example, omit a semicolon. PHP
will show an error, including the line PHP thinks it's on.
This type of error stops the execution of the php script.
Also, SQL errors are normally a matter of syntax.

Parse error: syntax error, unexpected T_VARIABLE in
C:serverwwwfile_name.php on line 6
Runtime errors
Run-time errors include those things that don't stop a PHP
script from executing but do stop the script from doing
everything it was supposed to do.
This occurs when, for example, it is called a function using
the wrong number or types of parameters.
Missing argument 1 for function_name(), called in
C:serverwwwfile_name.php on line 6 and defined in
C:serverwwwfile_name.php on line 5
Logical errors
Logical errors are actually the worst,
because PHP won't necessarily report it to
you. These are outand-out bugs: problems
that aren't obvious and don't stop the
execution of a script.
PHP Error Handling
There are four type of errors are present in PHP
1-Notices : These are trivial, non-critical errors. that does not
terminate script .
Condition: Accessing a variable that not define
2-Warning : These are more serious errors
Condition:attempting to include() a file which does not exist.
3-Fatal errors (Runtime errors)
These are critical errors that terminate script and stop
Condition:1- Calling a non-existent function
2-Missing semicolon 3-missing braces
4- Parse Error (Syntax errors)
When we make mistake in PHP code like, missing semicolon or any
unexpected symbol in code
PHP Error Handling (con’t)
Following are the different error handling
methods:
1.
2.
3.

Simple "die()" statements
Custom errors and error triggers
Error reporting
Basic Error Handling: Using the
die() function
simple script that opens a text file
<?php
$file=fopen("welcome.txt","r");
?>

If the file doesnot exist youmight get an error like this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in C:webfoldertest.php on line 2
To avoid that the user gets an error message
like the one above, we test if
the file exist before we try to access it:

9.

<?php
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}

10.

?>

1.
2.
3.
4.
5.
6.
7.
8.

File not found
Creating a Custom Error Handler
Creating a custom error handler is quite simple.
We simply create a special function that can be
called when an error occurs in PHP.
This function must be able to handle a minimum
of two parameters (error level and error
message) but can accept up to five parameters
(optionally: file, line-number, and the error
context):
Custom Error Handler
Syntax:error_function(error_level,error_message,
error_file,error_line,error_context)
Parameter

Description

error_level

Required. Specifies the error report
level for the user-defined error. Must
be a value number. See table below
for possible error report levels

error_message

Required. Specifies the error
message for the user-defined error

error_file

Optional. Specifies the filename in
which the error occurred

error_line

Optional. Specifies the line number in
which the error occurred

error_context

Optional. Specifies an array
containing every variable, and their
values, in use when the error
occurred
Example
1.
2.
3.
4.
5.
6.

7.
8.

9.
10.
11.

<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
?>

Out put:
Error: [8] Undefined variable: test
Trigger an Error
In a script where users can input data it is useful to trigger
errors when an illegal input occurs. In PHP, this is done by
the trigger_error() function.
In this example an error occurs if the "test" variable is
bigger than "1":
1.
2.
3.
4.
5.
6.
7.

<?php
Notice: Value must be 1 or below
$test=2;
in C:webfoldertest.php on line 6
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?>
Send an Error Message by E-Mail
1.
2.
3.
4.
5.
6.
7.
8.
9.

By using the error_log() function you can send error logs to
a specified file or a remote destination.
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br />";
echo "Webmaster has been notified";
error_log("Error: [$errno] $errstr",1,
"someone@example.com","From:
webmaster@example.com");
}
Generating an XML Document
XML is a markup language that looks a lot like HTML. An XML
document is plain text and contains tags delimited by < and
>.There are two big differences between XML and HTML:
XML doesn't define a specific set of tags you must use.

HTML list that's not valid XML:
<ul>
<li>Braised Sea Cucumber
<li>Baked Giblets with Salt
<li>Abalone with Marrow
and Duck Feet
</ul>

HTML list that is valid XML:
<ul>
<li>Braised Sea Cucumber</li>
<li>Baked Giblets with Salt</li>
<li>Abalone with Marrow
and Duck Feet</li>
</ul>
PHP XML DOM
The built-in DOM parser makes it possible to
process XML documents in PHP.
What is DOM?
The W3C DOM provides a standard set of objects
for HTML and XML documents, and a standard
interface for accessing and manipulating them.
*Core DOM - defines a standard set of objects for
any structured document
* XML DOM - defines a standard set of objects for
XML documents
* HTML DOM - defines a standard set of objects
for HTML documents
Parsing an XML Document
To read and update - create and manipulate - an XML document,
you will need an XML parser.
There are two basic types of XML parsers:
1.
Tree-based parser: This parser transforms an XML document
into a tree structure. It analyzes the whole document, and
provides access to the tree elements
2.
Event-based parser: Views an XML document as a series of
events. When a specific
The DOM parser is an tree-based parser.
To create a SimpleXML object from an XML document stored in a
string, pass the string to simplexml_load_string( ). It returns
a SimpleXML object.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

PHP file handling
PHP file handling PHP file handling
PHP file handling
 
Lesson 2 php data types
Lesson 2   php data typesLesson 2   php data types
Lesson 2 php data types
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
 
Html media
Html mediaHtml media
Html media
 
Php forms
Php formsPhp forms
Php forms
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
CSS Positioning Elements.pdf
CSS Positioning Elements.pdfCSS Positioning Elements.pdf
CSS Positioning Elements.pdf
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
 
ASP.NET Basics
ASP.NET Basics ASP.NET Basics
ASP.NET Basics
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
html5.ppt
html5.ppthtml5.ppt
html5.ppt
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Html and Xhtml
Html and XhtmlHtml and Xhtml
Html and Xhtml
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 

Andere mochten auch (9)

MySQL Record Operations
MySQL Record OperationsMySQL Record Operations
MySQL Record Operations
 
mySQL and Relational Databases
mySQL and Relational DatabasesmySQL and Relational Databases
mySQL and Relational Databases
 
Introduction to Active Record at MySQL Conference 2007
Introduction to Active Record at MySQL Conference 2007Introduction to Active Record at MySQL Conference 2007
Introduction to Active Record at MySQL Conference 2007
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQL
 
File organization
File organizationFile organization
File organization
 
File organization 1
File organization 1File organization 1
File organization 1
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL Presentation
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and Concepts
 

Ähnlich wie Sending emails through PHP

Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debugging
salissal
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
Swanand Pol
 

Ähnlich wie Sending emails through PHP (20)

lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
 
Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debugging
 
Php manish
Php manishPhp manish
Php manish
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
Php advance
Php advancePhp advance
Php advance
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
Introduction to-php
Introduction to-phpIntroduction to-php
Introduction to-php
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Introduction to web and php mysql
Introduction to web and php mysqlIntroduction to web and php mysql
Introduction to web and php mysql
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
 
phptutorial
phptutorialphptutorial
phptutorial
 
phptutorial
phptutorialphptutorial
phptutorial
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 
Dynamic Web Programming
Dynamic Web ProgrammingDynamic Web Programming
Dynamic Web Programming
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 

Mehr von krishnapriya Tadepalli

Mehr von krishnapriya Tadepalli (13)

Web content accessibility
Web content accessibilityWeb content accessibility
Web content accessibility
 
Data visualization tools
Data visualization toolsData visualization tools
Data visualization tools
 
Drupal vs sitecore comparisons
Drupal vs sitecore comparisonsDrupal vs sitecore comparisons
Drupal vs sitecore comparisons
 
Open Source Content Management Systems
Open Source Content Management SystemsOpen Source Content Management Systems
Open Source Content Management Systems
 
My sql vs mongo
My sql vs mongoMy sql vs mongo
My sql vs mongo
 
Node.js
Node.jsNode.js
Node.js
 
Json
JsonJson
Json
 
Comparisons Wiki vs CMS
Comparisons Wiki vs CMSComparisons Wiki vs CMS
Comparisons Wiki vs CMS
 
PHP Making Web Forms
PHP Making Web FormsPHP Making Web Forms
PHP Making Web Forms
 
Using advanced features in joomla
Using advanced features in joomlaUsing advanced features in joomla
Using advanced features in joomla
 
Presentation joomla-introduction
Presentation joomla-introductionPresentation joomla-introduction
Presentation joomla-introduction
 
Making web forms using php
Making web forms using phpMaking web forms using php
Making web forms using php
 
Language enabling
Language enablingLanguage enabling
Language enabling
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Sending emails through PHP

  • 1. PHP Sending Emails Krishna priya April 21, 2011 C-DAC, Hyderabad
  • 2. Sending Emails PHP must be configured correctly in the php.ini file with the details of how your system sends email. PHP makes use of mail() function to send an email Syntax: mail( to, subject, message, headers, parameters ); Parameter Description to Required. Specifies the receiver / receivers of the email subject Required. Specifies the subject of the email. message Required. Defines the message to be sent. Each line should be separated with a (n). headers Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (rn) parameters Optional. Specifies an additional parameter to the sendmail program
  • 3. Debugging and Error Handling General Programming Errors 1) Syntax errors 2)Runtime errors 3) Logical errors Syntactical errors are the most common and the easiest to fix. You'll see them if, for example, omit a semicolon. PHP will show an error, including the line PHP thinks it's on. This type of error stops the execution of the php script. Also, SQL errors are normally a matter of syntax. Parse error: syntax error, unexpected T_VARIABLE in C:serverwwwfile_name.php on line 6
  • 4. Runtime errors Run-time errors include those things that don't stop a PHP script from executing but do stop the script from doing everything it was supposed to do. This occurs when, for example, it is called a function using the wrong number or types of parameters. Missing argument 1 for function_name(), called in C:serverwwwfile_name.php on line 6 and defined in C:serverwwwfile_name.php on line 5
  • 5. Logical errors Logical errors are actually the worst, because PHP won't necessarily report it to you. These are outand-out bugs: problems that aren't obvious and don't stop the execution of a script.
  • 6. PHP Error Handling There are four type of errors are present in PHP 1-Notices : These are trivial, non-critical errors. that does not terminate script . Condition: Accessing a variable that not define 2-Warning : These are more serious errors Condition:attempting to include() a file which does not exist. 3-Fatal errors (Runtime errors) These are critical errors that terminate script and stop Condition:1- Calling a non-existent function 2-Missing semicolon 3-missing braces 4- Parse Error (Syntax errors) When we make mistake in PHP code like, missing semicolon or any unexpected symbol in code
  • 7. PHP Error Handling (con’t) Following are the different error handling methods: 1. 2. 3. Simple "die()" statements Custom errors and error triggers Error reporting
  • 8. Basic Error Handling: Using the die() function simple script that opens a text file <?php $file=fopen("welcome.txt","r"); ?> If the file doesnot exist youmight get an error like this: Warning: fopen(welcome.txt) [function.fopen]: failed to open stream: No such file or directory in C:webfoldertest.php on line 2
  • 9. To avoid that the user gets an error message like the one above, we test if the file exist before we try to access it: 9. <?php if(!file_exists("welcome.txt")) { die("File not found"); } else { $file=fopen("welcome.txt","r"); } 10. ?> 1. 2. 3. 4. 5. 6. 7. 8. File not found
  • 10. Creating a Custom Error Handler Creating a custom error handler is quite simple. We simply create a special function that can be called when an error occurs in PHP. This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context):
  • 11. Custom Error Handler Syntax:error_function(error_level,error_message, error_file,error_line,error_context) Parameter Description error_level Required. Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels error_message Required. Specifies the error message for the user-defined error error_file Optional. Specifies the filename in which the error occurred error_line Optional. Specifies the line number in which the error occurred error_context Optional. Specifies an array containing every variable, and their values, in use when the error occurred
  • 12. Example 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. <?php //error handler function function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr"; } //set error handler set_error_handler("customError"); //trigger error echo($test); ?> Out put: Error: [8] Undefined variable: test
  • 13. Trigger an Error In a script where users can input data it is useful to trigger errors when an illegal input occurs. In PHP, this is done by the trigger_error() function. In this example an error occurs if the "test" variable is bigger than "1": 1. 2. 3. 4. 5. 6. 7. <?php Notice: Value must be 1 or below $test=2; in C:webfoldertest.php on line 6 if ($test>1) { trigger_error("Value must be 1 or below"); } ?>
  • 14. Send an Error Message by E-Mail 1. 2. 3. 4. 5. 6. 7. 8. 9. By using the error_log() function you can send error logs to a specified file or a remote destination. <?php //error handler function function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br />"; echo "Webmaster has been notified"; error_log("Error: [$errno] $errstr",1, "someone@example.com","From: webmaster@example.com"); }
  • 15. Generating an XML Document XML is a markup language that looks a lot like HTML. An XML document is plain text and contains tags delimited by < and >.There are two big differences between XML and HTML: XML doesn't define a specific set of tags you must use. HTML list that's not valid XML: <ul> <li>Braised Sea Cucumber <li>Baked Giblets with Salt <li>Abalone with Marrow and Duck Feet </ul> HTML list that is valid XML: <ul> <li>Braised Sea Cucumber</li> <li>Baked Giblets with Salt</li> <li>Abalone with Marrow and Duck Feet</li> </ul>
  • 16. PHP XML DOM The built-in DOM parser makes it possible to process XML documents in PHP. What is DOM? The W3C DOM provides a standard set of objects for HTML and XML documents, and a standard interface for accessing and manipulating them. *Core DOM - defines a standard set of objects for any structured document * XML DOM - defines a standard set of objects for XML documents * HTML DOM - defines a standard set of objects for HTML documents
  • 17. Parsing an XML Document To read and update - create and manipulate - an XML document, you will need an XML parser. There are two basic types of XML parsers: 1. Tree-based parser: This parser transforms an XML document into a tree structure. It analyzes the whole document, and provides access to the tree elements 2. Event-based parser: Views an XML document as a series of events. When a specific The DOM parser is an tree-based parser. To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.