Send Email by Filling the html form using php Script

Send Email by Filling the html form using php Script
Share

Here First you know how your mail script will work , its the first and simple script,

[code lang=”php”]
< ?php $to = "recipient@example.com"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; if (mail($to, $subject, $body)) { echo("Message successfully sent!"); } else { echo("Message delivery failed..."); } ?>
[/code]

the 3 variables $to, $subject and $body are the variables that used to send email using php script, here you may add additional header like from or CC or BCC, etc. but these are optional, mail is the php predefind keyword.

Detailed with Form
First of all you will define a form and handle it with the css as you would like to.
make a form how many fields you would like to create it depends on your needs.

[code lang=”html”]









[/code]

here you complete your form
Now lets go to some php script.

[code lang=”php”]
< ?php $to = $_REQUEST['sendto'] ; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Company"} = "Company"; $fields{"Email"} = "Email"; $fields{"Phone"} = "Phone"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b)
{ $body .= sprintf(“%20s: %s\n”,$b,$_REQUEST[$a]); }

$headers2 = “From: noreply@Company.com”;
$subject2 = “Thank you for contacting us”;
$autoreply = “Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.xyz.com”;

if($from == ”) {print “You have not entered an email, please go back and try again”;}
else {
if($name == ”) {print “You have not entered a name, please go back and try again”;}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( “Location: http://www.xyz.com/thankyou.html” );}
else
{print “We encountered an error sending your mail, please notify webmaster@abc.com”; }
}
}
?>
[/code]

this is the code to send e-mail. hope you learn from it.

  • Aneeq

    To send HTML mail in PHP, you need to use some additional headers. Below is the code to send HTML mail in PHP.

    $to = “recipient@domain.com“;
    $subject= “Subject of email”;
    $message= “This is HTML mail.”;
    $fromName = “Name of the sender”;
    $fromEmail = “sender@domain.com“;

    // To send HTML mail, the Content-type header must be set
    $headers = “MIME-Version: 1.0? . “\r\n”;
    $headers .= “Content-type: text/html; charset=iso-8859-1? . “\r\n”;

    // Additional headers
    $headers .=”From: $fromName ”.”\r\n”;

    //Mail function
    mail($to, $subject, $message, $headers);

    Source:
    http://http://phphelp.co/2012/05/03/how-to-send-html-mail-in-php//

    OR

    http://http://addr.pk/a1c4

  • developer

    Thanks for sharing

© 2018 IT Tech Developer | XML Sitemap | HTML Sitemap
Powered by T-Developers | Designed by: Shah Rukh | Thanks to Friends
show
 
close
rss Follow on Twitter facebook linkedin youtube google+ email