WHAT'S NEW?

5th Lesson: create an emailing script using PHP


Setup The Environemment o develope

You'll need to setup a localhost, i personnally use EasyPHP and i recommand to use this soft:
http://www.easyphp.org/easyphp-devserver.php

Install it then lunch it:
Okey great, Now wil use the CSS HTML form that we have already create in the previous lesson:
http://www.digitalservicesmarket.biz/2015/05/4th-lesson-creating-customized-forms.html

Take the whole folder and move it to : programmefiles/Easyphp/data/localweb/Script.

Now go to your browser and type, localhost.
Choose Scripts/ then you'll find email folder clic on it the page Index.html will open automaticlly.

Now, Create an other file with extention PHP call it send.php.
Secondly we will edit HTML file that countain the form and make it redirected to send.php after send button clicked.


in this part of code:


<form class="form"> 

we will add Action to send.php like that:


<form class="form" action="send.php" method="POST"> 

Now it's PHP time:

In send.php file we will declare that we will write a php code :


<?php

?>

Now we will create 4 variables that will countain name, email, subject, and message.


<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['web'];
$msg = $_POST['text'];
?>

Every value of every field is stord in a variable.

PHP MAIL() FUNCTION



<?php
// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("someone@example.com","My subject",$msg);
?>

we will use this function to send emails with the contact form we have already work on.

so we will keep just the mail fuction.


<?php
mail("someone@example.com","My subject",$msg);
?>

and replace the email with the email variable, subject with website and meassage with message:


<?php
mail($email,$website,$msg);
?>


that's it :D

So the send.php file will countain this code


<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['web'];
$msg = $_POST['text'];


?>
<?php
mail($email,$website,$msg);
?>

Great!

But :( we have a problem, Localhost can't send emails, yes the server is for offline use not online, so what we have to do is to put it into an online Host i will write about that later.

PS: NEXT TUTORIAL WE WILL CHECK IF THE EMAIL WAS SEND OR NOT AND CREATE A THANK YOU MESSAGE.

0 Comments:

Post a Comment

Thank for leaving a comment