Send emails from PHP

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.

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@.

Test Email

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.

‘; //for sending in HTML format $headers = “MIME-Version: 1.0\r\n”; $headers .= “Content-type: text/html; charset=iso-8859-1\r\n”; //address of the sender $headers .= “From: Miguel Angel Alvarez \r\n”; //reply address, if we want it to be different from the sender’s $headers .= “Reply-To: mariano@\r\n”; //message path from source to destination $headers .= “Return-path: hellohello@\r\n”; //addresses that will receive the copy $headers .= “Cc: maria@\r\n”; //addresses that will receive blind copies $headers .= “Bcc: pepe@pepe.com,juan@juan.com\r\n”; mail($recipient,$subject,$body,$headers) ?>

Note: Before starting the script on your server, please, change the configuration data of the email addresses that will receive the message and place some addresses that are yours and where you can check if the messages are sent correctly.

conclusion

We think and hope that after this article you share our opinion, that sending emails in PHP is a very simple task. Also, it is very appreciated that all versions of PHP include a function for sending emails.

However, although this task can be very simple in principle, it should be noted that sending emails is not so easy, partly because of the server configuration, but mainly because of the amount of spam that circulates on the Internet. , many times we can find that we too are classified as spam and therefore our messages are lost.

Treating your mail does not go to spam is a complex task. What is on our side and that we can quickly make sure of is having built a well-formed message, having a server that is not on blacklists and taking care to respect a series of rules and advice. We talk about all this in many other articles within .com. Here are some references to complement and expand this information:

Note: To send mail using PHP it is necessary that it have a correct configuration.

If our website is on a server of a hosting provider, surely they have already configured PHP to send emails. If we are working on our own server, we will have to configure PHP.

PHP is configured in the php.ini file, where we must specify data such as the outgoing mail server that PHP must use to transfer messages.

Within the php.ini, we must look for the heading . Depending on our system we will have to configure it in one way or another.

In Windows systems we will find the php.ini in the windows directory or within it, in the system32 subdirectory or similar. In this system we must indicate the domain of the smtp server, something like smtp.mydomain.com. If it is the local computer that acts as the server, we can put “localhost” as the machine that will send the mail. We can also specify the address from where we want the message to appear to be sent from if no other is indicated during delivery.

See also  Control output in PHP
Loading Facebook Comments ...
Loading Disqus Comments ...