Explanation of how to send emails from PHP pages and simple and complex examples to perform the task.
In this article we are going to tell you how to send email from PHP in the simplest and most direct way. It is a utility that any development requires and therefore something that you must learn quickly. Fortunately, the process is very simple, thanks to the functions incorporated in the language itself.
For sending emails using PHP we have a quite powerful function, included in all versions of PHP, without the need to install any add-ons. Its behavior is quite simple, although in the background there is a lot of additional information to make sure that the mail is delivered and to configure PHP and the SMTP server that you use for the e-mail output. In this article we are going to stay with the most basic, although in .com there are many articles to help you in many other aspects as well and that offer you various alternatives and additional information to ensure that your emails are built and delivered correctly.
Specifically, in PHP we have a function called mail() that allows us to configure and send the email message. The function is called mail() and it receives three mandatory parameters and two other parameters that we can optionally place. Returns true if the message was sent successfully and false otherwise.
Parameters required in all cases in the mail() function
First let’s look at the required parameters for PHP’s mail() function, which are the same ones you’d need in any type of message.
- Recipient: the email address or email addresses that are to receive the message. If we include several addresses we must separate them by a comma.
- Subject: to indicate a string of characters that we want to be the subject of the email to be sent.
- Body: the body of the message, what we want the email to have written.
Example of sending a simple email
As you will see below, sending mail, with the basic configurations, from PHP is extremely simple.
mail("pepito@,maria@guiartemultimedia.com","asuntillo","This is the body of the message") ?>
Optional parameters of the mail() function for sending mail
In addition, the mail() function, native to PHP, offers you the possibility of indicating other parameters, optionally. But, despite being simple optional parameters, the use of at least the first one is recommended.
- Headers: Mail headers. Data such as the reply address, the possible addresses that will receive a copy of the message, the addresses that will receive a blind copy, if the email is in HTML format, etc.
- additional_parameters: This option is rarely used and, moreover, it is only available from PHP version 4.0.5 and from PHP 4.2.3 it is disabled in safe mode. It can be used to pass additional parameters to the program configured to send the mail, when the mail is sent using the sendmail_path configuration option. We can obtain more information in the .
Complex mail delivery example
We are going to send an email in HTML format to pepito@, with a copy to mariano@ and with a blind copy to pepe@pepe.com and juan@juan.com. We will configure the response address to maria@.
Hello folks!
Welcome to my test email . I am delighted to have so many readers. This body of the message is from the article on sending emails by PHP. It would have to be changed to put your own body. By the way, it also changes the message headers.