简体   繁体   English

使用php,mysql发送电子邮件

[英]Send email using php ,mysql

I need to send an email to user to notify them of reports expiring. 我需要向用户发送电子邮件,以通知他们报告即将到期。

Code I have set up so far but it is not sending the email nor are there any error messages through the browser, can anyone tell me where I am going wrong. 到目前为止,我已经设置了代码,但是它没有发送电子邮件,也没有通过浏览器显示任何错误消息,任何人都可以告诉我我要去哪里了。

<?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);

}
?>

HI, thank you all for pointing out my silly mistakes, I corrected the syntax now at least I get an error message. 嗨,谢谢大家指出我的愚蠢错误,我现在至少纠正了语法错误消息。

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)

How the hell do I format code? 我该如何格式化代码? Sorry first time on StackOverflow. 抱歉第一次在StackOverflow上。

The code is on my VPS, I have other mail scripts running fine using mail. 该代码在我的VPS上,我还有其他使用脚本运行良好的邮件脚本。

$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>
'

You're missing a ; 你错过了; at the end of this. 在此结束。 You aren't getting any error messages as it's a syntax error. 您没有收到任何错误消息,因为它是语法错误。

and at the end of this line 在该行的末尾

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

Try this, You have some errors in 试试这个,你有一些错误

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

Missed to add ; 错过补充; in some places, I have corrected the same and use below one, 在某些地方,我已将其更正并在下面使用,

Replace: 更换:

    // 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.";
    }
    <?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);
?>

you have a lot of syntax problems... maybe but that. 您有很多语法问题...也许但是。

if you have a mail server setup then add this to your php.ini file 如果您有邮件服务器设置,则将其添加到您的php.ini文件中

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

for Linux/Unix systems 用于Linux / Unix系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM