Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

HTML Button and PHP Mail

the mail() function

Moderators: macek, egami, gesf

HTML Button and PHP Mail

Postby coco » Wed Jun 08, 2011 9:48 am

Hi everybody,

I created a small online form to ask for a product at my job. Once the form is filled, the user has to click on a button "submit". After doing so, he is redirected on another page where he has a message telling them a mail was sent to an admin. When accessing this page, the server send a mail to an admin with this function :

Code: Select all
<?php

$name=$_POST["name"];
$email=$_POST["email"];
$vmname=$_POST["vmname"];
$vcpu=$_POST["vcpu"];
$ram=$_POST["ram"];
$dmos=$_POST["dmos"];
$dmstorage=$_POST["dmstorage"];
$os=$_POST["os"];
$database=$_POST["database"];
$nfs=$_POST["nfs"];
$vlan=$_POST["vlan"];

function send_email($from, $to, $subject, $message){
        $headers = "From: ".$from."\r\n";
        $headers .= "Return-To:  ".$from."\r\n";
        $headers .= "Return-Path: ".$from."\r\n";
        $headers .= "Content-type: text/html\r\n";

        if (mail($to,$subject,$message,$headers) ) {
           echo "email sent";
        } else {
           echo "email couldn't be sent";
        }
}

$subject = "Virtual Machine Formulary!";
$message .= "<html><body>";
$message .= "<b>From : </b>";
$message .= $name;
$message .= "<br /><b>Contact : </b>";
$message .= $email;
$message .= "<br />";
$message .= "<br /><b>VM Name : </b>";
$message .= $vmname;
$message .= "<br /><b>vCPU : </b>";
$message .= $vcpu;
$message .= "<br /><b>RAM : </b>";
$message .= $ram;
$message .= " Gb<br /><b>Disk Memory OS : </b>";
$message .= $dmos;
$message .= " Gb<br /><b>Disk Memory Storage : </b>";
$message .= $dmstorage;
$message .= " Gb<br /><b>OS : </b>";
$message .= $os;
$message .= "<br /><br /><b>Database? : </b>";
$message .= $database;
$message .= "<br /><b>NFS server? : </b>";
$message .= $nfs;
$message .= "<br /><br /><b>VLAN : </b>";
$message .= $vlan;
$message .= "</body></html>";
send_email($_POST["email"],"julien.parent-trudeau@transcontinental.ca",
        $subject ,
        $message);
?>


The thing is that I would like to know if it would be possible for the server to redirect the user to another page if the server is not able to send the email.


So :
Page 1 is the form with all the field and the submit button
Code: Select all
<!--Boutton Submit-->
<button type="submit">Submit</button>
<div class="spacer"></div>


Once hit, the user is redirecting on Page 2 wich tells him an e-mail has been sent (because of the code I posted earlier).


So in the end, I would like to know if it is possible to redirect the user on another page if the mail() function failed. Maybe I should mail that function on the first page? I'm kinda new with php so I don't really know what to do.

Thank you!
coco
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Jun 08, 2011 9:41 am

Re: HTML Button and PHP Mail

Postby coco » Wed Jun 08, 2011 10:37 am

well I guess I was a bit tired earlier as I now found the answer and it is a pretty easy one.

When the user hits Submit, he's redirected on another page. As soon this page opens, it start the php function mail(). If it works, the server keep showing all the information and reading the html code.

If it fails, it redirects the user on an error page and stop reading code from the current page.

:) Easy!
coco
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Jun 08, 2011 9:41 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.