发送电子邮件的PHP脚本不起作用

时间:2016-05-25 11:01:53

标签: php html

我在我的页面上创建了一个简单的表单,现在我尝试添加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>

3 个答案:

答案 0 :(得分:0)

首先,提交按钮中缺少name属性。并且php邮件功能错误。

应该是:

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

而不是:

 <div class="form-group">
        <% if(Dealer_cmpny_name_list.size() > 0) 
    {%>
        <label for="Dealer_Company_Name">
            Select Dealer company name:</label>
        <select name="Dealer_Company_Name" id="Dealer_Company_Name" class="form-control">
            <option>Select</option>
            <%for (int i = 0; i < Dealer_cmpny_name_list.size(); i++) 
    {%>
            <option value="<%=Dealer_cmpny_name_list.get(i)%>">
                <%=Dealer_cmpny_name_list.get(i)%></option>
            <%}%>
        </select>
        <% } %>
    </div>

答案 1 :(得分:0)

PHP的默认mail()函数在大多数情况下都不起作用,尤其是对于GMail。这是因为您的电子邮件需要以特殊方式格式化,以便由Google的邮件服务器处理。你最好使用像PHPMailer这样的邮件库。

以下是如何使用来自GMail帐户的PHPMailer发送电子邮件。

    $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";
    }

答案 2 :(得分:0)

我尝试了所有内容,现在我收到了邮件SMTP ERROR:无法连接到服务器并且SMTP连接()失败

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