简体   繁体   English

如何使用materializeCSS和PHP发送电子邮件

[英]How can I send an E-mail using materializeCSS and PHP

and thank you for reading this, I'm really a beginner in programming and I need some help on a simple website I'm trying to make. 非常感谢您阅读本文,我确实是编程的初学者,并且在我尝试制作的简单网站上需要一些帮助。 My goal is to make a form that sends me a mail but when I press send, it justs sends me back to the top of the page. 我的目标是制作一个向我发送邮件的表单,但是当我按send时,它会将我发送回页面顶部。 I'm pasting HTML and php below. 我在下面粘贴HTML和php。 Another question that I'm asking you guys is Do I have to set something on my web hosting service (ovh) for php to work ? 我要问的另一个问题是,是否需要在我的网络托管服务(ovh)上设置某些内容才能使php正常工作? Thank you all, bye :) ! 谢谢大家,再见:)!

html : html:

<form class="col s12">
  <div class="row">
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="first_name" type="text" class="validate">
      <label for="first_name">Prénom</label>
    </div>
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="last_name" type="text" class="validate">
      <label for="last_name">Nom de famille</label>
    </div>
  </div>
  <div class="row">
    <div class="input-field col s12">
      <i class="material-icons prefix">phone</i>
      <input id="Mail" type="email" class="validate">
      <label for="Mail">E-mail</label>
    </div>
  </div>
    <div class="row">
        <form class="col s12">
        <div class="row">
            <div class="input-field col s12">
            <i class="material-icons prefix">chat</i>
            <textarea id="message" class="materialize-textarea"></textarea>
            <label for="message">Message</label>
            </div>
        </div>
<button class="btn waves-effect waves-light" type="submit" 
name="action">Envoyer
<i class="material-icons right">send</i>
</button>
        </form>

PHP : PHP的:

<?php 
if(isset($_POST['submit'])){
$to = "myemail@gmail.com";
$from = $_POST['Mail'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Mail subject";
$message = $first_name . " " . $last_name . " à écrit ceci:" . "\n\n" . 
$_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you 
shortly.";

}
?>

Well start slow, here there are some errors a submit form is a special dialog with the client and the server side in this case server side PHP, the form tag need first of all the method that you want to use to send data so or POST or GET but you must see this in the html FORM TAG...so your html form begin tag become 好慢的开始,这里有一些错误提交表单是与客户端和服务器端的特殊对话框,在这种情况下是服务器端PHP,form标签首先需要您要用来发送数据的所有方法,例如POST或POST。或GET,但您必须在html FORM TAG中看到它...因此您的html form begin标记变为

<form class="col s12" method="post">

The second problem is where you want to send data?At which file?and also this must be written in the form at the begin using action, so your form become. 第二个问题是您要在哪里发送数据?在哪个文件上?而且还必须在开始使用操作时以表格形式写入数据,这样您的表格就变成了。

 <form class="col s12" method="post" action="nameOfFilePHPWhereSendData.php">

if the action is in the same page the action can be leave empty so action="" After you don't need to write 如果该动作在同一页面中,则该动作可以保留为空,因此action =“”之后,您无需编写

   if( isset($_POST['submit']))

also because is an error you must use the html name so $_POST['action' ]and you must write in post your $_POST 也因为是错误,所以您必须使用html名称,因此必须使用$ _POST ['action'],并且必须在发布时写入$ _POST

 $from = $_POST['Mail'];
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];

AHHH and IMHO opinion is better use input type="submit" AHHH和IMHO的意见最好使用输入type =“ submit”

Simple and easy way to send emails via PHPMailer best option. 通过PHPMailer最佳选择发送电子邮件的简便方法。

Check this out ! 看一下这个 ! https://github.com/PHPMailer/PHPMailer https://github.com/PHPMailer/PHPMailer

you have to define the form action, when the button click what php script would be executed, and obviously to send mail, your server or hosting need some configuration indeed 您必须定义表单操作,当单击按钮时将执行什么php脚本,并且显然要发送邮件,您的服务器或主机确实需要一些配置

<form action="thephpfilename.php">

</form>

Thank you all for trying to help a beginner, appreciated. 谢谢大家为初学者提供的帮助,不胜感激。 I modified a little my code following your instructions and web tutorials but now a mail is sent but it's completely emtpty, seems like my variables are empty. 我按照您的说明和网络教程修改了一些代码,但现在已发送了一封邮件,但完全是空的,似乎我的变量为空。 Thanks 谢谢

index.html index.html

<form class="col s12" method="POST" action="mail.php">
  <div class="row">
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="first_name" type="text" class="validate" name="first_name">
      <label for="first_name">Prénom</label>
    </div>
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="last_name" type="text" class="validate" name="last_name">
      <label for="last_name">Nom de famille</label>
    </div>
  </div>
  <div class="row">
    <div class="input-field col s12">
      <i class="material-icons prefix">phone</i>
      <input id="mail" type="email" class="validate" name="email">
      <label for="mail">E-mail</label>
    </div>
  </div>
    <div class="row">
            <div class="input-field col s12">
            <i class="material-icons prefix">chat</i>
            <textarea id="message" class="materialize-textarea"></textarea>
            <label for="message">Message</label>
            </div>
  <button class="btn waves-effect waves-light" type="submit" name="action" id="submit" for="submit">Envoyer
<i class="material-icons right">send</i>
  </button>
    </div>
</form>

mail.php mail.php

<?php
error_reporting(E_ALL);
// if (isset($_POST['submit'])) {  <<-- not an error, I just wanted to get rid of it w/o deleting it
$prenom = !empty($_POST['first_name']) ? $_POST['first_name'] : NULL;
$nom = !empty($_POST['last_name']) ? $_POST['last_name'] : NULL;
$from = !empty($_POST['mail']) ? $_POST['mail'] : NULL;
$msg = !empty($_POST['message']) ? $_POST['message'] : NULL;

if(mail('hugoleon2002@gmail.com', 'Commande Amarrex', $msg))
{
     echo 'Le message a été envoyé';
}
else
{
     echo 'Le message n\'a pu être envoyé';
}
// }
?>

I finally answered my own question, thank you for your time and help :) bye bye 我终于回答了我自己的问题,谢谢您的时间和帮助:)再见

index.html : index.html:

<form class="col s12" method="POST" action="mail.php">
  <div class="row">
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="first_name" type="text" class="validate" name="first_name">
      <label for="first_name">Prénom</label>
    </div>
    <div class="input-field col s6">
      <i class="material-icons prefix">info</i>
      <input id="last_name" type="text" class="validate" name="last_name">
      <label for="last_name">Nom de famille</label>
    </div>
  </div>
  <div class="row">
    <div class="input-field col s6">
      <i class="material-icons prefix">mail</i>
      <input id="mail" type="email" class="validate" name="email">
      <label for="mail">E-mail</label>
    </div>
    <div class="input-field col s6">
      <i class="material-icons prefix">phone</i>
      <input id="phone" type="tel" name="phone">
      <label for="phone">Téléphone (optionel)</label>
    </div>
  </div>
    <div class="row">
            <div class="input-field col s12">
            <i class="material-icons prefix">chat</i>
            <textarea id="message" class="materialize-textarea" name="message"></textarea>
            <label for="message">Message</label>
            </div>
<button class="btn waves-effect waves-light" type="submit" name="action" id="submit" for="submit">Envoyer
<i class="material-icons right">send</i>
</button>
    </div>
</form>

mail.php : mail.php:

<?php
$prenom = !empty($_POST['first_name']) ? $_POST['first_name'] : NULL;
$nom = !empty($_POST['last_name']) ? $_POST['last_name'] : NULL;
$from = !empty($_POST['email']) ? $_POST['email'] : NULL;
$msg = !empty($_POST['message']) ? $_POST['message'] : NULL;
$tel = !empty($_POST['phone']) ? $_POST['phone'] : NULL;
$headers = 'From: WEBSITE E-MAIL';
//  echo "$msg" . "$nom" . "$from";

if(empty($prenom) || empty($nom) || empty($from) || empty($msg)) 
{
     echo 'Mail couldn't be send, a fiel is empty';
}
elseif(mail('EMAIL ADRESS', "Commande Amarrex de $prenom $nom", "$prenom $nom a ecrit : $msg \n\n\n E-mail de contact : $from\n\n Telephone : $tel", "$headers"))
{
     echo 'Mail sent.';
}
else
{
    echo 'mail not sent, unexpected error';
}
// }
?>

This is working, feel free to use it even if it's pretty messy 这是可行的,即使很乱也可以随意使用

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

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