gpt4 book ai didi

html - 如何从 HTML 表单发送电子邮件

转载 作者:技术小花猫 更新时间:2023-10-29 11:36:16 26 4
gpt4 key购买 nike

我知道有很多示例使用 mailto: post 操作仅使用 html 表单发送电子邮件。

但使用它实际上会弹出发送电子邮件对话框,例如展望对话框。它实际上使用我们自己的 smtp 服务器来发送电子邮件。

有什么方法可以创建在提交后发送电子邮件的 html 表单吗?

有没有javascript api可以实现这个效果?比如说 node.js?

谁能提供一些代码示例?

最佳答案

正如其他人所说,你不能。您可以在网上找到 HTML-php 表单的好例子,这里有一个非常有用的链接,它结合了 HTML 和用于验证的 javascript 以及用于发送电子邮件的 php。

请查看来源中的完整文章(包括 zip 示例): http://www.html-form-guide.com/contact-form/php-email-contact-form.html

HTML:

    <form method="post" name="contact_form"
action="contact-form-handler.php">
Your Name:
<input type="text" name="name">
Email Address:
<input type="text" name="email">
Message:
<textarea name="message"></textarea>
<input type="submit" value="Submit">
</form>

JS:

    <script language="JavaScript">
var frmvalidator = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email",
"Please enter a valid email address");
</script>

PHP:

    <?php
$errors = '';
$myemail = 'yourname@website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>

关于html - 如何从 HTML 表单发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8239782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com