PHP从数据库中的地址发送邮件

时间:2012-03-08 09:33:30

标签: php

我读过&尝试了几个帖子,但无法解决这个问题。我正在尝试将邮件发送到数据库中的地址,但收到以下错误:警告:mail()期望参数3为字符串,给定数组...

include 'datalogin.php';
$sql10 = "SELECT * FROM ex_usrs_admin_dept WHERE linked_dept = '$newdept1'";
$res10 = mysql_query($sql10);
while ($row10 = mysql_fetch_assoc($res10)) {
    $adminid1 = $row10['adminid'];

    $elist = mysql_query("SELECT mail_address FROM ex_usrs WHERE id = '$adminid1'");
    $subject = "This is the subject";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

    $message = '<h3>This is the message</h3>';

    ini_set(SMTP, "10.xxx.xxx.xx");
    ini_set(smtp_port, 25);
    ini_set('display_errors', '1');
    ini_set("sendmail_from", "xx@xx.com");

    if (mysql_num_rows($elist) > 0) {
        while ($elist_result = mysql_fetch_array($elist)) {
            $headers = array('To' => $elist_result['mail_address']);
            mail($subject, $message, $headers);
        }
    }
} 

5 个答案:

答案 0 :(得分:1)

选项3必须是一个字符串,但是你给了一个数组:

$headers = array('To' => $elist_result['mail_address']);
mail($subject, $message, $headers);

选项3也是信息。

mail ( string $to , string $subject , string $message [, string $additional_headers ...

查看手册:http://php.net/manual/en/function.mail.php

答案 1 :(得分:1)

您将$ header作为数组:

$headers = array('To' => $elist_result['mail_address']);
mail($subject, $message, $headers);

正确的语法是:

mail($elist_result['mail_address'], $subject, $message);

答案 2 :(得分:1)

不是我做-1。

但你读错了吗?它解释了你的错误。 mail功能的正确用法可在以下位置找到:http://php.net/manual/en/function.mail.php

答案 3 :(得分:1)

根据php规范......首先是param.that你忘记了

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

答案 4 :(得分:0)

您混淆了邮件功能的参数......您应该发送以下内容:

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

注意开头的$to字段。从

更改while循环
$headers = array('To' => $elist_result['mail_address']);
mail($subject, $message, $headers);

mail($elist_result['mail_address'],$subject, $message, $headers);

The PHP documentation is very detailed