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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22586226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:07:37  来源:igfitidea点击:

How to send a message using form (html) and php to send message?

phphtmlformsemailsend

提问by user3445041

I am using this form code in html to make a contact form on my website:

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

This is the php code that I'm using to get the contact form sent to my email address:

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

<?php
   if(isset($_POST['send'])) {
   // Prepare the email
   $to = '[email protected]';
   $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.';
   }
   }
?>

I cannot get it to send to my email address. I've made it live to try it but it still doesn't work and I don't know what I'm doing wrong. How can I get the user the send the email to my email address - have it sent to my email address so that I can see the message in my email? (Please help)

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

回答by santosh devnath

This code Work 100%Try this code in PHP. Then upload it in your hosting site. Because email does not work on local. So you need to upload it. Happy Coding.!!!

这段代码 100% 有效在 PHP 中尝试这段代码。然后将其上传到您的托管站点。因为电子邮件在本地不起作用。所以你需要上传它。快乐编码.!!!

    <?php 
     $to ='[email protected]';
     $from = '[email protected]';
     $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';
    }

?>

回答by Funk Forty Niner

Your code checks out. The only thing (or one of possibly other unknown factors at this time) I can see that could affect it is, that you don't have a From:in your mail headerswhich may be ignored or sent to Spam; this is entirely possible and I have seen it happen before.

您的代码检出。唯一的事(或在这个时候可能还有其他未知的因素之一),我可以看到,可能影响它,那你没有一个From:mail headers其可能被忽略或发送垃圾邮件; 这是完全可能的,我以前也见过这种情况。

Try the following with added header for From:and the person who sent it.

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

(Tested on my hosted server, and sent to Inbox)

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

<?php

if(isset($_POST['send'])) {
   // Prepare the email
$to = '[email protected]';

$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.';
   }
}
?>