Php get email function error



Php get email function error

(PHP 4, PHP 5, PHP 7, PHP 8)

mail — Send mail

Description

Parameters

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User
  • User , Another User

Subject of the email to be sent.

Subject must satisfy » RFC 2047.

Message to be sent.

Each line should be separated with a CRLF (\r\n). Lines should not be larger than 70 characters.

(Windows only) When PHP is talking to a SMTP server directly, if a full stop is found on the start of a line, it is removed. To counter-act this, replace these occurrences with a double dot.

String or array to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.

If an array is passed, its keys are the header names and its values are the respective header values.

Before PHP 5.4.42 and 5.5.27, repectively, additional_headers did not have mail header injection protection. Therefore, users must make sure specified headers are safe and contains headers only. i.e. Never start mail body by putting multiple newlines.

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini .

Failing to do this will result in an error message similar to Warning: mail(): «sendmail_from» not set in php.ini or custom «From:» header missing . The From header sets also Return-Path when sending directly via SMTP (Windows only).

If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

The additional_params parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitize this parameter to avoid adding unwanted parameters to the shell command.

Since escapeshellcmd() is applied automatically, some characters that are allowed as email addresses by internet RFCs cannot be used. mail() can not allow such characters, so in programs where the use of such characters is required, alternative means of sending emails (such as using a framework or a library) is recommended.

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a ‘X-Warning’ header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users .

Return Values

Returns true if the mail was successfully accepted for delivery, false otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Changelog

Version Description
7.2.0 The additional_headers parameter now also accepts an array .

Examples

Example #1 Sending mail.

Using mail() to send a simple email:

// The message
$message = «Line 1\r\nLine 2\r\nLine 3» ;

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap ( $message , 70 , «\r\n» );

// Send
mail ( ‘caffeinated@example.com’ , ‘My Subject’ , $message );
?>

Example #2 Sending mail with extra headers.

The addition of basic headers, telling the MUA the From and Reply-To addresses:

= ‘nobody@example.com’ ;
$subject = ‘the subject’ ;
$message = ‘hello’ ;
$headers = ‘From: webmaster@example.com’ . «\r\n» .
‘Reply-To: webmaster@example.com’ . «\r\n» .
‘X-Mailer: PHP/’ . phpversion ();

mail ( $to , $subject , $message , $headers );
?>

Example #3 Sending mail with extra headers as array

This example sends the same mail as the example immediately above, but passes the additional headers as array (available as of PHP 7.2.0).

= ‘nobody@example.com’ ;
$subject = ‘the subject’ ;
$message = ‘hello’ ;
$headers = array(
‘From’ => ‘webmaster@example.com’ ,
‘Reply-To’ => ‘webmaster@example.com’ ,
‘X-Mailer’ => ‘PHP/’ . phpversion ()
);

mail ( $to , $subject , $message , $headers );
?>

Example #4 Sending mail with an additional command line parameter.

The additional_params parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path .

Example #5 Sending HTML email

It is also possible to send HTML email with mail() .

// Multiple recipients
$to = ‘johny@example.com, sally@example.com’ ; // note the comma

// Subject
$subject = ‘Birthday Reminders for August’ ;

// Message
$message = ‘

Birthday Reminders for August

Источник

PHP Error Handling

Error handling in PHP is simple. An error message with filename, line number and a message describing the error is sent to the browser.

PHP Error Handling

When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks.

This tutorial contains some of the most common error checking methods in PHP.

We will show different error handling methods:

  • Simple «die()» statements
  • Custom errors and error triggers
  • Error reporting

Basic Error Handling: Using the die() function

The first example shows a simple script that opens a text file:

Example

If the file does not exist you might get an error like this:

To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it:

Example

Now if the file does not exist you get an error like this:

The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to stop the script after the error.

However, simply stopping the script is not always the right way to go. Let’s take a look at alternative PHP functions for handling errors.

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):

Syntax

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

Error Report levels

These error report levels are the different types of error the user-defined error handler can be used for:

Value Constant Description
1 E_ERROR A fatal run-time error. Execution of the script is stopped
2 E_WARNING A non-fatal run-time error. Execution of the script is not stopped
8 E_NOTICE A run-time notice. The script found something that might be an error, but could also happen when running a script normally
256 E_USER_ERROR A fatal user-generated error. This is like an E_ERROR, except it is generated by the PHP script using the function trigger_error()
512 E_USER_WARNING A non-fatal user-generated warning. This is like an E_WARNING, except it is generated by the PHP script using the function trigger_error()
1024 E_USER_NOTICE A user-generated notice. This is like an E_NOTICE, except it is generated by the PHP script using the function trigger_error()
2048 E_STRICT Not strictly an error.
8191 E_ALL All errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4)

Now lets create a function to handle errors:

The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script.

Now that we have created an error handling function we need to decide when it should be triggered.

Set Error Handler

The default error handler for PHP is the built in error handler. We are going to make the function above the default error handler for the duration of the script.

It is possible to change the error handler to apply for only some errors, that way the script can handle different errors in different ways. However, in this example we are going to use our custom error handler for all errors:

Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a second parameter could be added to specify an error level.

Example

Testing the error handler by trying to output variable that does not exist:

The output of the code above should be something like this:

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.

Example

In this example an error occurs if the «test» variable is bigger than «1»:

The output of the code above should be something like this:

An error can be triggered anywhere you wish in a script, and by adding a second parameter, you can specify what error level is triggered.

Possible error types:

  • E_USER_ERROR — Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted
  • E_USER_WARNING — Non-fatal user-generated run-time warning. Execution of the script is not halted
  • E_USER_NOTICE — Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally

Example

In this example an E_USER_WARNING occurs if the «test» variable is bigger than «1». If an E_USER_WARNING occurs we will use our custom error handler and end the script:

The output of the code above should be something like this:

Now that we have learned to create our own errors and how to trigger them, lets take a look at error logging.

Error Logging

By default, PHP sends an error log to the server’s logging system or a file, depending on how the error_log configuration is set in the php.ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.

Sending error messages to yourself by e-mail can be a good way of getting notified of specific errors.

Send an Error Message by E-Mail

In the example below we will send an e-mail with an error message and end the script, if a specific error occurs:

The output of the code above should be something like this:

And the mail received from the code above looks like this:

This should not be used with all errors. Regular errors should be logged on the server using the default PHP logging system.

Источник

PHP Error Handling

Error handling in PHP is simple. An error message with filename, line number and a message describing the error is sent to the browser.

PHP Error Handling

When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks.

This tutorial contains some of the most common error checking methods in PHP.

We will show different error handling methods:

  • Simple «die()» statements
  • Custom errors and error triggers
  • Error reporting

Basic Error Handling: Using the die() function

The first example shows a simple script that opens a text file:

Example

If the file does not exist you might get an error like this:

To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it:

Example

Now if the file does not exist you get an error like this:

The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to stop the script after the error.

However, simply stopping the script is not always the right way to go. Let’s take a look at alternative PHP functions for handling errors.

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):

Syntax

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

Error Report levels

These error report levels are the different types of error the user-defined error handler can be used for:

Value Constant Description
1 E_ERROR A fatal run-time error. Execution of the script is stopped
2 E_WARNING A non-fatal run-time error. Execution of the script is not stopped
8 E_NOTICE A run-time notice. The script found something that might be an error, but could also happen when running a script normally
256 E_USER_ERROR A fatal user-generated error. This is like an E_ERROR, except it is generated by the PHP script using the function trigger_error()
512 E_USER_WARNING A non-fatal user-generated warning. This is like an E_WARNING, except it is generated by the PHP script using the function trigger_error()
1024 E_USER_NOTICE A user-generated notice. This is like an E_NOTICE, except it is generated by the PHP script using the function trigger_error()
2048 E_STRICT Not strictly an error.
8191 E_ALL All errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4)

Now lets create a function to handle errors:

The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script.

Now that we have created an error handling function we need to decide when it should be triggered.

Set Error Handler

The default error handler for PHP is the built in error handler. We are going to make the function above the default error handler for the duration of the script.

It is possible to change the error handler to apply for only some errors, that way the script can handle different errors in different ways. However, in this example we are going to use our custom error handler for all errors:

Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a second parameter could be added to specify an error level.

Example

Testing the error handler by trying to output variable that does not exist:

The output of the code above should be something like this:

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.

Example

In this example an error occurs if the «test» variable is bigger than «1»:

The output of the code above should be something like this:

An error can be triggered anywhere you wish in a script, and by adding a second parameter, you can specify what error level is triggered.

Possible error types:

  • E_USER_ERROR — Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted
  • E_USER_WARNING — Non-fatal user-generated run-time warning. Execution of the script is not halted
  • E_USER_NOTICE — Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally

Example

In this example an E_USER_WARNING occurs if the «test» variable is bigger than «1». If an E_USER_WARNING occurs we will use our custom error handler and end the script:

The output of the code above should be something like this:

Now that we have learned to create our own errors and how to trigger them, lets take a look at error logging.

Error Logging

By default, PHP sends an error log to the server’s logging system or a file, depending on how the error_log configuration is set in the php.ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.

Sending error messages to yourself by e-mail can be a good way of getting notified of specific errors.

Send an Error Message by E-Mail

In the example below we will send an e-mail with an error message and end the script, if a specific error occurs:

The output of the code above should be something like this:

And the mail received from the code above looks like this:

This should not be used with all errors. Regular errors should be logged on the server using the default PHP logging system.

Источник

Читайте также:  Sigusr1 soft tls error received process restarting
Оцените статью
toolgir.ru
Adblock
detector