使用 php 发送确认邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6142433/
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
Send a confirmation email using php
提问by Craig Eves
I am using the script below to return an email with the submitted fields to my email address.
我正在使用下面的脚本将包含已提交字段的电子邮件返回到我的电子邮件地址。
I have recently found the need to have a confirmation email sent to who ever submitted the form in the first place but am unsure on how to alter the script. I basically need it to say something like "Dear $name, Thanks for contacting us....".
我最近发现需要向最初提交表单的人发送一封确认电子邮件,但不确定如何更改脚本。我基本上需要它说“亲爱的 $name,感谢您与我们联系......”。
Please can someone help?
请问有人可以帮忙吗?
<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Website Enquiry';
// Your email address. This is where the form information will be sent.
$emailadd = '[email protected]';
// Where to redirect after form is processed.
$url = 'thanks.php';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "WEBSITE ENQUIRY:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
The script allows for any form element to be sent in the email without the need of including it within the php script.
该脚本允许在电子邮件中发送任何表单元素,而无需将其包含在 php 脚本中。
The email address is collected in the html
电子邮件地址收集在 html 中
<span class="input">
<input type="text" name="Email" id="Email"/>
</span>
and the name...
还有这个名字……
<span class="input">
<input type="text" name="Name" id="Name" />
</span>
Many thanks
非常感谢
Craig
克雷格
回答by Kalessin
This code will send a confirmation to the user who submitted the form, but it doesn't check to make sure they've entered a valid email address. There's a handy script at isemail.infowhich will validate that for you.
此代码将向提交表单的用户发送确认,但不会检查以确保他们输入了有效的电子邮件地址。isemail.info 上有一个方便的脚本,可以为您验证。
Insert the code just above the ---Do not edit below this line---
line.
将代码插入该---Do not edit below this line---
行正上方。
// Subject of confirmation email.
$conf_subject = 'Your recent enquiry';
// Who should the confirmation email be from?
$conf_sender = 'Organisation Name <[email protected]>';
$msg = $_POST['Name'] . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
mail( $_POST['Email'], $conf_subject, $msg, 'From: ' . $conf_sender );