无法使PHP邮件功能正常工作

时间:2016-02-11 19:17:20

标签: php

在将其添加到我的网站之前,尝试使用以下代码实现简单的php邮件功能。收到此错误

错误:1

  

您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的“1”附近使用正确的语法

<?php 
global $post;
$post_data = get_post($post->post_parent);

if ($post_data->post_name == 'in-the-city'){
    $ticket = 'Cats';
} elseif ($post_data->post_name == 'on-the-beach') {
    $ticket = 'Dogs';   
} else {
    $ticket = 'Birds';  
}

echo $ticket;
?>

只是寻找任何形式的洞察力或明显的错误即时通讯或如果我完全错误地完成它。感谢。

1 个答案:

答案 0 :(得分:0)

首先,你不要以好的方式使用$ sql。 以下是此电子邮件所需的内容

$servername = "localhost";
$dbusername = "dbname";
$dbpassword = "password";
$dbname = "dbname";

            // Create connection
            $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 
            // END CREATING CONNECTION

从DB中选择信息:

$sql = "SELECT * FROM users'";
        $result = $conn->query($sql);


                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) { 
                        $username = $row['username'];
                        $email = $row['email'];
                    }
                }  

电子邮件格式:

    $to = "$username <$email>";
    $subject = 'Subject';
    $message = "<html>Hello $username, <br /><br />
        YOUR MESSAGE
        </html>";

    $mimeheader = 'MIME-Version: 1.0 '."\r\n".
                'Content-type: text/html; charset="iso-8895-1" '."\r\n".
                'Content-Transfer-Encoding: 7bit '."\r\n";
    $mimeheader = 'From: YOURNAME <YOUREMAIL> '."\r\n";


    $mimeheader .= "MIME-Version: 1.0\r\n";
    $mimeheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    if( mail($to, $subject, $message, $mimeheader) ){


    }else{
        echo 'Mail not delivered.';
    }