Creating a website can be the beginning of a large project, and choosing the right technology for customer feedback is the key to success. Web developers often use the PHP Mail feature to send email. The most convenient way to do this is when a site visitor fills out a form.
PHP Technology History
They are used to develop web applications. These technologies appeared by chance, like many other great discoveries. Rasmus Lerdorf was looking for a solution to the problem of overload on the server, where he posted his personal page and training program. This led to the creation of PHP (Personal Home Page) in 1994. In 1995, Rasmus released the source code and created a mailing list to exchange ideas, improve, fix. Since then, the language has grown and developed as an open source project.
In 1996, just a year after its publication, it was already used on 15,000 websites. In 1998, his presence on the Internet amounted to almost 60,000 domains, which is approximately 1% of the total number of existing projects and blogs. Currently, approximately 24% of all available web projects in the world use PHP.
Such popularity of the model is caused not only by the quality, but also by the importance of feedback. Among the sites that today use this technology: WordPress, Twitter and Facebook. Therefore, the evolution of PHP corresponding to the hypertext preprocessor is undoubtedly exponential in every way.
Self-Installing Package Project
PHP is a free and open language with a low learning curve, its syntax is simple and meets the basic standards of object-oriented programming. Complex development environments are not required, as is time for education. Programming is done without assistance, all available IDEs are free, and development is quick and easy to set up.
Deploying a project in PHP is easy because it has self-contained integration packages that use: Apache and MySQL for both UNIX (LAMPP) and Windows (XAMPP). Both are easy to install and configure as a desktop application. The user receives a local server with access to the database on the computer, ready to develop any project. No time wasted on compatibility issues. In addition, it offers easy access to databases and supports many formats, not just MySQL.
Below is the code for the main email function. You can apply the script and use the form on any website by setting variables to send such an email.
The first part of the form checks if the email address input field is filled. If not, an HTML form is displayed on the page. If the online letter is prepared after the visitor completes the form, it is ready to be sent. When you click the "Send" button, the page reloads, PHP checks to see if an email address is entered, and sends the document.
Popular postal library
There are many ways to deliver letters. Usually send email in two ways:
- Using the built-in PHP function Mail;
- using a third-party library such as PHPMailer with SMTP.
Before you start using the program, you will need the following:
- Web hosting.
- Email account
- Included PHP Mail feature.
- Enabled SMTP function.
- Domain with hostinger MX record.
Mail in PHP allows you to send email using the local sendmail program. Each time the user starts the mail service, he calls this local program, usually configured by the system administrator. You can enable or disable this feature in the "Email" -> "Mail Service Management Section" section.
PHPMailer is a popular library that supports sending mail through the PHP Mail or SMTP function.
Work algorithm:
- To use PHPMailer with Hostinger SMTP, you first need to create an identifier in the "Email Account" section of the web hosting panel. The user will have four fields: email account username, password, SMTP host, SMTP port.
- After that, install PHPMailer. One of the easiest ways to do this is to use Composer, which is pre-integrated into shared hosting plans, so they connect via SSH and run the following command: cd public_html.
- Then they start: composer requires phpmailer and wait a few minutes until the installation is completed and PHPMailer appears in the created vendor directory. For delivery, SMTP is used. This line imports the PHPMailer class into the global namespace and includes various libraries.
- Send an HTML message. Its contents will be taken from the message.html file located in the same directory. This allows you to format the text and gives more functionality compared to regular text messages.
Script run
PHPMailer offers the user additional features that you can try in the official GitHub repository. Also, if he uses WordPress, he can easily create a contact form using plugins.
Use PHPMailer not only to send messages. You can create a contact form where visitors or users contact the site. Below is the final result of creating the form.
Script execution order:
- Create a testphpmailer.php file and populate the SMTP fields that were received earlier.
- Run the script by entering YourDomain.com/testphpmailer.php in the browser, a message appears.
- As soon as the user sends it, they will receive a confirmation that the content will be delivered to the email inbox specified in the Address field.
- If the PHPMailer contact form does not work, add the following line to check the Mail PHP function to find out the cause of the problem: $ mail-> SMTPDebug = 2.
Troubleshooting
PHPMailer is a pretty simple tool to learn. But from time to time errors may occur, and then the Mail PHP function does not work.
Common problems sending mail:
- Sender address rejected - an error means that the server could not authenticate using the provided data. To fix it, analyze the headers from and make sure that they correspond to the existing mailbox. After elimination, the script will start to run.
- Gmail cannot confirm that example.com sent a warning message when testing a PHP mail script. Indicates that the SPF record is not enabled, the email address does not exist or does not belong to the user.
- DNS failures - often considered connection timeouts: “Temporary failure to resolve the name”, “Failed to resolve the host”, “Getaddrinfo failed” or similar errors.
- Firewall redirection. This is due to Google, and if the user sees something like the name of the provider, then there are authentication errors and TLS certificate verification.
- SELinux lock - prevents PHP or the web server from sending emails, which is likely on RedHat / Fedora / Centos.
Parsing JSON with PHP
PHP JSON support is extremely simple, with only one common library for handling data conversion from PHP arrays and objects. It is rapidly gaining popularity due to its readability, mobility and the ability to use in web browsers without additional databases. If the user is wary of XML, JSON, for him this is the best direction of work.
With XML, it is more convenient to work with user data directly in the XML parser used. Using JSON, you can immediately convert data to an object or array of PHP and work with them using familiar PHP structures, control flow, and functions.
The first thing the application needs to do is check the Mail PHP function to see if the received error code matches expectations. For a simple GET request, the response should be 200 if it was successful.
To get the status code using Json decode, where $ return represents the content returned from the API request, $ code will be set to 200. The second argument to the Mail function of PHP Json decode post returns the result as an associative array, not an object.
An even better way to check for errors is to do the following:
$ json = json_decode ($ return, true);
$ code = $ json ['code'];
$ json = json_decode ($ return, true);
$ error = isset ($ json ['error']);
It is very important that the code is configured to correctly process and account for all errors. But even if the request is formed correctly, there is still a chance that the user will receive an error code when it is executed.
The advantage of a mail project
For a programmer who wants to learn PHP, the programming language offers huge benefits. He, perhaps, has one of the largest online communities in comparison with others, and it is within the framework of this support that almost any problem is solved.
For each PHP user, its capabilities are important not only in the current network, but also in the future. From the point of view of specialists, if this technology continues its current pace of development, adapting to new needs and adding more functions, it has a future.
PHP Benefits:
- The language is completely free and open.
- Very low learning curve.
- Development environments are quick and easy to set up.
- Easy deployment: fully self-installing packages with PHP integration.
- Easy access to databases.
- Very large community.
PHP technology is interesting, it is necessary to solve many of the most popular tasks related to sending email and using attachments.