PHP split array and get just one record/row - php

I am using the following code:
$sql2 = "SELECT * from contacts where company_sequence = '".$customersequence."' and receive_accountsemails = 'yes' ";
$rs2 = mysql_query($sql2,$conn) or die(mysql_error());
while($result2=mysql_fetch_array($rs2))
{
$emails_list[] = $result2["email"];
}
This puts all email addresses from the contacts table into an array and then I send the emails to implode(',',$emails_list) so it separates each email address with a comma.
What is the best way to just get one email address into a variable on its own so I can insert each email sent into a different table? When I put sentto it wont do the whole array of emails.

Best way is to use another foreach loop:
foreach($emails_list as $email)
{
$user_email = $email;
// Then you can send email in loops for each email. Target email is stored in $user_email or just $email
}

Simply do not glue them all together with joinbut traverse them with
foreach ( $emails_list as $mail ) {
// do something with each individual $mail
}

Related

php should not send email multiple times to same person who reply multiple time with same email

I am New to php . Any help would be greatly appreciated.
My issue is that when user reply to any post with specific id, php sends an email alerting involved users that have previously comment on the post with same id, the problem is if the person has entered 3 reply for that post the user gets 3 emails, Since my database has reply entries from the same user with email for each post multiple times.
This is what is the problem
$to = $row['Email'];
How do I make sure it does not send an email to the user multiple times?
// update data in mysql database
$sql4= "select * from answer WHERE id='$id'";
$result4=mysql_query($sql4);
while($row = mysql_fetch_array($result4))
{
$to = $row[‘email’] ;
// Your subject
$subject="$name Reply on example.com”;
// From
$header="from: Associate <no-reply#example.com>";
// Your message
$message = "$name Reply on example.com\n$comment\nDate and Time=$datetime\nhttps://www.example.com/upload/$img\nfor More Visit https://www.example.com/visit/“;
// send email
$sentmail = mail($to,$subject,$message,$header);
}
Change your query to this: $sql4= "SELECT DISTINCT * FROM answer WHERE id='$id'";
you can add email in array and use array_unique function so your duplicate entry will remove auto.
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>

Are IMAP email identifiers unique in PHP?

My goal is to be able to use a form, checkbox and submit button (delete button) to remove specific emails.
I am starting off with this connection script:
<?php
//Inbox - Connect;
$inbox = imap_open($folder_inbox,$username,$password) or die('Error connecting to Gmail: ' . imap_last_error());
//Grab Inbox Emails;
$inbox_emails = imap_search($inbox,'ALL');
//If there are emails;
if($inbox_emails) {
//Output Arrays;
$inbox_from = array();
$inbox_subject = array();
$inbox_date = array();
$inbox_msg = array();
//Put the newest emails to the top;
rsort($inbox_emails);
//For each email, Give it an email number;
foreach($inbox_emails as $inbox_email_number) {
//Get information specific to this email;
$inbox_overview = imap_fetch_overview($inbox,$inbox_email_number,0);
$str_date = $inbox_overview[0]->date;
include('../func/inbox/grab/datefix.php');
mb_internal_encoding('UTF-8');
//Add email information to the arrays;
$inbox_read[] = $inbox_overview[0]->seen;
$inbox_from[] = str_replace('"',"", str_replace("_"," ", mb_decode_mimeheader($inbox_overview[0]->from)));
$inbox_subject[] = str_replace("_"," ", mb_decode_mimeheader($inbox_overview[0]->subject));
$inbox_date[] = $str_date;
//$inbox_msg[] = imap_fetchbody($inbox,$inbox_email_number,2);
}
}
//Close The Connection;
imap_close($inbox);
?>
I know how to move an email to a trash folder but I am concerned that there might be a race condition between getting a set of IDs for a folder and, a few minutes later, requesting the deletion of one of those items.
For example, I have my form showing 2 email subjects, I click the checkbox related to email 1 which has the id '1', then click the delete button which submits the form. The checkbox will have the name="1" parameter. Now when we submit the form lets say on the submit page it uses post gets the id 1 but lets say before we loaded the PHP file I got a new email wouldn't it incorrectly delete the new email rather then the email we checked?

Get all rows into a variable then mailto for each PHP

So, I found this thread (Get all data from mysql row in a variable) but I am too much of a beginner to make it apply easily to my situation. Thank you for helping me out... sorry for the total newb questions.
I have a PHP form that lets the user select one of my tables in a database where email addresses are stored to send an email to each of them. Right now, I have this code:
$recipientid = $_POST['recipientid'];
$body = $_POST['body'];
$subject = $_POST['subject'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SENDER NAME <senderemail#gmail.com>' . "\r\n";
date_default_timezone_set('America/Los_Angeles');
$time = date('m/d/Y h:i:s a', time());
$sendbody = $body . "<br><br>This is a bulk email announcement sent by
the institution.<br><br>It was sent at " . $time . ". If you have any
questions about this message or wish to unsubscribe, please contact
the institution.";
if($recipientid == 'allstudents'){
// SEE NOTE #2//
$recipientlist = //email addresses
}
$process=explode(",",$recipientlist);
reset($process);
foreach ($process as $to) {
$sent=mail($to,$subject,$sendbody,$headers);
}
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
What is the easiest way to capture all of the email addresses in the column of the specified table and then loop a mailto for each? I was originally trying to get them all into one string and then explode them as you can see, but that might not be the best solution. Am I on the right track here?
(NOTE #2 FROM IF)
HERE IS WHERE I NEED SOMETHING... I was sort of thinking about trying to use the following. I need it to grab all the emails from the column emailaddresses in the table students. I am using an if statement because there are four other things that $recipientid could equal, and each different one grabs email addresses from a different table.
array pg_fetch_all_columns ( resource $result [, int $column = 3 ] ) But then, I don't know how to get this array to work with my mail. I originally tried to use just a SELECT * from emailaddresses and then use each row somehow but I don't know how to implement that.
YES, I know I am using mysql not mysqli and I know that mailto is probably not the best solution, but it is what I have to work with right now (unless you can suggest an alternative route for the mail loop).
Thank you again! I really want to learn what I am doing, so an explanation would be appreciated:)
(and ps I am using the mail function with the explode because of this article http://tutorial.world.edu/web-development/php-script/how-to-send-out-mass-emails-php-script/)
I might be a little confused about the question. It sounds like you have a database with email address and you want to send an email for each email address. I think you can just do the query SELECT emailaddress from table and cycle through the results and use your mail function each time.
$query = *your select query*
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$sent=mail($row['emailadress'],$subject,$sendbody,$headers);
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
If you want your user to select the table the email addresses are coming from you can use a form and a variable in the query.
You can use the below piece of code to send the mail.
Consider you have an email field in your table 'students'
then
$sel = mysql_query("select email from students");
while($info = mysql_fetch_assoc($sel))
{
$to = $info['email'];
$sent=mail($to,$subject,$sendbody,$headers);
//---Remaining code goes on
}
try it.

Sending form information to emails stored in the database

I have been researching this but cant sem to get an answer.
I have a form that is sending the info to my database, I also want the info to go to email addresses which are stored in a column in my table in the database. Heres the code, I was just playing around to see if I could do a SELECT query but it wouldnt work, also Iknow that sending the email works when I manually ut an address into my "setTo"
require 'mail/class.simple_mail.php';
$mailer = new SimpleMail();
$comments = $_REQUEST['comments'];
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$place = $_REQUEST['place'];
$email = "SELECT email FROM bridesmaids";
$message = "<strong>This is an email from the bride to you the bridesmaids, here is the information on the next dress fitting<br />".$date." ".$time." ".$place." ".$comments." </strong>";
$send = $mailer->setTo('$email', 'Your Email')
->setSubject('Dress Fitting')
->setFrom('donaghy-e5#email.ulster.ac.uk', 'Domain.com')
->addMailHeader('Reply-To', 'donaghy-e5#email.ulster.ac.uk', 'Domain.com')
->addMailHeader('Cc', 'donaghy-e5#email.ulster.ac.uk', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Your Email has been sent to your bridesmaids' : 'Could not send email';
If you haven't created an SQL connection, do this first then:
1: Select your emails from your bridesmaids table - then return the results into an array (PDO::FETCH_ASSOC or fetch_array(MYSQLI_ASSOC)) and bind this array to a variable e.g. $mailingList.
2: Wrap your mailer code in a foreach loop (http://uk3.php.net/manual/en/control-structures.foreach.php) an example would be:
foreach($mailingList as $value) {
// your mailer code here, replace $email with $value
$send = $mailer->setTo($value, 'Your Email')
// $value will update for each iteration with your emails
}
3: Ensure any code or variables which do not need to be repeated are outside of the loop.
You don't query the database for the actual e-mail adresses. You just have s string that contains an SQL Query but you never actually connect to a database and fetch data using a query.
$email = "SELECT email FROM bridesmaids";
This should become something like this:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($result = $mysqli->query("SELECT email FROM bridesmaids")) {
printf("Select returned %d rows.\n", $result->num_rows);
$result->close();
}

php email sending script not sending email

following is my script for send email inquiry.. the recipient email address was stored in a db called users.. this script will not work properly.. i think the problem is recipient email section.. because when i used a email address instead of $user it will work..
thanx help me
<?php
$refno = $HTTP_POST_VARS['refno'];
$proid = $HTTP_POST_VARS['proid'];
$name = $HTTP_POST_VARS['name'];
$email = $HTTP_POST_VARS['email'];
$msg = $HTTP_POST_VARS['msg'];
//connect db and find email address related to id
include 'db_connector.php';
$id=$HTTP_POST_VARS['id'];
$query=mysql_query("SELECT user_email FROM users WHERE id='".$id."'");
$cont=mysql_fetch_array($query);
$user=$cont['user_email'];
// recipient name
$recipientname = "'".$name."'";
// recipient email
$recipientemail = $user ;
// subject of the email sent to you
$subject = "Inquiry for your advertisement No. (".$refno.")";
// send an autoresponse to the user?
$autoresponse = "no";
// subject of autoresponse
$autosubject = "Thank you for your inquiry!";
// autoresponse message
$automessage = "Thank you for your inquiry.! We'll get back to you shortly.
";
// END OF NECESSARY MODIFICATIONS
$message = "reference number : $refno
$msg
From $name $email
---";
// send mail and print success message
mail($recipientemail,"$subject","$message","From: $recipientname <$email>");
if($autoresponse == "yes") {
$autosubject = stripslashes($autosubject);
$automessage = stripslashes($automessage);
mail($email,"$autosubject","$automessage","From: $recipientname <$recipientemail>");
}
header("Location:index.php");
exit;
?>
First of all, your query is SQL injectable. Never ever pass a variable coming from a POST request directly into an SQL query. Use mysql_real_escape().
As to your bug: it seems that $user does not contain a valid e-mail address. so, the Mysql query is not returning an e-mail address.
Use $_POST rather than $HTTP_POST_VARS.
Switch on error reporting by prepending these two lines to your PHP code:
PHP code:
error_reporting(E_ALL);
ini_set('display_errors','1');
Run your script again. Do you get any notices or warnings?
If not, try to display your query, by adding
die($query);
just before the line that has the mysql_query command, and then run the query manually (e.g. using PhpMyAdmin or MySQL Query Browser) to see if you are actually getting a result that looks like an e-mail address.
Debug your PHP program.
Check out :
If the variables contain the supposed values.
Query is okay and returns result.
Proper header is set with mail function.
etc.
PHP manual have a good example to send mail.
Do not use $HTTP_POST_VARS['name']; it is deprecated use $_POST instead.
hey guys thanx for the help.. i found the error done in my inquiry form.. the id filed hidden outside of the form tag.. therefore the id value will not passed to the sendinq.php. i change it thank now the sendmail option work properly

Categories