I create a web site like booking where on each page I have two forms. However, if I have just two recipients this forms work very well with AJAX, jQuery, PHP validation, PHPMailer and MySQL, but in second form I have problem, because on each page I need change email recipients which I need get from database using PDO, so I'm try the seguent lines of code, but this send form data to all emails from table owners_email which contain email addresses.
$sql="SELECT email_address FROM neum.owners_email";
foreach ($pdo->query($sql) as $row) {
$mail->AddAddress($row[email_address]);
}
However, I understand why, because my sql query it's select all emails but I need to select for each page different email address and again how to use it in INSERT statement, because in table with form data I have referenced to email_address_id from table with email addresses, how you can see bellow:
CREATE TABLE IF NOT EXISTS `neum`.`owners_email` (
`email_address_id` INT(11) NOT NULL AUTO_INCREMENT,
`email_address` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
PRIMARY KEY (`email_address_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 3
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
COMMENT = 'Table with information about email addresses of owners.';
-- -----------------------------------------------------
-- Table `neum`.`form_data`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `neum`.`form_data` (
`form_data_id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`tel` VARCHAR(50) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`from` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`to` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`mail` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`message` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`dateSent` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`email_address_id` INT(11) NOT NULL,
PRIMARY KEY (`form_data_id`),
INDEX `fk_form_data_idx` (`email_address_id` ASC),
CONSTRAINT `fk_form_data`
FOREIGN KEY (`email_address_id`)
REFERENCES `neum`.`owners_email` (`email_address_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
COMMENT = 'Table with information about form data.';
Nevertheless, I'm try on google to find answer for this question, but probably nobody use PHPMailer for send email on each page to different email address, because on every my question google usually show me simple results like how to send mail or how to send multiple mails, but for my question dont exist answer, so if somebody know how to fix it thanks in advance for your help and for your appreciate time.
PHPMailer doesn't care who you send to – it just uses whatever you pass to addAddress()
. If you want to send to different people, you need to figure out how to do get the list of addresses you want to send to, which is entirely independent of later sending them messages. Treat it as two independent tasks:
First task: get a list of addresses. This has nothing to do with PHPMailer.
Second task: send a message to an array of addresses. This has nothing to do with your database.
As it happens, PHPMailer provides an example that combines both of these tasks you should be able to adapt. Just because you're sending to 2 people rather than 10,000 doesn't mean you're not sending to a list, and the same code will work fine.
The only thing missing from your first code snippet is something in your MySQL query to limit who it selects for sending. Adapt it by doing something like:
$sql="SELECT email_address FROM owners_email JOIN form_data ON form_data.email_address_id = owners_email.id WHERE form_data_id = " . $form_data_id;
How you get the right $form_data_id
value is up to you, and you might want to consider using a join table so that you can have any number of addresses associated with any number of pages, rather than just a single fixed one.