繁体   English   中英

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

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

我一直在尝试学习HTML的基础知识,为此,我一直在设计自己的基本网页。 我根本不打算将其公开,但是很好地帮助我学习HTML和CSS的所有不同方面。 为此,我在其中一个页面上添加了“联系人”部分,其中既包含电子邮件地址,又包含允许发送电子邮件的表格,我使用了W3Schools上的其中一篇教程来创建它。 为此,我删除了自己的电子邮件地址,并用someone@example.com替换了它,但这是该代码的此特定部分的输出:

电子邮件

但是,每当我尝试填写表单进行测试时,都会收到此弹出消息。 如果单击“取消”,则什么也不会发生,但是,如果单击“确定”,则会打开计算机上的邮件应用程序。 但是,我在表格中键入的消息不存在,并且我在框中键入要发送的电子邮件地址只是更改为计算机上的默认地址。

弹出

那么,如何防止出现此弹出消息并仅向我发送电子邮件呢?

以下是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>

如果HTML不足以从网页发送电子邮件,而我需要另一种语言来编写可以做到这一点的程序,则我在Python方面相当称职,并且在某种程度上我知道C#。 但是,我从未使用过JavaScript,PHP,Perl或其他任何东西(我不知道哪种语言合适)

如果您想提交表格以发送电子邮件,那真的很简单。 您可以使用简单的服务器端语言,例如PHP。 您将需要3个文件。 一个文件包含前端表单的内容,一个文件在用户单击“提交”按钮后处理该表单,并在发送表单后向用户发送感谢页面,以告知用户表单已提交。 这是下面的演示。

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文件)

/***********************************************************/
/*******   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(提交数据后)

<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