简体   繁体   English

如何从网页发送电子邮件?

[英]How do I send an E-mail from a webpage?

I've been trying to learn the basics of HTML, and in doing this I've been designing my own rudimentary webpage. 我一直在尝试学习HTML的基础知识,为此,我一直在设计自己的基本网页。 I don't plan on making it public at all, but it's good to help me to learn all of the different aspects of both HTML and CSS. 我根本不打算将其公开,但是很好地帮助我学习HTML和CSS的所有不同方面。 In doing this, I've added a Contact section on one of the pages, that includes both an email address and a form that allows for an E-mail to be sent, I used one of the tutorials on W3Schools to create it. 为此,我在其中一个页面上添加了“联系人”部分,其中既包含电子邮件地址,又包含允许发送电子邮件的表格,我使用了W3Schools上的其中一篇教程来创建它。 For the purposes of this I have removed my own email address, and replaced it with someone@example.com , but here is the output of this specific part of the code: 为此,我删除了自己的电子邮件地址,并用someone@example.com替换了它,但这是该代码的此特定部分的输出:

电子邮件

However, whenever I try to fill the form in to test it, I get this pop-up message. 但是,每当我尝试填写表单进行测试时,都会收到此弹出消息。 If I click cancel, then nothing happens, but if I click OK then then the mail app on my computer is opened. 如果单击“取消”,则什么也不会发生,但是,如果单击“确定”,则会打开计算机上的邮件应用程序。 But the message that I typed into the form isn't there, and the E-mail address that I type into the box to send from is just changed to the default address on my computer. 但是,我在表格中键入的消息不存在,并且我在框中键入要发送的电子邮件地址只是更改为计算机上的默认地址。

弹出

So what can I do to prevent this pop-up message, and to just send the e-mail to me? 那么,如何防止出现此弹出消息并仅向我发送电子邮件呢?

Here is the relevant code from the HTML document: 以下是HTML文档中的相关代码:

<h2 style = 'font-weight:normal'><a name = 'Contact' id = 'Contact'></a>Contact me:</h2>

<p>
  You can reach me at: someone@example.com <br />
  Or by just using the form below
</p>

<form action = 'mailto: someone@example.com' method = 'post' enctype = 'text/plain'>
  <input type = 'text' name = 'name' placeholder = 'Name'> <br />
  <br />
  <input type = 'text' name = 'mail' placeholder = 'E-mail'> <br />
  <br />
  <input type = 'text' name = 'comment' size = '50' placeholder = 'Comment'> <br />
  <br />
  <input type = 'submit' value = 'Send'>
  <input type = 'reset' value = 'Reset'>
</form>

If HTML isn't going to be enough to send an e-mail from a webpage, and I need another language to write a program that can do it, I am quite competent in Python, and I know C# to some extent. 如果HTML不足以从网页发送电子邮件,而我需要另一种语言来编写可以做到这一点的程序,则我在Python方面相当称职,并且在某种程度上我知道C#。 However, I've never used JavaScript, PHP, Perl, or anything else (I don't know what sort of languages would be appropriate) 但是,我从未使用过JavaScript,PHP,Perl或其他任何东西(我不知道哪种语言合适)

If you want to submit the form to go to an email it's simple really. 如果您想提交表格以发送电子邮件,那真的很简单。 You could use a simple server-side language like PHP. 您可以使用简单的服务器端语言,例如PHP。 You'll need 3 files. 您将需要3个文件。 One file that houses the front-end form contents, one file that processes the form once the user hits the submit button and a thank you page after the form gets sent to let the user know that the form has been submitted. 一个文件包含前端表单的内容,一个文件在用户单击“提交”按钮后处理该表单,并在发送表单后向用户发送感谢页面,以告知用户表单已提交。 Here is a demo below. 这是下面的演示。

HTML: HTML:

 <form action="processor.php" method="post" id="myForm">
        <label for="firstname"></label>
        <input type="text" name="firstname" placeholder="First Name" id="firstname" required>
        <label for="lastname"></label>
        <input type="text" name="lastname" placeholder="Last Name" id="lastname" required> 
        <label for="email"></label>
        <input type="email" name="email" placeholder="Email" id="email" required>
        <label for="comments"></label>
        <textarea rows="4" cols="32" name="comments" id="comments" placeholder="Questions & Comments"></textarea>
        <input type="submit" value="Submit">
    </form>

PHP (processor.php file) PHP(processor.php文件)

/***********************************************************/
/*******   Set the receipient email address below   ********/
/*******   And set the subject line of the email    ********/
/*$recipient_email = "testemail@yahoo.com";*/
$recipient_email = "testemail@yahoo.com";
$email_subject_line = "Mail from Website";

/***********************************************************/
/***********************************************************/

if(isset($_POST['firstname']))
{
$firstName = $_POST['firstname'];
    $lastName = $_POST['lastname'];
    $email = $_POST['email'];
    $comments = $_POST['comments'];

   if(!empty($firstName) && 
    !empty($lastName) &&
    !empty($email) &&
    !empty($comments))
   {

$message = "Name: $firstName, Lastname: $lastName, Phone: $phoneNumber, 
Email: $email, Comments: $comments";

send_mail($email, $message, $recipient_email, $email_subject_line);

   }
}

function send_mail($email, $message, $recipient_email, $email_subject_line)
{
$to = $recipient_email;
$from = $email;
$subject = $email_subject_line;
$headers = "From: {$email}" . "\r\n" . 'Reply-To:' . $email . "\r\n" . 'X-
Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}

header("Location:thankyoupage.php");

thankyoupage.php (After data has been submitted) Thankyoupage.php(提交数据后)

<div class="thankyoucontainer">
    <h1>Thank you, your message has been submitted.</h1>
    <a href="index.php">Go back to home page</a>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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