PHP自动发送电子邮件不起作用

时间:2014-01-24 14:06:15

标签: php mysql sql email

我写了一些代码,只需点击一下按钮,就可以将电子邮件自动发送到用户的电子邮件地址。但由于某种原因,它没有发送任何电子邮件,我已经用两个不同的电子邮件地址进行了测试。

按钮代码:

form action="sendellie.php" method="post">
<input type="submit" value="Buy tickets"/>
</form>

处理页面的代码:

<?php
session_start();
    include 'connect.php';
     ini_set("sendmail_from", "********");

    $user_id= $_SESSION['id'];
    $sql = "SELECT username FROM user WHERE user_id = '$user_id'";

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uname = mysql_fetch_array($result);

     $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uemail = mysql_fetch_array($result);

    $user_id= $_SESSION['id'];
    $sql = "SELECT email FROM user WHERE user_id = '$user_id'";

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    $uemail = mysql_fetch_array($result);

     while($row = mysql_fetch_array($result))
     {
        $name = $uname['username'];
        $email = $uemail['email'];
        $message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
        $subject = $uname['username'];
        echo "sent to"." ".$email." ".$subject." ".$message."<p />";

        mail($email, $subject, $message);
        }

        echo "Go check your mail box:";
        include 'close.php';
        ?>
    <?php echo $uemail['email'] ?><br>
    <a href="ellie.php">Back</a>

2 个答案:

答案 0 :(得分:0)

这应该有用(请查看下面的注释):

<?php
session_start();
include 'connect.php';
ini_set("sendmail_from", "********");

$user_id= $_SESSION['id'];
$sql = "SELECT username FROM user WHERE user_id = '$user_id'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uname = mysql_fetch_array($result);

 $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);

$user_id= $_SESSION['id'];
$sql = "SELECT email FROM user WHERE user_id = '$user_id'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);

 while($row = mysql_fetch_array($result))
 {
    $name = $uname['username'];
    $email = $uemail['email'];
$youremail = "youremail@mail.com";
$yoursubject = " Subject of the email";
    $message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
    $subject = $uname['username'];

     mail( $email , $yoursubject, $message, "From:" . $youremail);

    echo "sent to"." ".$email." ".$yoursubject." ".$message."<p />";
    }

    echo "Go check your mail box:";
    include 'close.php';
    ?>
<?php echo $uemail['email'] ?><br>
<a href="ellie.php">Back</a>

注意:

PHP mail()函数 语法

mail(to,subject,message,headers,parameters) 

答案 1 :(得分:0)

我使用此代码发送邮件试试这个

<html>
    <body>

    <?php
    if (isset($_REQUEST['email']))

      {

      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("someone@example.com", $subject,
      $message, "From:" . $email);
      echo "Thank you for using our mail form";
      }
    else

      {
      echo "<form method='post' action='mailform.php'>
      Email: <input name='email' type='text'><br>
      Subject: <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'>
      </textarea><br>
      <input type='submit'>
      </form>";
      }
    ?>

    </body>
    </html>