How can I send emails to all emails on my database? E.g. here is my format of MYSQL.
MYSQL -- Table = users --- column = email. I need to send emails to everyone of the email on column "email".
Simple ready to use PHP script for sending mail from mysql data
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT email FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0]);
}
mysql_free_result($result);
function sendMail($to){
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
Do you know how to pull data from MySQL? if so, you should just execute the mail() function for each row.
The code that JapanPro suggested is great! However, I have read over the internet about doing this. If there is loads of emails to be sent it will take a long time and possible slow down your server and clog it up. I would recommend doing it in intervals. So like in sets of 10 or 20.
This is only based on the information I have read. And I can se where they are coming from.
Related
I have a MySQL DB with EMAIL row.
User perform a search and the relevant info comes up in a <table> format (address 1, address 2, phone, fax, email, etc).
In the <tabel>, the last column has an "E-mail" button and if you click it the system should send an automated email to that email address.
I have found one article here which actually works for me, but it send email to everyone in the DB. How can I modify this php to send email to ONLY that specific row.
Here is what I found:
mysql_connect('localhost', 'mysql_user', 'mysql_password') or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT email FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0]);
}
mysql_free_result($result);
function sendMail($to){
$subject = 'AC the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
What to add/replace to it to send email to individual row in DB TABLE?
when the email sent by this code to my gmail account i can see the sender is: "webmaster#example.com ip-XXX-XXX-XXX-XXX.yyyyyyyyyyy.zzzz.net" (which is my server's IP).... WHY does it appear like that???
I have added to this code the following to see if it picks up the right records (which it does):
mail($to, $subject, $message, $headers);
echo 'email has been sent to: ' . $to . '<br/>';
}
after email sent, it list the email addresses back to me and all email addresses receives the email...
I would appreciate your help!
1: What to add/replace to it to send email to individual row in DB TABLE?;
you can add conditional query to fetch particular user's email like :
SELECT email FROM mytable where <your condition>
2: when the email sent by this code to my gmail account i can see the sender is: "webmaster#example.com ip-XXX-XXX-XXX-XXX.yyyyyyyyyyy.zzzz.net" (which is my server's IP).... WHY does it appear like that???
by default it shows the server default email address, you can update it also. To do so, follow the link : Change outgoing mail address from root#servername - rackspace sendgrid postfix
I am trying a lot but nothing work for me.
My query is that how to fetch column data from sql database Table and put that data into an string in PHP?
Please check What I am doing.
I have the table "DriveRegisterTable" in which 4 columns in database table, 1st "id" 2nd "username" 3rd "txtMail" 4th "rxtMail" .
Now I want to select txtMail and rxtMail from database table where username = "John"
Now in my database table "username" john have txtMail is " johnery99#gmail.com" and rxtMail is "sarah88#gmail.com"
Now I want to fetch these thing on my php string.
Now I want to select txtMail and rxtMail from database table where username = "John" and put txtMail and rxtMail values which are johnery99#gmail.com and sarah88#gmail.com in PHP String and then send the mail.
Please check what I am doing it is not working for me:
<?php
$db_host = "localhost";
$username = "jacktask";
$db_pass = "sormor4455";
$database = "myDatabaseDB";
mysql_connect("$db_host", "$username", "$db_pass");
#mysql_select_db($database) or die("Unable to find database4");
$txt_username = $_REQUEST['txt_username']; // here user name is John
$query = mysql_query("SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username'");
//$sql = "SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username' ORDER BY id ";
$res = mysql_query($query);
if($query){ // Dont know how to send mail or what to write in this condition block
$to = $txt_Mail; // want johnery99#gmail.com here only but not able to fetch this mail here
$subject = 'Hello Subject!';
$message = 'Hi';
$headers = 'From: $txt_email' . "\r\n" .
'Reply-To: txt_email' . "\r\n" . // want sarah88#gmail.com here but txt_email does not give any value here means sarah88#gmail.com
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
mysql_close();
?>
I just want to select johnery99#gmail.com and sarah88#gmail.com from John and send mail.
Any Idea or suggestions would be highly welcome.
Try this:
$txt_username = mysql_real_escape_string($txt_username);
$query = mysql_query("SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username'");
//use loop if you are expecting more than 1 rows, otherwise remove loop
// and just use $row = mysql_fetch_array($query);
while ($row = mysql_fetch_array($query)) {
$email1 = $row['txt_email'];
$email2 = $row['rxt_email'];
//code to send email to those 2 emails
}
Note that I added mysql_real_escape_string to escape your variable. Dont just plug into your query it is vulnerable to SQL Injection.
I would suggest looking at PDO or MySQLi prepared statements.
Youre going to loop through your query
also your $res is redundant, its better to remove that
if($query){ // Dont know how to send mail or what to write in this condition block
while ($row = mysql_fetch_array($query)) {
$txt_Mail = $row['txtMail'];
$rxtMail = $row['rxtMail'];
$to = $txt_Mail; // want johnery99#gmail.com here only but not able to fetch
$subject = 'Hello Subject!';
$message = 'Hi';
$headers = 'From:'. $rxtMail .' . "\r\n" .
'Reply-To: '. $rxtMail .' . "\r\n" . // want sarah88#gmail.com here but txt_email does not give any value here means sarah88#gmail.com
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
}
next time, try to use mysqli (Mysql Improved) or PDO. mysql functions is deprecated and for the future it wont be supported anymore
I have a cron job (PHP) to get the commission of sales for N sales persons. It runs once in a month.
It has a number of criteria to calculate the commission for each sales person and can be updated by Admin.
What is best method to check whether the cron has run for all the sales persons, if not how can I get the details and rerun the cron?
Note: Using MySql
Please help
* * 1 * * php /yourjob.php >> /var/log/monthly_sales.log
Do something like that, echo out all your debug info into a log file, then check that logfile to see what happened, or what went wrong. Turn error_reporting on.
Cpanel cron jobs usually have an option for cron email. Which sends the results of the cron job to an email address. If you are having trouble with the cron job, set it to run every minute so you can get the email output and go from there.
Another option would be to add a php mail function to the script the cron job is executing:
<?PHP
//mail variables
$emailAddress = "your#email.net";
$subject = "Email Subject";
$message = "Sales person 1 commission = $salesPersonOneVariable";
$message.= "Sales person 2 commission = $salesPersonTwoVariable";
$message.= "Sales person 3 commission = $salesPersonThreeVariable";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers.= 'From: noreply#yourdomain.com' . "\r\n" .
'Reply-To: noreply#yourdomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail function
mail($emailAddress, $subject, $message, $headers);
?>
With this you can add any information/variables from the script into the $message variable so that you can verify certain parts of the script are completing successfully.
The headers give you the option to include html in the email if needed.
Just looking for advice on how to apply the urlencode into this bit of code. It is actually working, the issue when the email is received, the urlencode doesn't seem to work.
function reset_password($email) {
$query = "DELETE from reset_password where email = $email";
$deletepass = mysql_query($query);
$code = substr(base64_encode(crypt('', '')), 0, 32);
$query2 = "INSERT into reset_password values ($email, '$code', " . time() . ")";
$insertval = mysql_query($query2);
$f = "SELECT userEmail from gn_users where email = $email";
$from = "***"; // sender
$f['userEmail']; // recepient
$message =
"From: *** <***>\r\n" . // email headers
"To: {$f['userEmail']} <{$f['userEmail']}>\r\n" .
'Subject: Reset Password' . "\r\n" .
"\r\n" .
"Hello\r\n" . // email imap_body(imap_stream, msg_number)
"\r\n" .
"A request has been made to reset your example.com web site password.\r\n" .
"\r\n" .
"To complete the request, click on the following link within 48 hours of the transmision of this email and follow the on screen instructions.\r\n" .
"\r\n" .
"index.php?page=reset-password&email=" . urlencode($email) . "&code=" . urlencode($code) . "\r\n" .
"\r\n" .
"Kind regards,\r\n" .
"\r\n" .
"The example.com Web Site";
$to = "$email";
$subject = "Test mail";
$message = "$message";
$from = "***";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";}
I'll try to help you out with some tips to write one yourself, without having to rely on that awful code.
You need to break the task down in multiple, bite sized, easy to do tasks.
Here we go:
The user needs a page where to request a password reset. It's a form with an email field (and/or username).
We have the user email, and if it exists, we need to generate a reset password link that can't be guessed, so that not everyone can reset someones password.
So you need to generate a unique hash for this request, an option would be uniqid(), but there are many options here. So you generate a link that looks like: http://test.com/reset.php?uid=443&hash=33rr3344rree22. It can't really be guessed because you need to know both the user id and the has. But to make sure, we will make it expire in an hour or day.
Next, we ensure that this link will work. We have to create a table for password reset requests that contains the following columns: id, email, hash, date_added, and insert it (the date can be TIMESTAMP with a default of CURRENT_TIMESTAMP).
Now it's time to send the email. You can add any text you want, as long as you mention the url you generated a bit earlier.
Now the user clicks the link. You get the user id and hash, and check if such an entry exists. If it does, and the request is not older than 1 day, we generate a new password, update the users table, and send him a confirmation mail.
This is optional, but recommended. Create a cron job that clears the password reset request table for entries older than 1 day.
Each of these steps are fairly easy to do, or you can find a lot of information about them around the web. If you take your time to understand each step, sanitize everything properly, and do things by the book, you will learn a lot.
Dear experts, I want to create email id dynamically in my website to send messages. my site is a free messaging system ,which is used to send message in U.S phone numbers.Please give me a solution for these.
Create a database.
Create an email table in your database.
To this table:
Define an ID field in the table that is auto numbered. (Make it a
unique primary key).
Define all of the fields of information you want to remember about
your email.
I am assuming that you refer to building an RFC-822 compliant Message-Id field as described by :
http://www.faqs.org/rfcs/rfc2822.html
$fqdn_hostname = "mydomain.com";
$MessageId = "<" . sha1(microtime()) . "#" . $fqdn_hostname . ">";
I think he wants to send emails from random email ID's ..
copied from php.net
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
you can change the headers as required clubbed with some random string generation function.