简体   繁体   English

使用带有html表单数据的php发送电子邮件

[英]Send Email with php with html form data

I have no idea how php works.我不知道 php 是如何工作的。 Im using JavaScript for every logic but its only for client use and php is for server i think.我对每个逻辑都使用 JavaScript,但我认为它仅用于客户端,而 php 用于服务器。 Im getting a 405 (Method Not Allowed) error.我收到 405(不允许的方法)错误。

How can i send a email (Example: "example@gmail.com") to myself with the input data from a html form?如何使用来自 html 表单的输入数据向自己发送电子邮件(例如:“example@gmail.com”)?

 //** IF u need it ** // Working Contact Form $('#contact-form').submit(function() { var action = $(this).attr('action'); $("#message").slideUp(750, function() { $('#message').hide(); $('#submit').before('').attr('disabled', 'disabled'); $.post(action, { name: $('#name').val(), email: $('#email').val(), comments: $('#comments').val(), }, function(data) { document.getElementById('message').innerHTML = data; $('#message').slideDown('slow'); $('#cform img.contact-loader').fadeOut('slow', function() { $(this).remove() }); $('#submit').removeAttr('disabled'); if (data.match('success') != null) $('#cform').slideUp('slow'); } ); }); return false; }); // Example starter JavaScript for disabling form submissions if there are invalid fields (function() { 'use strict' window.addEventListener('load', function() { // Fetch all the forms we want to apply custom Bootstrap validation styles to var forms = document.getElementsByClassName('needs-validation') // Loop over them and prevent submission Array.prototype.filter.call(forms, function(form) { form.addEventListener('submit', function(event) { console.log("test") if (form.checkValidity() === false) { event.preventDefault() event.stopPropagation() } form.classList.add('was-validated') }, false) }) }, false) }())
 //Ignore CSS, doesnt matter
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="post" action="php/contact.php" name="contact-form" id="contact-form"> <div class="row"> <div class="col-lg-6"> <div class="row"> <div class="col-lg-12 col-md-6"> <div class="form-group"> <input name="name" id="name" type="text" class="form-control border rounded" placeholder="Name :"> </div> </div> <!--end col--> <div class="col-lg-12 col-md-6"> <div class="form-group"> <input name="email" id="email" type="email" class="form-control border rounded" placeholder="Email :"> </div> </div> <!--end col--> <div class="col-lg-12"> <div class="form-group"> <input name="subject" id="subject" class="form-control border rounded" placeholder="Betreff :"> </div> </div> <!--end col--> </div> <!--end row--> </div> <!--end col--> <div class="col-lg-6"> <div class="form-group"> <textarea name="comments" id="comments" rows="4" class="form-control border rounded" placeholder="Nachricht :"></textarea> </div> </div> <!--end col--> </div> <!--end row--> <div class="row"> <div class="col-sm-12 text-right"> <input type="submit" id="submit" name="send" class="submitBnt btn btn-pill btn-custom" value="Senden"> <div id="simple-msg"></div> </div> <!--end col--> </div> <!--end row--> </form> <!--end form-->

Im using VS-Code with LiveServer extension to create a website.我使用 VS-Code 和 LiveServer 扩展来创建一个网站。 Can i use php there or what is the problem?我可以在那里使用 php 还是有什么问题? Btw, still have a problem with JS i think..?!顺便说一句,我认为 JS 仍然有问题..?!

If anybody know how to help, please comment!如果有人知道如何帮助,请评论! Thank you.谢谢你。

Every .php file opens with the <?php keyword.每个.php文件都以<?php关键字打开。 It indicates that this file contains PHP code.它表明该文件包含 PHP 代码。 HTML can be used in PHP files so there is no different file type for that. HTML 可以在 PHP 文件中使用,因此没有不同的文件类型。 Close the php file with ?> .?>关闭 php 文件。

You could also have single lines of PHP code in for example your HTML.例如,您还可以在 HTML 中包含单行 PHP 代码。

<?php $title = 'Hello World'; ?>
<h1><?php echo $title; ?></h1>

You'll need to collect the data that send to the server.您需要收集发送到服务器的数据。 PHP has global variables which are variables that are available everywhere in any file. PHP 具有全局变量,它们是在任何文件中随处可用的变量。 For example the $_GET variable.例如$_GET变量。 If you use the GET method to send your data, then you will be able to read that data from the global $_GET variable in the file that you send it to.如果您使用GET方法发送数据,那么您将能够从发送到的文件中的全局$_GET变量中读取该数据。

$_GET is an associative array . $_GET是一个关联数组 So sent data is stored like so:所以发送的数据存储如下:

[
  'name' => '',
  'email' => '',
  'comments' => ''
]

Associative arrays are comparable to objects in JavaScript.关联数组类似于 JavaScript 中的对象。 They both have a key and a value.它们都有一个键和一个值。 Check if a key is set with the isset() function in PHP.检查是否使用 PHP 中的isset()函数设置了一个键。

Because you are allowing the user to send data it is important to sanitize their data.因为您允许用户发送数据,所以清理他们的数据很重要。 It can contain malicious code and should be treated as such until you have sanitized it.它可能包含恶意代码,在您对其进行清理之前,应将其视为恶意代码。 filter_var is a PHP function which can do that for you. filter_var是一个 PHP 函数,它可以为您做到这一点。

Now that you have your data and cleaned it you can use the mail function to send a mail to the an email address.现在您已经拥有并清理了数据,您可以使用mail功能向电子邮件地址发送邮件。 The mail function returns a boolean based on if the mail was sent successfully. mail函数根据邮件是否成功发送返回一个布尔值。

Good luck!祝你好运!

<?php

/** 
 * Get all the send data from JS.
 * $_GET is a global PHP variable. It is an array
 * which contains the data sent with the GET method.
 */
$name     = isset($_GET['name']) ? $_GET['name'] : '';
$email    = isset($_GET['email']) ? $_GET['email'] : '';
$comments = isset($_GET['comments']) ? $_GET['comments'] : '';

/**
 * All data that a user can send must be treated as dirty.
 * Therefor it is important to filter all malicious characters
 * from the data. Otherwise you are open to vulnerabilities.
 */
$name     = filter_var($name, FILTER_SANITIZE_STRING);
$email    = filter_var($email, FILTER_SANITIZE_EMAIL);
$comments = filter_var($comments, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

/**
 * Now the data is received and sanitized we can use it
 * to send an email with.
 */
$to      = $email; // The email address to send to.
$from    = 'sender@example.com'; // Your email address.
$subject = 'the subject'; 
$message = $comments; // I assume comments is the message. 
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 

/**
 * Send the email.
 * The mail() function returns a true or false based
 * on if the email was sent successfully.
 */
$mail_status = mail($to, $subject, $message, $headers);

/**
 * Send a message back to JS based on if the mail has beent sent or not.
 */
if ($mail_status === true) {
  return 'Mail has been sent';
} else {
  return 'Sending the mail has failed';
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM