簡體   English   中英

如何使用materializeCSS和PHP發送電子郵件

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

非常感謝您閱讀本文,我確實是編程的初學者,並且在我嘗試制作的簡單網站上需要一些幫助。 我的目標是制作一個向我發送郵件的表單,但是當我按send時,它會將我發送回頁面頂部。 我在下面粘貼HTML和php。 我要問的另一個問題是,是否需要在我的網絡托管服務(ovh)上設置某些內容才能使php正常工作? 謝謝大家,再見:)!

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 
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.";

}
?>

好慢的開始,這里有一些錯誤提交表單是與客戶端和服務器端的特殊對話框,在這種情況下是服務器端PHP,form標簽首先需要您要用來發送數據的所有方法,例如POST或POST。或GET,但您必須在html FORM TAG中看到它...因此您的html form begin標記變為

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

第二個問題是您要在哪里發送數據?在哪個文件上?而且還必須在開始使用操作時以表格形式寫入數據,這樣您的表格就變成了。

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

如果該動作在同一頁面中,則該動作可以保留為空,因此action =“”之后,您無需編寫

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

也因為是錯誤,所以您必須使用html名稱,因此必須使用$ _POST ['action'],並且必須在發布時寫入$ _POST

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

AHHH和IMHO的意見最好使用輸入type =“ submit”

通過PHPMailer最佳選擇發送電子郵件的簡便方法。

看一下這個 ! https://github.com/PHPMailer/PHPMailer

您必須定義表單操作,當單擊按鈕時將執行什么php腳本,並且顯然要發送郵件,您的服務器或主機確實需要一些配置

<form action="thephpfilename.php">

</form>

謝謝大家為初學者提供的幫助,不勝感激。 我按照您的說明和網絡教程修改了一些代碼,但現在已發送了一封郵件,但完全是空的,似乎我的變量為空。 謝謝

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

<?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é';
}
// }
?>

我終於回答了我自己的問題,謝謝您的時間和幫助:)再見

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:

<?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';
}
// }
?>

這是可行的,即使很亂也可以隨意使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM