如何使用php和html发送电子邮件

时间:2016-11-16 08:23:10

标签: php html

我正在尝试从HTML表单发送消息。但由于某种原因,我没有得到任何东西。有人可以帮帮我吗?

这是我的HTML表单:

<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>

和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";
?>

我已将PHP文件命名为subb.php,并将它们列在同一目录中,但由于某些原因它仍无法正常工作。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

代码中的某些内容错误mail($to , $subject , $Message, "From :" . $Name . $Email);

Mail函数语法:

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

所以,

<?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";
    }  
}
?>

注意:使用任何邮件库都可以防止像PHPMailer

这样容易受到标头注入攻击

答案 1 :(得分:0)

<?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!";
}
;

测试一下,如果它不起作用,则意味着您的主机阻止邮件功能。