Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Website > PHP & MySQL > Reading a tutorial

Send an e-mail with php

Avatar
Author : William
Creation : : on 03/02/2008 01:30:33 PM
Last modification: : on 03/18/2008 12:32:26 PM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
How can I send an e-mail with PHP ?


Nothing easier: You can use the mail () function, and input it the recipient, the subject and the contents.
So: it works, but the email will be probably end up in 'spams'... And I reckon that it's not exactly what you want :)

Therefore, this tutorial will not only teach you how to send an e-mail in HTML, but also how to send an e-mail that arrives at its destination.
Chapter Contents :

The header

Definition:




The header of an e-mail is the part at the beginning of an e-mail which is invisible to normal users who only read their e-mails.


So the header contains all the information about the e-mail. Here are some examples:



What? But you just tell us that we can't see the header! But I know very well what time my e-mail was sent and who sent it to me!




Yes, well of course the user would see some of this information but it is only because your email will read the header and then translates it into a text for you to read.


Diagram:



When you create the header to send an e-mail with the mail () PHP you need to specify 4 arguments :





Now, I will explain this list. The first two arguments do not require explanation (or you are even worse than a newbie :D ).
For the version of the MIME (Multipurpose Internet Mail Extension), it's to tell the server that receives the e-mail, what the version of the e-mail is. Basically, it allows you to tell HIM: *M@teo, ici, cefci été traduit de "lui dire" en français automatiquement. en anglais, ce serait "it", mais "lui"="him" pour un traducteur! Aucun angliais ne ferait cette erreur!* how to read it. :)
- Masquer le texte des messages précédents -


If you want more explanation go on wikipedia


For the content-type, you may already be met. It will be used in this case to define what is going to contain your e-mail. Keep in mind that it can also be used to define the HTML content of a page or PHP when generating an image with a graphics library (to bring to your browser how it is going to have to interpret what's going within the same page). It will be seen that the content-type would have to be reused later in the message body.

How well declare his header?




We will keep the same pattern as before. I remind you:




I will start to write code from now on. As I am dealing with the header, I will put all that concerns him in a variable $ header.


Statement of the sender:
Code: PHP
1
2
3
<?php
$header = 'From: "CONSIGNOR"<ADRESS_CONSIGNOR>\n';
?>



Statement of the return address:
Code: PHP
1
2
3
<?php
$header.= 'Reply-to: "RETURN" <ADRESS_RETURN>\n';
?>


Statement of the MIME version:
Code: PHP
1
2
3
<?php
$header.= 'MIME-Version: 1.0\n';
?>



Statement of the content-type:
Code: PHP
1
2
3
<?php
$header.= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
?>



Some explanations :



First, the value of content-type: multipart / alternative.
I chose to use it because my tutorial aims to send an e-mail in text mode and HTML (I will detail these modes a little later).


There is also the boundary value which I assigned $boundary: I am compelled to declare the value here, but I will explain later the contents of the variable and its usefulness.


A concrete example:

Code: PHP
1
2
3
4
5
6
7
8
<?php
//=====Création du header de l'e-mail
$header = "From: \"WeaponsB\"<you@you.com>\n";
$header .= "Reply-to: \"WeaponsB\" <he@he.com>\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/alternative; boundary=\"$boundary\"";
//==========
?>

The different type of e-mail

Definition:




So I imagine that all of you have received e-mails at least once in your life ^ ^, you have had an opportunity to see that some e-mails are with beautiful images and a nice formatting. For example:


Hi everybody, here's an e-mail in HTML.
In HTML format



Hi everybody, here's an e-mail in text format.
In text format




As we will send an e-mail in text mode and HTML mode, we will have two variables: the first containing the contents of the e-mail text mode that we will call $message_txt and the second containing the e-mail in HTML format which will be called $message_html. But beware: these messages will end with a OBLIGATORY "\ n".


How can such a statement?




To make such a statement, you must use two things:



If we want to bring this party PHP code, there will be something like this:

Code: PHP
1
2
3
4
5
6
7
<?php
$message = "...";
$message .= "Content-Type: XXX/XXX; charset=\"XXXXXX\"\n";
 
$message .= "Content-Transfer-Encoding: XXXXXXXXXX\n\n";
$message .= "...";
?>


For information, here I am using a new variable $message. This variable will be sent to the mail() function and contain the entire contents of the e-mail (text and HTML).

You will also notice that I added "..." Before and after my type declaration. That's because normally there is something else before and after the statement. I speak later in the tutorial.



Some explanations



Firstly, the content-type (yes him again, but I told you that it would :) ).
Here, it is used to say if we want to save the rest of the text or HTML.



Moving to the charset. It is used to determine the type of character encoding that will follow.


My knowledge is limited in this area, I can't really tell you that this or that charset is better than another. Personally, I have always used the charset iso-8859-1 for my emails and I never had any problem. If I chose the latter because I looked at the source code of several e-mail received and it is one that is used each time. :)



Now the last point: Content-Transfer-Encoding. This setting allows you to define how many bits will be encoded message, which actually determines the number of different characters as possible.
A small example: an e-mail with a Content-Transfer-Encoding set on 7 bits can have 128 different characters.



So, now it's going to depend on what you put in your e-mail. If you want to use accents, you must save the Content-Transfer-Encoding on 8 bits. :)


So if we apply what is above, it should be reflected in a statement by HTML mode with this:


Code: PHP
1
2
3
4
5
6
7
<?php
$message = "...";
$message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
 
$message .= "...";
?>

And let's go for the assembly!

On your marks, ready? Assemb ... STOP!


And it is not yet for right away. :D No I do not see me with these eyes! Yes them :angry2:

It's going to come soon, but I have to explain you one last thing before you can run.

Boundary :



Finally, I will tell you what contains the famous variable $boundary. :)



Boundary will allow us to separate the different parts of our e-mail and it is MANDATORY. One could see them as super-tags.


The format of a boundary is as follows:


Quote: Format
----=Random_String



To generate this string, we will use two functions PHP. First, the rand() function that allows you to get a random number, then md5() function which allows a string hash (here we hash that the rand() function we released).



So, if you understand what I explained to you you should have written a code like this:

Code: PHP
1
2
3
<?php
$boundary = "-----=".md5(rand());
?>



Here wa are! Our boundary is created. :)

But our bounadry need a end . Unfortunately, the establishment of the boundary as it will not be enough for the main text . It will be necessary to add these characters every time you use it outside of the declaration, which is located in the header.



Schematic's final message:



Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
//===== Declaration of messages in text format and in HTML format
$message_txt = "        Hi to all, here is an e-mail sent by a PHP script.";
 
$message_html = "<html><head></head><body><b>Salut à tous</b>, here is a e-amil sent by<i>Me</i>.</body></html>";
//==========
 
//=====Creation of the boundary
 
$boundary = "-----=".md5(rand());
//==========
 
//=====Definition of the topic
$sujet = "Hey my friend !";
//=========
 
//===== Creating the header of the e-mail
$header = "From: \"WeaponsB\"<you@you.com>\n";
 
$header.= "Reply-to: \"WeaponsB\" <he@he.com>\n";
$header.= "MIME-Version: 1.0\n";
$header.= "Content-Type: multipart/alternative; boundary=\"$boundary\"";
 
//==========
 
//=====Creating the Post
$message = "--".$boundary."\n";
//===== Adding to the message in text format
$message.="Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
 
$message.="Content-Transfer-Encoding: 8bit\n";
$message.= $message_txt."\n";
 
//========== 
$message.= "--".$boundary."\n";
//===== Adding to the message html format
 
$message.="Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$message.="Content-Transfer-Encoding: 8bit\n";
$message.= $message_html."\n";
//==========
$message.= "--".$boundary."--\n";
 
$message.= "--".$boundary."--\n";
//==========
 
//=====Send the e-mail
mail("weaponsb@mail.fr",$sujet,$message,$header);
//==========
 
?>

This tutorial is over.
I think I have managed to teach you how to send an e-mail in PHP.
If you want more explanations send me a PM or post in the Forum . :)
Author : William
Rate and comment this tutorial
Print this tutorial

Number of Newbies connected 11 Newbies Connected | SQL requests 7 Requests | Page loading delay 0.0828s (0.0673s)

Change your template - Learn more about NP - Site map
Contact us - Privacy Policy -->
Terms of use - RSS feed - XHTML 1.0 - CSS 2.0

There is nothing else to read, you have to go up now !