如何使用表单(html)和php发送消息发送消息?

时间:2014-03-23 02:11:01

标签: php html forms email send

我在html中使用此表单代码在我的网站上创建联系表单:

<form method="post" action="contact.php">
    <p>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" />
    </p>
    <p>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" />
    </p>
    <p>
        <label for="message">Message</label>
        <textarea id="message" name="message" rows="6" cols="30"></textarea>
    </p>
    <p>
        <input type="submit" name="send" value="Send message" />
    </p>
</form>

这是我用来将联系表单发送到我的电子邮件地址的PHP代码:

<?php
   if(isset($_POST['send'])) {
   // Prepare the email
   $to = 'someone@example.com';
   $subject = 'Message sent from website';
   $message = $_POST['message'];
   // Send it
   $sent = mail($to, $subject, $message);
   if($sent) {
   echo 'Your message has been sent successfully!';
   } else {
   echo 'Sorry, your message could not send.';
   }
   }
?>

我无法将其发送到我的电子邮件地址。我已经尝试了它,但它仍然不起作用,我不知道我做错了什么。如何让用户将电子邮件发送到我的电子邮件地址 - 将其发送到我的电子邮件地址,以便我可以在电子邮件中看到该消息? (请帮忙)

2 个答案:

答案 0 :(得分:1)

此代码工作100% 在PHP中尝试此代码。然后将其上传到您的托管网站。因为电子邮件不适用于本地。所以你需要上传它。快乐的编码。!!!

    <?php 
     $to ='santoshdevnath15@gmail.com';
     $from = 'santoshdevnath15@gmail.com';
     $subject = 'Html Eamil Check';
     $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Untitled Document</title>
                </head>

                <body>
                <div style="width:400px; height:400px; background-color:#096; color:#FFF">
                <h1>Welcome to inet shopping site</h1>
                <p>
                Dear Santosh,
                We have received your Cash-On-Delivery order. We need to confirm your order by call. We will get in touch with you soon on Your order               will be processed only after confirmation.
                For the payment, please pay Rs. 4000 cash to the courier person when he delivers the product.
                "Please call us on 8882248819 to confirm after you deposit the amount."
                </div>
                </body>
                </html>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if(mail($to, $subject, $message, $headers)){
        echo 'ok';
    }
    else{
        echo 'error';
    }

?>

答案 1 :(得分:0)

您的代码已签出。我可以看到唯一可能影响它的事情(或此时可能还有一个未知因素),mail headers中没有From:可以忽略或发送给垃圾邮件;这完全有可能,我以前见过它。

尝试以下内容,添加From:标题和发送标题的人。

(在我的托管服务器上测试,并发送到收件箱)

<?php

if(isset($_POST['send'])) {
   // Prepare the email
$to = 'someone@example.com';

$name = $_POST['name'];
$mail_from = $_POST['email'];
   $subject = 'Message sent from website';
   $message = $_POST['message'];

$header = "From: $name <$mail_from>";

   // Send it
   $sent = mail($to, $subject, $message, $header);
   if($sent) {
   echo 'Your message has been sent successfully!';
   } else {
   echo 'Sorry, your message could not send.';
   }
}
?>