This is an application that is recurring on our forum, and the part of novice users with PHP in general:
1. Send email in text format
To send a basic email in PHP is simple, just use the mail () function.
Parameter, it takes respectively the recipient, subject, message, and (optionally) the headers.
The easiest way to email text is as follows:
$message = "Hello World"; mail('destinataire@example.com', 'Mon Sujet', $message);
It is very simple, but somewhat limited in this way. Indeed, you do not specify the sender, can not specify the person to place a copy (to send an email to several people), etc..
With additional headers that you can pass in the fourth parameter, you will be able to customize things a little. Here are some commonly used headers:
$headers ='From: "mon nom"<expediteur@example.com>' . "\n "; $headers .='To: Mary
<mary@example.com>, Kelly <kelly@example.com>' . "\n"; $headers .='Cc:
anniversaire_archive@example.com' . "\n"; $headers .=' Bcc:
anniversaire_verif@example.com' . "\n"; $headers .=' Reply-To:
adresse_de_reponse@example.com' . "\n"; $headers .='MIME-Version: 1.0' . "\n";
$headers .='Content-Type: text/plain; charset="iso-8859-1"' . "\n"; $headers .='
Content-Transfer-Encoding: 8bit'; $message = "Hello World"; mail
('destinataire@example.com', ' Mon Sujet', $message, $headers);
Notice that all headers are concatenated (end to end with. "="), And it takes place a newline at the end of each header, where the character "n" at the end of each of them.
Warning: I put "\ n" for line breaks, but if it does not work for you, you can try replacing the "\ n" with "\ rn", because it depends on the mail servers.
I will now detail the usefulness of these headers (not exhaustive, there are others):
- From: the name and email address of the sender
- To: Name and address of one or more recipients (extra, for as we have seen, we can already specify a recipient in the first parameter of the mail)
- Cc: other recipients who will receive the mail in "Carbon Copy". They will be able to see and copy that to the message was transmitted.
- Bcc: ADDRESSEES others who will receive the mail in "Blind Carbon Copy". They will copy but will not be able to see that the message has been transmitted, it is a blind copy.
- Reply-To: Specifies an email address different from the response sender address, if you want the answer you in a different address.
- MIME-Version: the version of the mime type used
- Content-type: to specify the mime type of mail and charset (character set). Often you will use "text / plain" as the type for an email in text format, and "text / html" for an email in html format (we'll see below).
- Content-Transfer-Encoding: This header specifies the encoding of the mail or any part thereof (for sending a text + html for example). It can for example take the values 7 and 8 bit (7 bit encoding is used in English speaking countries who do not need to manage accented letters)
2. Send email as HTML
We've already done the heavy lifting in the previous paragraph, because there is not much to change in order to send an email in html format.
Suffice it to change the "Content-type" like this:
$headers .='Content-Type: text/html; charset="iso-8859-1" ' . "\n";
It's left to do, of course, modify the message body to insert HTML code.
An example of sending mail in html with PHP, in this example I will place the logo 6ma.fr early message, and to insert an image, it is mandatory to use the HTML format.
$headers ='From: "mon nom"<expediteur@example.com>' . "\ n"; $headers .='To: Mary
<mary@example.com>, Kelly <kelly@example.com>' . "\n"; $headers .='Cc:
anniversaire_archive@example.com' . "\n"; $headers .='Bcc:
anniversaire_verif@example.com' . "\n"; $headers .=' Reply-To:
adresse_de_reponse@example.com' . "\n"; $headers .='MIME-Version: 1.0' . "\n"; $
headers .='Content-Type: text/html; charset="iso-8859-1"' . "\n"; $headers .='
Content-Transfer-Encoding: 8bit'; $message = "<html><head> <title>Mon titre
</title></head>"; $message .= "<body> <img src='http://www.6ma.fr/image/bannieres
/6ma-181x72.png' /> <br /><br /> Hello World</body> </html>"; mail
('destinataire@example.com', 'Mon Sujet', $message, $headers);
Compared to sending email in text format, so I just changed the "Content-Type" header in the mail, and the message body stored in the variable $ message.
In the body, place the html code classic, as you would for a web page in html.
Here in the email that is sent, we will have the logo 6ma.fr, two line breaks, and below the text "Hello World".