0

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);
  1. What to add/replace to it to send email to individual row in DB TABLE?
  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???

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!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Lesly
  • 9
  • 5
  • 3
    You need some data to identify the individual user to email, like a username or a user id, so that your db query can be more specific, like `SELECT email FROM mytable WHERE userid = '136';` – larsAnders Apr 18 '16 at 22:49
  • 3
    Any code examples you find that use `mysql_*` functions can be safely discarded as either very old and outdated or poor quality. You should be using PDO or mysqli for all new projects. – Mike Apr 18 '16 at 22:54

1 Answers1

1

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

Community
  • 1
  • 1
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14