PHP secure mail
Need to write a code to send secure mail in php
If no preventive measures are taken while coding a contact or feedback form in php, the code can be used by spammers to spam others. In this page, we will discuss how to write php mailing code so that it can not be compromised to spam.
A typical php code for mailing:
A PHP script for sending email calls mail() function to deliver the email. The code looks like this:
[email protected] is the address of the webmaster and $message and $email are a message and email collected from the feedback or contact form.
Unless preventive measures are taken, it is possible for a spammer to inject additional headers into the email messages by placing lines like the following into the $email variable
When this code is executed, all the email addresses added to the list are going to receive mails, which is unintended and will solve the purpose of the spammers.
How to write a secure code for mailing with php:
header("location : http://www.example.com/mail-error.php");
Here, preg_match function will check of the user name (stored in $usrname) and email (stored in $email) contains any newline characters. If newline characters are found, then somebody trying to compromise the script to spam. In that case, the code will redirect to a page like http://www.example.com/mail-error.php instead of sent mail.
PHP: Tips of the Day
PHP: How to convert string to boolean?
Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):
- "" (an empty string);
- "0" (0 as a string)
If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.
$test_mode_mail = $string === 'true'? true: false;
EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:
$test_mode_mail = ($string === 'true');
or maybe use of the filter_var function may cover more boolean values:
filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on".
Ref : https://bit.ly/33SQ3eP
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework