我正在尝试创建一个函数,可以检查数据库中是否有活动状态为1的活动客户,他们应该接收电子邮件。 对于电子邮件功能,我使用过PHP Mailer功能。
以下是我的剧本:
<?php
include '../mailer/class.phpmailer.php';
$sqlx = mysql_query("SELECT * from `cust");
$numRows = mysql_num_rows($sqlx);
$mail_body = '';
while($row = mysql_fetch_array($sqlx))
{
// fetch email
$uid = $row["uid"];
$email = $row["email"];
$count = "20";
if($count <= '70')
{
$f_name = "abc";
$f_email = "abc@xyz.com";
$mail_body = "Hii message";
$subject = "Hi you got notificaiton";
$headers = "From: abc <abc@xyz.com>";
$headers .= "Content-type: text/html\r\n";
function Send_mail($email, $subject, $headers, $mail_body, $f_email, $f_name)
{
$mail = new PHPMailer();
$Email = $email;
$fname = $f_name;
$femail = $f_email;
//==================smtp mail ===============================//
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Port= 25; // Sets the default SMTP server port.
$mail->SMTPSecure= 'tls'; // Options are "", "ssl" or "tls"
$mail->Host = 'localhost'; // SMTP server
$mail->Username = 'abc@xyz.com'; // Sets SMTP username.
$mail->Password = '1234556'; //Sets SMTP password.
//=========================================================//
$fname = $f_name;
$femail = $f_email; // email address of reciever
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject; // subject of mail
$mail->Body = $mail_body; // body of mail
$mail->Send(); return true;
}
$mail_result=Send_Mail($email, $subject, $headers, $mail_body,$f_email, $f_name);
}
}
?>
&#13;
但是我收到了错误:
您必须至少提供一个收件人电子邮件地址。 致命错误:无法在第161行的C:\ xampp \ htdocs \ testing \ update.php中重新声明Send_mail()(之前在C:\ xampp \ htdocs \ testing \ update.php:161中声明)
答案 0 :(得分:1)
将你的函数放在循环之外。将你的代码结构改为此。
function Send_mail($email, $subject, $headers, $mail_body, $f_email, $f_name)
{
................//code
$mail->Send(); return true;
}
while($row = mysql_fetch_array($sqlx))//use mysqli or PDO
{
.......//code
if($count <= '70')
{
.......//code
$mail_result=Send_mail($email, $subject, $headers, $mail_body,$f_email, $f_name);
}
}
答案 1 :(得分:1)
检查客户表,必须输入您要在其上发送电子邮件的电子邮件地址。并更改您的
$mail->Port= 587;
$mail->SMTPSecure= 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'abc@gmail.com';//Valid Gmail address
$mail->Password = '1234556';//Gmail password
如果仍然出现错误,则可以转到gmail帐户设置并允许安全的应用程序身份验证。
答案 2 :(得分:0)
将您的功能置于循环之外。这是代码结构:
<?php
include '../mailer/class.phpmailer.php';
function Send_Mail($email, $subject, $headers, $mail_body, $f_email, $f_name)
{
$mail = new PHPMailer();
$Email = $email;
$fname = $f_name;
$femail = $f_email;
//==================smtp mail ===============================//
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Port= 25; // Sets the default SMTP server port.
$mail->SMTPSecure= 'tls'; // Options are "", "ssl" or "tls"
$mail->Host = 'localhost'; // SMTP server
$mail->Username = 'abc@xyz.com'; // Sets SMTP username.
$mail->Password = '1234556'; //Sets SMTP password.
//=========================================================//
$fname = $f_name;
$femail = $f_email; // email address of reciever
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;// subject of mail
$mail->Body = $mail_body; // body of mail
$mail->Send();
return true;
}
$sqlx = mysql_query("SELECT * from `cust");
$numRows = mysql_num_rows($sqlx);
$mail_body = '';
while($row = mysql_fetch_array($sqlx))
{
// fetch email
$uid = $row["uid"];
$email = $row["email"];
$count = "20";
if($count <= '70')
{
$f_name = "abc";
$f_email = "abc@xyz.com";
$mail_body = "Hii message";
$subject = "Hi you got notificaiton";
$headers = "From: abc <abc@xyz.com>";
$headers .= "Content-type: text/html\r\n";
$mail_result=Send_Mail($email, $subject, $headers, $mail_body,$f_email, $f_name);
}
}
&GT;