使用 HTML 发送邮件 [重复]
Posted
技术标签:
【中文标题】使用 HTML 发送邮件 [重复]【英文标题】:Sent mail using HTML [duplicate] 【发布时间】:2018-06-27 08:04:42 【问题描述】:我正在构建一个简单的 html 网站,我必须从联系我们表单中发送电子邮件。我不知道如何使用 html 发送邮件。我正在尝试使用 php 以显示成功消息的方式发送邮件,但邮件既没有在收件箱中也没有在垃圾邮件中接收。是否有任何替代方案可以从 php 中逃脱,或者是否有任何免费的 api 来发送邮件。 这是php代码。
<?php
if(isset($_POST['email']))
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";
function died($error)
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))
died('We are sorry, but there appears to be a problem with the form you submitted.');
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]2,4$/';
if(!preg_match($email_exp,$email_from))
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name))
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
if(!preg_match($string_exp,$last_name))
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
if(strlen($comments) < 2)
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
if(strlen($error_message) > 0)
died($error_message);
$email_message = "Form details below.\n\n";
function clean_string($string)
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
?>
【问题讨论】:
【参考方案1】:如果我正确理解您的问题,您的页面上有一个联系表,填写并提交后,应该会向您的收件箱发送一封电子邮件以供您处理。如果是这种情况,我真的建议您查看https://formspree.io/。 Formspree 的工作方式不是您处理 POST 响应,而是直接将其发送到 Formspree。他们会收到回复,为您处理,然后立即通过电子邮件将内容发送给您。
您不必担心运行服务器或类似的事情,而且您可以保证始终收到电子邮件。此外,每月最多可免费发送 1000 封电子邮件。
【讨论】:
【参考方案2】:试试这个代码!我已经测试并工作了!
创建新文件contact.php并将其添加到文件中! 您可以稍后自定义 HTML/CSS!
<?php
if(isset($_POST['submit']))
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
【讨论】:
以上是关于使用 HTML 发送邮件 [重复]的主要内容,如果未能解决你的问题,请参考以下文章