I was trying to find out the error but I coudnt. I use codeigniter.
Here is the code
function send_emailalertstoAdmin($emailmessage,$ad_title)
{
$this->load->model('user_model');
$query_result = $this->user_model->load_administrator_email();
//$this->load->database();
//$query = $this->db->query('SELECT users.email FROM users WHERE users.id =1');
foreach ($query_result as $row)
{
$email= $row->email;
$successmasg = '';
$from = "info#xxxt.com";
$subject = "xxx ". $ad_title . " is Stolen";
$message = $emailmessage;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxxxx.com <info#xxxxt.com>' . "\r\n";
$successmasg = mail($email, $subject, $message, $headers);
}
}
I got the following error.
Fatal error: Call to undefined method User_model::load_administrator_email()
Here is the load_administrator_email() function
function load_administrator_email()
{
$sql = "SELECT users.email FROM users WHERE users.id =1";
$queryData = $this-> db->query($sql);
return $queryData -> result();
}
I was trying to find the error but I coudnt.
what I tried,
1. sql query is correct and it gives correct result.
2. when I hardcode email and comment calling load_administrator_email() in first function ,email goes correctly.
please help me to correct this.
thx in advance
Load your model like this..
$this->load->model('user_model', '', TRUE);
Or else load it in the constructor
Related
I'm making this code that (1) moves records from one table to another (in the same database), (2) sends the contents of table 1 to a pre-determined e-mail, and (3) delete the contents of table 1 - simulating an "add to cart" feature.
My problem is that the code below will only be successful in sending the e-mail if $headers is not sent on mail(). However, I need to send the table contents as HTML or at least allow for for the different records. If by any chance the e-mail isn't sent, then the delete part of the code isn't executed either. what am I doing wrong?
Thanks in advance!
Modified code (that works if I DON'T send $headers)
<?php
include '../config/database.php';
date_default_timezone_set('CET');
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, #date FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query);
if ($stmt->execute()) {
$query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";
$stmt=$con->prepare($query);
$stmt->execute();
$to = "testmail2#gmail.com";
$subject = "Test";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Test <testmail1#mail.com>" . "\r\n";
$body = "Sent on: ". $date . "-\r\n";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$body .= "Track title: ".$row ["track_title"]. "-";
}
$success = mail($headers, $to, $subject, $body);
if ($success) {
$query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query_delete);
$stmt->execute();
header('Location: cart.php?action=sent');
} else {
header('Location: cart.php?action=sent_failed');
}
} else {
header('Location: cart.php?action=sent_failed');
}
include 'layout_foot.php';
?>
Original code
<?php
include '../config/database.php';
date_default_timezone_set('CET');
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, #date FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query);
if ($stmt->execute()) {
//header('Location: cart.php?action=sent'); //please disregard this line as I forgot to remove it when I wrote this post.
$query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";
$stmt=$con->prepare($query);
$stmt->execute();
$to = "testmail2#gmail.com";
$subject = "Test";
$headers = "Test <testmail1#mail.com>". "\r\n". "MIME-Version: 1.0" ."\r\n". "Content-type: text/html; charset=iso-8859-1" ."\r\n";
$body = "Sent on: ". $date . "-";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$body .= "Track title: ".$row ["track_title"]. "-";
}
$success = mail($to, $subject, $body);
if ($success) {
header('Location: cart.php?action=sent');
} else {
header('Location: cart.php?action=sent_failed');
}
$query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query_delete);
$stmt->execute();
} else {
header('Location: cart.php?action=sent_failed');
}
include 'layout_foot.php';
?>
Your delete code will not be executed in any case since your are redirecting before this code run.
Remove header('Location: cart.php?action=sent'); written just
below to if ($stmt->execute()) {.
Place your delete code in if($success) condition before header.
Update $headers variable with below code and include it into your mail function:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Test <testmail1#mail.com>' . "\r\n";
This
$success = mail($headers, $to, $subject, $body);
has not the right parameter order ...
See http://php.net/manual/de/function.mail.php The correct signature is this:
bool mail ( string $to , string $subject , string $message
[, string $additional_headers [, string $additional_parameters ]] )
so
$success = mail($to, $subject, $body, $headers);
i get this error when i try to add a record from my function any suggestions to resolve this?
PHP Fatal error: Call to a member function insert() on a non-object
My function is this
function setdb($email){
global $wpdb;
$set_mail=$email;
$table_name = $wpdb->prefix . 'nd_tempemails';
$result=$wpdb->insert(
$table_name,
array('email'=>$set_mail
)
);
}
*Edit i added my whole php code due to request.here i am sending a link and for that i am keeping the email i sent the link as a record.
function sendmail()
{
$email = $_POST['email'];
$passmail=md5($email);
$subject = 'The Registration Link';
$headers = "From: xxx#xxx.xx ". "\r\n";
$headers .= "Reply-To: xxx#xxx.xx" . "\r\n".
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n".'X-Mailer: PHP/' . phpversion();
$message = 'Here is your One time URL<br>';
$message.="<a href='link";
$message.=$passmail;
$message.="'>click here</a>";
mail($email, $subject, $message, $headers);
setdb($passmail);
}
if(isset($_POST['email']))
{
sendmail();
}
all functions are in same file.
Try this
function setdb($email){
global $wpdb;
$table_name = $wpdb->prefix.'nd_tempemails';
$result=$wpdb->insert(
$table_name,
array('email'=>$email)
);
}
Call setdb('xyz#xyz.com');
Ok i solved this by directly calling wordpress config file and using default mysqli commands.I tried to include wp-db.php and get this done but didn't work.
I'm trying to send an email to users with the group level of 4 or 5. I have the group levels distinguished in my 'users' table. I'm trying to get the email and 'firstname' of the users with the 'group' level of 4 or 5. Then to use that email and first name to create variables with it and use it for my email.
Am I structuring my SQL statement incorrectly? I am not getting any errors.
The area where I am struggling with are these lines.
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5") {
$email = $con->query($sql_email);
} else {
echo "Query not working right";
}
$sql_name = "SELECT 'firstname' FROM 'users' WHERE 'group' = 4 AND 5";
$admin_name = $con->query($sql_name);
More code to show how I am using it
ini_set('display_errors', 1);
error_reporting(E_ALL);
//User Request Email that goes to the commissioner and creator
if(isset($_POST['submit'])) {
$con = mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5") {
$email = $con->query($sql_email);
} else {
echo "Query not working right";
}
$sql_name = "SELECT 'firstname' FROM 'users' WHERE 'group' = 4 AND 5";
$admin_name = $con->query($sql_name);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$to = $email;
$subject = 'There is a new user request to join the SFL';
$message = '
<html>
<head>
<title>New SFL User Request</title>
</head>
<body>
<p>Hi '.$admin_name.',</p><br>
<p>Thank you for wanting to join the!</p>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests#example.com";
$Bcc = "user-requests-confirm#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
UPDATE
$sql_email = "SELECT `email` FROM `users` WHERE `group` = 4 OR `group` = 5";
$email = $con->query($sql_email);
if( ! $con->query($sql_email) )
echo "Query not working right";
$sql_name = "SELECT `firstname` FROM `users` WHERE `group` = 4 OR `group` = 5";
$admin_name = $con->query($sql_name);
I get this error..
Catchable fatal error: Object of class mysqli_result could not be converted to string in /home4/db/public_html/example.com/register.php on line 219
Line 219 is this...
<p>Hi '.$admin_name.',</p><br>
After you execute a select query, you need to fetch records. I assume you need to send email to all users found.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$sql_email = "SELECT `email`, `firstname` FROM `users` WHERE `group` IN (4,5)";
$users = $con->query($sql_email);
if (($users) && ($users->num_rows > 0)){// Got any record?
// output data of each row
while($user = $users->fetch_assoc()){
$to = $user['email'];
$subject = 'There is a new user request to join the SFL';
$message = '
<html>
<head>
<title>New SFL User Request</title>
</head>
<body>
<p>Hi '.$user['firstname'].',</p><br>
<p>Thank you for wanting to join the!</p>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests#example.com";
$Bcc = "user-requests-confirm#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
}
else{
echo "No User Found!";
}
You can learn more from here: Select Data With MySQLi
SELECT anything FROM `users` WHERE `group` = 4 OR `group` = 5
Besides,
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5")
is useless as the result will always be true.
You can say
if( ! $con->query($sql_email) )
echo "Query not working right";
i design an application and build with phonegap all is working but when a value in my mysql database that is attached the app is updated i want an email to be sent automatically to the user. When the counter completes counting the value of "status" in runnindAds table is updated and i want to an email to be sent each time that value is updated, below is the script i wrote for the email to be sent but i don't know how to automate it, please help with what i can should or i can
<?
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
mysql_query("update runningAds set status = 3 where id = '$id'");
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
sendConfirmationEmail($username, $stopTime);
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Try this:
<?php
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
if (mysql_query("update runningAds set status = 3 where id = '$id'"))
{
sendConfirmationEmail($username, $stopTime);
}
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Tell me if it works or not.
I am trying to send notification emails(which is working fine) but have added the html headers to try to send links etc...for some reason nothing is showing up at all, just blank space where the desired links are supposed to be. Here is my code:
if(isset($_POST['commentBlogSubmit']) && $auth) {
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "Someone sent you left you a comment";
$message = "You have a new blog comment <br />".
" <a href='http:www.blah.org/indexNew.php'></a>";
$from = "info#blah.org";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
mail($to, $subject, $message, $headers);
}
Perhaps because you have no text inside the link tag?
Because the PHP email function generally sends plain text.
Rather than trying to do this yourself, you should probably use Mail_Mime
Also, though your headers are probably correct, you have nothing between the <a> and </a> tags.