簡體   English   中英

PHP腳本發送電子郵件不起作用

[英]Php script to sending email doesn't work

我在頁面上創建了一個簡單表單,現在嘗試添加php腳本來發送電子郵件。 不幸的是,它不起作用。 單擊該按鈕后,我希望用戶保持我的立場而無需重定向。

mail_sender.php

<?php 
if(isset($_POST['submit'])){
   $to = "someone@gmail.com"; 
   $from = $_POST['email']; 

   $message = " You received the fallowing message:" . "\n\n" . $_POST['message'];


   mail($to,$message,$from);
   echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>

的HTML

<form action="mail_sender.php" method="post">
    <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
    <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
    <input type="submit" id="submit" value="Send">
</form>

首先,您的“提交”按鈕中缺少“名稱”屬性。 和PHP郵件功能是錯誤的。

它應該是:

$subject = "Your subject";
$headers = "From: $from ";   
mail($to,$subject,$message,$headers);

代替:

mail($to,$message,$from);

PHP的默認mail()函數在大多數情況下不起作用,尤其是對於GMail。 這是因為您的電子郵件需要以一種特殊的方式進行格式化才能被Google的郵件服務器使用。 使用類似PHPMailer的郵件庫會更好。

這是使用PHPMailer從GMail帳戶發送電子郵件的方法。

    $mail = new PHPMailer();

    // ---------- adjust these lines ---------------------------------------
    $mail->Username = "xxx@gmail.com"; // your GMail user name
    $mail->Password = "passwd";  // your GMail Password
    $mail->AddAddress("yyy@gmail.com"); // recipients email
    $mail->FromName = "Your Name"; // readable name

    $mail->Subject = "Subject";
    $mail->Body    = "Body"; 

    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsSMTP(); // use SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->From = $mail->Username;

    //----------------------------------------------------------------------

    if(!$mail->Send())
    {
        echo "mail sent";
    }

我嘗試了所有操作,現在收到消息SMTP錯誤:無法連接到服務器,並且SMTP connect()失敗

        <form action="mail_sender.php" method="post">
        <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
        <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
        <input type="submit" name="submit" id="submit" value="Send">
    </form>

的PHP

<?php 
require "PHPMailer-master/PHPMailerAutoload.php";

$mail = new PHPMailer();

$to = "someone@gmail.com"; // required
$from = $_POST['email']; 

$comment = 
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;


$mail->Username = "someone@gmail.com"; // your GMail user name
$mail->Password = "Password";  // your GMail Password
$mail->AddAddress("someone@gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body  = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';

//----------------------------------------------------------------------

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
 }    else {
echo 'Message has been sent';
  }
  ?>

暫無
暫無

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

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