php

13
Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP. So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated. Sending a Simple Text Email Sending HTML Email Sending Email with Attachments Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server. Sending a Simple Text Email At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise. Have a look at the example: <?php //define the receiver of the email $to = '[email protected]'; //define the subject of the email $subject = 'Test email'; //define the message to be sent. Each line should be separated with \n $message = "Hello World!\n\nThis is my first mail."; //define the headers we want passed. Note that they are separated with \r\n $headers = "From: [email protected]\r\nReply-To: [email protected]"; //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don't receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.

Upload: lamine

Post on 20-Nov-2015

5 views

Category:

Documents


3 download

DESCRIPTION

PHP code

TRANSCRIPT

Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated. Sending a Simple Text Email Sending HTML Email Sending Email with AttachmentsNote that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in yourphp.inifile to point to your email server.Sending a Simple Text EmailAt first let's consider how to send a simple text email messages. PHP includes themail()function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. Themail()function returns True if the message issent successfully and False otherwise. Have a look at the example:

As you can see, it very easy to send an email.You can add more receiversby either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don't receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.Back to topSending HTML EmailThe next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup. Have a look at the example:

--PHP-alt-Content-Type: text/plain; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

Hello World!!!This is simple text email message.

--PHP-alt-Content-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

Hello World!This is something with HTML formatting.

--PHP-alt---

In the preceding example we add one additional header of Content-type:multipart/alternative and boundary string that marks the different areas of the email. Note that the content type of the message itself is sent as a mail header, while the content types of the individual parts of the message are embedded in the message itself. This way, mail clients can decide which part of the message they want to display.Sending Email with AttachmentThe last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email.Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:--PHP-mixed-Content-Type: multipart/alternative; boundary="PHP-alt-"

--PHP-alt-Content-Type: text/plain; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

Hello World!!!This is simple text email message.

--PHP-alt-Content-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

Hello World!This is something with HTML formatting.

--PHP-alt---

--PHP-mixed-Content-Type: application/zip; name="attachment.zip"Content-Transfer-Encoding: base64Content-Disposition: attachment

--PHP-mixed---

As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include anattachment to our message, we read the data from the specified file into a string, encode itwith base64,split it in smaller chunks to make sure that it matches the MIME specifications and then include it asan attachment.

Verifying email addressWhen users sign up to join your website, you may want to verify their email address by sending confirmation link to their email address. You'll learn how to do this in this tutorial.OverviewIn this tutorial, create 4 PHP files and 2 databases1. signup.php2. signup_ac.php3. confirmation.php4. config.php

We have to create 2 databases1. temp_members_db2. registered_members

What to do1. When users sign up. Random a set of confirmation code.

2. Keep their informations and confirmation code in table "temp_members_db". This is temporary table, we have to move this informations to table "registered_members" after email address has been verified.

3. After sucessfully inserted data into table "temp_membes_db", send confirmation link to email that users used to sign up, if email is invalid they will not receive our email.

4. They have to click on confirmation link to activate their account. (move data from table "temp_member_db" to table "registered_members" and delete data from table "temp_members_db" in this step)STEP1:Create table "temp_members_db" and table "registered_members"

Table "temp_members_db"CREATE TABLE `temp_members_db` (`confirm_code` varchar(65) NOT NULL default '',`name` varchar(65) NOT NULL default '',`email` varchar(65) NOT NULL default '',`password` varchar(15) NOT NULL default '',`country` varchar(65) NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1;

Table "registered_members"CREATE TABLE `registered_members` (`id` int(4) NOT NULL auto_increment,`name` varchar(65) NOT NULL default '',`email` varchar(65) NOT NULL default '',`password` varchar(65) NOT NULL default '',`country` varchar(65) NOT NULL default '',PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;STEP2:signup.php - Create sign up form

############### Code

Sign up

Name:

E-mail:

password:

Country:

STEP3:signup_ac.php - Insert data into databaseIn this step1. Random confirmation code.2. Insert data and confirmation code into database.3. Send email to user with confirmation link.############### Code