使用php,mysql发送电子邮件

时间:2014-02-14 15:17:05

标签: php mysql email

我需要向用户发送一封电子邮件,通知他们报告即将过期。

我到目前为止设置的代码但它没有发送电子邮件,也没有通过浏览器发送任何错误消息,任何人都可以告诉我哪里出错了。

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include 'connect_mobile.php'; 
/*
$today = $_POST['reportdate'];
$name = $_POST['engineername'];
$engineerid = $_POST['engineerid'];
$engineeremail = $_POST['engineeremail'];
*/
$today = '2013-03-14';
$name = 'John Wheeler';
$engineerid = '130';
$engineeremail = 'jwheating@gmail.com';


$to = $engineeremail;
$subject = "Report expiring 30 days";

// give your message the starting string
$message = 'Greetings '.$name.',

    you are receiving this email as a reminder that the following reports expire in 30  days:
    <table style="width: 80%;">
        <tr>
            <td>Report No</td>
            <td>Client</td>
            <td>Property Address</td>
            <td>Address</td>
            <td>Address</td>
            <td>Postcode</td>
            <td>Expiry Date</td>
        </tr>
'
$query = "SELECT * FROM certifs WHERE currentdate = '"$today"' AND userid = '"$engineerid"'"
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    $message .= "        <tr>";
    $message .= "            <td>".$row['certno']."</td>";
    $message .= "            <td>".$row['clientcompany']."</td>";
    $message .= "            <td>".$row['propertyadd1']."</td>";
    $message .= "            <td>".$row['propertyadd2']."</td>";
    $message .= "            <td>".$row['propertyadd3']."</td>";
    $message .= "            <td>".$row['propertypostcode']."</td>";
    $message .= "            <td>".$row['currentdate']."</td>";
    $message .= "        </tr>";

    // then update the message with the ending
    $message .= "
    </table>

    Thank you,
    John Wheeler Gas Reports."

}
//-- The headers will let us send HTML code as an email
$headers = "From: support@john-wheeler.co.uk\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


//-- if mail gets sent, return true, else return false.
if (mail($to,$subject,$message,$headers))
{
$response = array('mail' => true);
}
else
{
$response = array('mail' => false);
}

echo json_encode($response);

}
?>

嗨,谢谢大家指出我的愚蠢错误,我现在纠正了语法,至少我得到了一条错误信息。

Warning: mysql_query(): Access denied for 'user'johnny'@'localhost' (using password:NO) in /var/www/vhosts/vvps-835282.dailyvps.co.uk/john-wheeler.co.uk/mobilereports/reportexpires.php on line 35 Warning: mysql_query(): A link to the server could not be established in /var/www/vhosts/vvps-835282.dailyvps.co.uk/john-wheeler.co.uk/mobilereports/reportexpires.php on line 35 Access denied for user 'johnny'@'localhost' (using password: NO)

我该如何格式化代码?对不起第一次使用StackOverflow。

代码在我的VPS上,我有其他邮件脚本使用邮件正常运行。

4 个答案:

答案 0 :(得分:1)

$message = 'Greetings '.$name.',

    you are receiving this email as a reminder that the following reports expire in 30  days:
    <table style="width: 80%;">
        <tr>
            <td>Report No</td>
            <td>Client</td>
            <td>Property Address</td>
            <td>Address</td>
            <td>Address</td>
            <td>Postcode</td>
            <td>Expiry Date</td>
        </tr>
'

你错过了;。您没有收到任何错误消息,因为它是语法错误。

在此行的末尾

$query = "SELECT * FROM certifs WHERE currentdate = $today AND userid = $engineerid"

答案 1 :(得分:0)

试试这个,你在

中有一些错误
 $query = "SELECT * FROM certifs WHERE currentdate = '"$today"' AND userid = '"$engineerid"'"
                                                  ....^

在某些地方错过了添加;,我已经纠正了相同的情况并使用了下面的一个,

替换:

    // give your message the starting string
    $message = 'Greetings '.$name.',

        you are receiving this email as a reminder that the following reports expire in 30  days:
        <table style="width: 80%;">
            <tr>
                <td>Report No</td>
                <td>Client</td>
                <td>Property Address</td>
                <td>Address</td>
                <td>Address</td>
                <td>Postcode</td>
                <td>Expiry Date</td>
            </tr>
    ';
    $query = "SELECT * FROM certifs WHERE currentdate = '$today' AND userid = '$engineerid'";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
        $message .= "        <tr>";
        $message .= "            <td>".$row['certno']."</td>";
        $message .= "            <td>".$row['clientcompany']."</td>";
        $message .= "            <td>".$row['propertyadd1']."</td>";
        $message .= "            <td>".$row['propertyadd2']."</td>";
        $message .= "            <td>".$row['propertyadd3']."</td>";
        $message .= "            <td>".$row['propertypostcode']."</td>";
        $message .= "            <td>".$row['currentdate']."</td>";
        $message .= "        </tr>";

        // then update the message with the ending
        $message .= "
        </table>

        Thank you,
        John Wheeler Gas Reports.";
    }

答案 2 :(得分:0)

    <?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include 'connect_mobile.php'; 
/*
$today = $_POST['reportdate'];
$name = $_POST['engineername'];
$engineerid = $_POST['engineerid'];
$engineeremail = $_POST['engineeremail'];
*/
$today = '2013-03-14';
$name = 'John Wheeler';
$engineerid = '130';
$engineeremail = 'jwheating@gmail.com';


$to = $engineeremail;
$subject = "Report expiring 30 days";

// give your message the starting string
$message = 'Greetings '.$name.',
    you are receiving this email as a reminder that the following reports expire in 30  days:
    <table style="width: 80%;">
        <tr>
            <td>Report No</td>
            <td>Client</td>
            <td>Property Address</td>
            <td>Address</td>
            <td>Address</td>
            <td>Postcode</td>
            <td>Expiry Date</td>
        </tr>
';
$query = "SELECT * FROM certifs WHERE currentdate = '".$today."' AND userid = '".$engineerid."'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    $message .= "        <tr>";
    $message .= "            <td>".$row['certno']."</td>";
    $message .= "            <td>".$row['clientcompany']."</td>";
    $message .= "            <td>".$row['propertyadd1']."</td>";
    $message .= "            <td>".$row['propertyadd2']."</td>";
    $message .= "            <td>".$row['propertyadd3']."</td>";
    $message .= "            <td>".$row['propertypostcode']."</td>";
    $message .= "            <td>".$row['currentdate']."</td>";
    $message .= "        </tr>";

    // then update the message with the ending
    $message .= "
    </table>

    Thank you,
    John Wheeler Gas Reports.";

}
//-- The headers will let us send HTML code as an email
$headers = "From: support@john-wheeler.co.uk\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


//-- if mail gets sent, return true, else return false.
if (mail($to,$subject,$message,$headers))
{
$response = array('mail' => true);
}
else
{
$response = array('mail' => false);
}

echo json_encode($response);
?>

你有很多语法问题......也许就是这样。

答案 3 :(得分:0)

如果您有邮件服务器设置,请将其添加到您的php.ini文件

sendmail_path = /usr/sbin/sendmail -t -i 

用于Linux / Unix系统