0

I m trying to make a function which can check the database for active customer whose status is 1 if active, they should receive email. For Email Functionality I had used PHP Mailer function.

Below is my script:

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


}

}

?>

But i getting error:

You must provide at least one recipient email address. Fatal error: Cannot redeclare Send_mail() (previously declared in C:\xampp\htdocs\testing\update.php:161) in C:\xampp\htdocs\testing\update.php on line 161

Riya Patel
  • 79
  • 10
  • 3
    Switch to mysqli or PDO. – Suchit kumar Jan 24 '17 at 09:59
  • 1
    Then call your function `Send_mail()` something else as phpMailer obviously has a function with that name. For example `mySend_mail()` – RiggsFolly Jan 24 '17 at 09:59
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 24 '17 at 10:00
  • Also, you are declaring your function as `Send_mail` and then calling it as `Send_Mail` with a capital M – Federico klez Culloca Jan 24 '17 at 10:00
  • 4
    you have written the function `inside while` take it out and call it in loop. – Suchit kumar Jan 24 '17 at 10:00
  • And surely this query `$sqlx = mysql_query("SELECT * from \`cust");`should be generating an error try `$sqlx = mysql_query("SELECT * from \`cust\`");` with matching backticks – RiggsFolly Jan 24 '17 at 10:00
  • its showing that you have not added a recipient email to the method. see the example of Send_Mail method in php.net and look what you missed ! – M. Alim Jan 24 '17 at 10:02
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 24 '17 at 10:02
  • @Suchit how to take out the function? I tried to take out but then also showing error – Riya Patel Jan 24 '17 at 10:04
  • @FedericoklezCulloca I tried to match the case but then also same error – Riya Patel Jan 24 '17 at 10:04
  • then you can remane and try as suggested by @RiggsFolly. – Suchit kumar Jan 24 '17 at 10:05
  • @FedericoklezCulloca Function names are not case sensitive so `Send_mail()` == `send_mail()` – RiggsFolly Jan 24 '17 at 10:05
  • The error you're getting is self-explanatory. There's already a function called send_mail somewhere in your codebase. You can't use the name send_mail again unless you use a namespace. By the way, mysql_* is dead, it's been removed from PHP 7 and your code will fail when there's no PHP 5 servers left to run it on. If you're new to PHP I'd suggest finding a learning resource that's less than 10 years old. – GordonM Jan 24 '17 at 10:20
  • @RiggsFolly oh, that's good to know. I'm still thinking in other languages, apparently. – Federico klez Culloca Jan 24 '17 at 10:28

3 Answers3

3

check your cust table there must be entered email address on which you are trying to send email. And change your

$mail->Port= 587; 
$mail->SMTPSecure= 'tls'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Username = 'abc@gmail.com';//Valid Gmail address 
$mail->Password = '1234556';//Gmail password

if still getting error you can go to your gmail account setting and allow for secure app authentication.

Jitendra Kumar
  • 382
  • 3
  • 14
1

take your function outside of loop.Change your code structure to this.

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);
        }
        }
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • I tried your code but getting error `Fatal error: Call to undefined function Send_mail()` – Riya Patel Jan 24 '17 at 10:11
  • I renamed the function then also getting same error : `Fatal error: Call to undefined function mySend_mail()` – Riya Patel Jan 24 '17 at 10:21
  • @RiyaPatel lets take small steps: comment entire code and try this: `while($i == 0) { $i=1; Send_mail(); } function Send_mail() { echo "hi"; }` and see if it is printing hi. – Suchit kumar Jan 24 '17 at 10:24
  • it showing twoo error `Notice: Undefined variable: i in ` and `Fatal error: Call to undefined function Send_mail() in` – Riya Patel Jan 24 '17 at 11:06
  • @RiyaPatel that code should work fine and print hi once.create a new php page and run it do not add anything else. demo here http://sandbox.onlinephpfunctions.com/code/4fbb1024f10cc21f0b7fbfbc1db09afb9f80a7b1 – Suchit kumar Jan 24 '17 at 11:13
  • error gone but getting this error `You must provide at least one recipient email address. You must provide at least one recipient email address.` – Riya Patel Jan 24 '17 at 11:33
  • @RiyaPatel where is it coming . It means you are not passing the email Id. – Suchit kumar Jan 24 '17 at 11:48
  • in variable it's showing the email ID has been passed – Riya Patel Jan 24 '17 at 11:57
  • before $mail->Send(); do print_r($mail) exit; and see what it prints. – Suchit kumar Jan 24 '17 at 12:01
  • `You must provide at least one recipient email address. PHPMailer Object ( [Priority] => 3 [CharSet] => iso-8859-1 [ContentType] => text/html [Encoding] => 8bit [ErrorInfo] => You must provide at least one recipient email address. [From] => root@localhost [FromName] =>.....................` – Riya Patel Jan 24 '17 at 12:04
  • @RiyaPatel you are not providing any emailId to $email object. add this: `$mail->AddAddress($email);` before `$mail->Send(); `. – Suchit kumar Jan 24 '17 at 12:09
  • I added your line, not its showing `SMTP Error: The following recipients failed: abc@gmail.com` – Riya Patel Jan 24 '17 at 12:13
  • @RiyaPatel add some valid email Id which exists. Please try to understand the error message. It will be very difficult to help like this. – Suchit kumar Jan 24 '17 at 12:14
  • this is valid email ID `SMTP Error: The following recipients failed: bhakuq@gmail.com` – Riya Patel Jan 24 '17 at 12:15
  • @RiyaPatel try setting $mail->SMTPAuth to false.Is your phpmailer the latest version. – Suchit kumar Jan 24 '17 at 12:20
1

Put your function outside of the loop. Here is the code structure:

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


}

}       

?>

Jitendra Kumar
  • 382
  • 3
  • 14
  • getting same error `Fatal error: Call to undefined function Send_Mail() ` – Riya Patel Jan 24 '17 at 11:04
  • error gone but getting notice `You must provide at least one recipient email address. You must provide at least one recipient email address.` – Riya Patel Jan 24 '17 at 11:15
  • check your **cust** table there must be entered email address on which you are trying to send email. And change your SMTP Details ----------TRY THIS FOR GMAIL ACCOUNT---------------------- $mail->Port= 587; $mail->SMTPSecure= 'tls'; $mail->Host = 'smtp.gmail.com'; $mail->Username = 'abc@gmail.com';//Valid Gmail address $mail->Password = '1234556';//Gmail password ----------------------------------------------------------------------------- if still getting error you can go to your gmail account setting and allow for secure app authentication. – Jitendra Kumar Jan 24 '17 at 12:56