使用相同的脚本在提交时从 html 表单发送带有 PHP 的电子邮件 [英] Send email with PHP from html form on submit with the same script

查看:13
本文介绍了使用相同的脚本在提交时从 html 表单发送带有 PHP 的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户填写完 HTML 表单然后通过电子邮件从表单发送信息时使用 PHP 发送电子邮件.我想从显示具有表单的网页的同一个脚本中执行此操作.

I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that displays the web page that has the form.

我找到了这个代码,但是邮件没有发送.

I found this code, but the mail does not send.

<?php 

if (isset($_POST['submit'])) {
    $to = $_POST['email']; 
    $subject = $_POST['name'];
    $message = getRequestURI();
    $from = "zenphoto@example.com";
    $headers = "From:" . $from;

    if (mail($to, $subject, $message, $headers)) {
        echo "Mail Sent.";
    }
    else {
        echo "failed";
    }
}

?>

用 PHP 发送电子邮件的代码是什么?

What is the code to send an email in PHP?

推荐答案

EDIT (#1)

如果我理解正确,您希望将所有内容都放在一个页面中并从同一页面执行.

EDIT (#1)

If I understand correctly, you wish to have everything in one page and execute it from the same page.

您可以使用以下代码从单个页面发送邮件,例如 index.phpcontact.php

You can use the following code to send mail from a single page, for example index.php or contact.php

这个和我原来的答案之间的唯一区别是 <form action="" method="post"> 其中动作被留空.

The only difference between this one and my original answer is the <form action="" method="post"> where the action has been left blank.

最好在 PHP 处理程序中使用 header('Location:thank_you.php'); 而不是 echo 之后将用户重定向到另一个页面.

It is better to use header('Location: thank_you.php'); instead of echo in the PHP handler to redirect the user to another page afterwards.

<?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:" . "

" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "

" . $_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> 

<小时>

原答案

<小时>

我不太确定问题是什么,但我的印象是该消息的副本将发送给填写表格的人.


Original answer


I wasn't quite sure as to what the question was, but am under the impression that a copy of the message is to be sent to the person who filled in the form.

这是 HTML 表单和 PHP 处理程序的测试/工作副本.这使用了 PHP mail() 函数.

Here is a tested/working copy of an HTML form and PHP handler. This uses the PHP mail() function.

PHP 处理程序还会将消息的副本发送给填写表单的人.

The PHP handler will also send a copy of the message to the person who filled in the form.

如果您不打算使用它,您可以在一行代码前使用两个正斜杠 //.

You can use two forward slashes // in front of a line of code if you're not going to use it.

例如: //$subject2 = "Copy of your form submit"; 不会执行.

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="mail_handler.php" 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>

<小时>

PHP 处理程序 (mail_handler.php)

(使用来自 HTML 表单的信息并发送电子邮件)


PHP handler (mail_handler.php)

(Uses info from HTML form and sends the Email)

<?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:" . "

" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "

" . $_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.
    // You cannot use header and echo together. It's one or the other.
    }
?>

<小时>

以 HTML 格式发送:

如果您希望以 HTML 格式发送邮件并且对于这两种情况,则需要使用不同的变量名称创建两组单独的 HTML 标头.

If you wish to send mail as HTML and for both instances, then you will need to create two separate sets of HTML headers with different variable names.

阅读有关 mail() 的手册以了解如何以 HTML 格式发送电子邮件:

Read the manual on mail() to learn how to send emails as HTML:

脚注:

  • 关于 HTML5

您必须使用 action 属性指定将处理提交数据的服务的 URL.

You have to specify the URL of the service that will handle the submitted data, using the action attribute.

https://www.w3.org/TR/html5/forms所述.html 4.10.1.3 配置表单与服务器通信.如需完整信息,请参阅页面.

As outlined at https://www.w3.org/TR/html5/forms.html under 4.10.1.3 Configuring a form to communicate with a server. For complete information, consult the page.

因此,action="" 在 HTML5 中不起作用.

Therefore, action="" will not work in HTML5.

正确的语法是:

  • action="handler.xxx"
  • action="http://www.example.com/handler.xxx".

请注意,xxx 将是用于处理进程的文件类型的扩展名.这可以是 .php.cgi.pl.jsp 文件扩展名等.

Note that xxx will be the extension of the type of file used to handle the process. This could be a .php, .cgi, .pl, .jsp file extension etc.

如果发送邮件失败,请参考以下 Stack 问答:

Consult the following Q&A on Stack if sending mail fails:

这篇关于使用相同的脚本在提交时从 html 表单发送带有 PHP 的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆