简体   繁体   中英

how to send email message with php and html

I am trying to send a message from HTML form. But for some reason I am not getting anything. Could someone please help me ?

Here is my HTML form:

<form method="post" action="subb.php">
    <div class="field half first">
        <label for="Name">Name</label>
        <input type="text" name="Name" id="name" />
    </div>
    <div class="field half">
        <label for="Email">Email</label>
        <input type="text" name="Email" id="email" />
    </div>
    <div class="field">
        <label for="Message">Message</label>
        <textarea name="Message" id="message" rows="5"></textarea>
    </div>
    <ul class="actions">
        <button type "submit" name="submit" id="submit" class="button submit">Send message</button>

and the PHP:

<?php
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    $Message = $_POST['Message'];

    $to = "combatstriker111@gmail.com";
    $subject="new message";

    mail($to , $subject , $Message, "From :"  . $Name . $Email);

    echo "Your message has been  Sent";
?>

I have named the PHP file subb.php and listed them both in the same directories but its still not working for some reason. Any help is very much appreciated.

Something in your code was wrong mail($to , $subject , $Message, "From :" . $Name . $Email);

Mail function SYNTAX :

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

So,

<?php
if(isset($_POST['submit'])) {
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];

    $Message = "Name : ".$Name."<br />"
    $Message .= $_POST['Message'];

    $to = "combatstriker111@gmail.com";
    $subject="new message";

    // 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 .= 'To: Name <$to>' . "\r\n";
    $headers .= 'From:  $Name <$Email>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";


    if(mail($to, $subject, $Message, $headers)) {
       echo "Your message has been  Sent";
    } else {
       echo "Mesage Error";
    }  
}
?>

Note : Use any mail library for prevent vulnerable to header injection like PHPMailer

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];

$to      = 'info@fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info@fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info@fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
            $phone."</br>Subject: ".$subject.
            "</br>Message: ".$message;
//echo $themessage;


if(mail($to, $subject, $themessage, $headers)){
 echo "message sent";
 header( 'Location: http://www.fullertoncomputerepairwebdesign.com/contactus.php?smresponse=Message Sent' ) ;
 }else{
echo "we have a error charlie!";
}
;

test this out and if it doesn't work it means your hosting blocks mail function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM