Cannot redeclare Send_mail() error - php

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

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.

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

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

Related

How to change email sender email in PHP Mailer

Currentlt it shows the mail on which phpmailer SMTP is registered.
$email='abc#email.com';
$subjectRsc="Any";
$message='Welcome';
phpmail($email, $subjectRsc, $message);
My phpmailer function:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->AddReplyTo(REPLY_EMAIL, 'ABC');
$mail->SetFrom(FROM_EMAIL, 'ABC');
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
This is how I am sending mails to the users I want to show the specific mail of my website to be displayed in the mails not that on which smtp server is registered.
You should try this :
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->addReplyTo('myemail#example.com', 'ABC'); //<-- this is the line i changed
$mail->From= "myemail#example.com"; //<-- this is the line i changed
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
According to phpmailer documentation this is the field to add the sender's email.
please try using this code :
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("test#gmail.com", "Test");
$mail->addAddress("test1#gmail.com");
//Address to which recipient will reply
//if you want to add CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Please try using this code:
<?php
$to = "test#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:test1#gmail.com \r\n";
$header .= "Cc:test2#gmail \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

Can solve my "SMTP Error: Could not authenticate" error

i have a problem with my PHPmailer function.
I want my registration form to send a mail to every new registered member.
I get this error code. SMTP Error: Could not authenticate.
I have contacted my webhost, and they say that i have the right username,password, smtp server etc.
include "class.smtp.php";
include "class.phpmailer.php";
$Host = "mail.smtp.com"; // SMTP servers
$Username = "support#mymail.com"; // SMTP password
$Password = "mymailpassword";
$From = "support#mymail.com";
$FromName = "My name";
$ToEmail = $mail;
$ToName = $username;
$header = str_replace('%homepage_url%', $hp_url, $_language->module['mail_subject']);
$Message = str_replace(Array('%nickname%', '%username%', '%password%', '%activationlink%', '%pagetitle%', '%homepage_url%'), Array(stripslashes($nickname), stripslashes($username), stripslashes($pwd1), stripslashes($validatelink), $hp_title, $hp_url), $_language->module['mail_text']);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($ToEmail , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $header;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($Message);
$mail->CharSet="UTF-8";
if(!$mail->Send()){
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
} else {
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
}

how to variable pass mail function in php

I am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);

Not able to email registeration confirmation email

I have set up the user registration form which requires email validation. However the code isn't able to generate an email
connection file:
$host="xxxx";
$dbname="xxxx";
$dbuser="xxxx";
$dbpassword="xxxx";
$con = mysqli_connect($host,$dbuser,$dbpassword,$dbname);
$website_url="http://www.xxxx/email_activation/";
$sender_email="xxxx#yahoo.com";
send_mail.php
function send_mail($to,$subject,$body)
{
require 'class.phpmailer.php';
$from = "from#yourwebsite.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.mail.yahoo.com"; // SMTP host
$mail->Port = 25; // set the SMTP port
$mail->Username = "xxxx#yahoo.com"; // SMTP username
$mail->Password = "xxxx"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
Register file code:
$activation = md5(uniqid(rand(), true));
include 'send_mail.php';
$message = " To activate your account, please click on this link: \n\n ";
$message .= $website_url . '/activate.php?email=' .urlencode($sender_email) .
"&key=$activation";
mail($email_address, 'Registeration Confirmation', $message, 'From:' .$sender_email);

Send mail via SMTP?

I have a form on my website where people can join events. The code behind it works in this way:
All info is saved in a database. This part works fine.
The second part of the code send out an email to me and to the user with the info he entered (same info as saved in the database)
The issue is that these emails are sent unauthenticated through a default email on the hosting account. I had to modify the script to force SMTP authentication with a valid email under my hosting account to fix the error. Right now the script sends out the email but it ends in spamfilter with all ISPs so the user never receive the email.
I have no idea of how to do, or create the codes so the script use SMTP authentication. Below is the codes I have so fare. Can someone help me?
<?
// SEND OUT EMAIL PART
// COPY SEND TO MY SELF
$to = "my#email.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$fields = array();
$fields{"name"} = "Name";
$fields{"address"} = "Address";
$fields{"phone"} = "Phone";
$fields{"email"} = "E-mail addesse";
$body = "INFO:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// SEND TO THE USER
$headers2 = "From: my#email.com";
$subject2 = "THANKS!";
$fields2 = array();
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$body2 = "
TEXT TO EMAIL RECEIVER
\n\n"; foreach ($fields2 as $a => $b){ $body2 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// ERROR MESSAGES
if($from == '') {print "MISSING EMAIL ADDRESS.";}
else {
if($name == '') {print "MISSING NAME";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $body2, $headers2);
if($send)
{header( "Location: http://mysite/send.php" );}
else
{print "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; }
}
}
?>
Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:
// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->Port = 465; // change port is required
This is one way to do it. You need to include PHPMailer
#somewhere in code before you call function
include($path_to_PHPMailer."class.smtp.php");
function sendEmail($email,$name,$title,$content,$who=false,$att=array('')){
//VARIABLE $email is email of sender
//VARIABLE $name is name of sender
//VARIABLE $title is title of email
//VARIABLE $content is content of email
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// DOUBLE $mail->Host = "your email_server"; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "your email_server"; // sets the SMTP server
$mail->Port = server port; // set the SMTP port for the GMAIL server
$mail->Username = "your username"; // SMTP account username
$mail->Password = "your password"; // SMTP account password
if($who){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$address = "set your email";//EMAIL OF RECIPENT
} else{
$mail->SetFrom("set your email", "your name (or service name)");
$mail->AddReplyTo("set your email","your name (or service name)");
$address = $email;
}
$content=str_replace("\n", "<br>", $content);
$mail->Subject = $title;
$mail->MsgHTML($content);
$mail->AddAddress($address, $name);
$mail->CharSet='utf-8';
if($att[0]!=''){
foreach ($att as $at){
$mail->AddAttachment($at);
}
}
if(!$mail->Send()) {
return false; //$mail->ErrorInfo;
} else {
return true;
}
}

Categories