2017-07-31 2 views
1

J'essaye d'envoyer une mise à jour d'email aux utilisateurs du candidates_table mais le contenu de l'email vient de la table jobs_list. S'il vous plaît voir ma tentative ci-dessous, j'utilise PHPmailer et je ne reçois aucune erreur. Le script ci-dessous est le script de gestion d'un formulaire.Comment envoyer un email basé sur deux tables après une requête d'insertion?

Les données du jobs_list sont affichées, mais les données candidates_table ne le sont pas.

Ceci est juste en dessous de la déclaration d'insertion:

MISE À JOUR:

$vac_last_id = $dbh->lastInsertId(); 
echo $vac_last_id; 


$sql = $dbh->prepare("SELECT * FROM jobs_list WHERE id=:id"); 

$sql->bindValue(':id', $vac_last_id, PDO::PARAM_INT); 

if($sql->execute()) { 
    $sql->setFetchMode(PDO::FETCH_ASSOC); 

} 

while($row = $sql->fetch()) { 


$mail = new PHPMailer; 
$mail->isSMTP(); 
$mail->Host = ''; 
$mail->SMTPAuth = true; 
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead 
$mail->Port =; 
$mail->Username = ''; 
$mail->Password = ''; 
$mail->setFrom('', '- Vacancies'); 
$mail->addReplyTo('', '- Vacancies'); 

$mail->Subject = ""; 
//Same body for all messages, so set this before the sending loop 
//If you generate a different body for each recipient (e.g. you're using a templating system), 
//set it inside the loop 
$mail->Body = 'THE BODY...'; 

     //msgHTML also sets AltBody, but if you want a custom one, set it afterwards 
     $mail->AltBody = 'To view the message, please use an HTML compatible email viewer'; 
     //Connect to the database and select the recipients from your mailing list that have not yet been sent to 
     //You'll need to alter this to match your database 

    $mysql = $dbh->prepare("SELECT * FROM candidates_table WHERE receive_email = 2"); 
    if ($mysql->execute()) { 
     $mysql->setFetchMode(PDO::FETCH_ASSOC); 

     } 

foreach ($mysql as $row) { //This iterator syntax only works in PHP 5.4+ 

$mail->addAddress($row['email_address'], $row['full_name']); 

if (!$mail->send()) { 
    echo "Mailer Error (" . str_replace("@", "&#64;", $row["email_address"]) . ') ' . $mail->ErrorInfo . '<br />'; 
    break; //Abandon sending 
} else { 
    echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email_address']) . ')<br />'; 
    //Mark it as sent in the DB 
} 
// Clear all addresses and attachments for next loop 
$mail->clearAddresses(); 
} 
} 
+0

* * CLAUSE OÙ: Qu'est-ce '..jobs_list où id..' dans' $ sql = $ dbh-> prepare ("SELECT * FROM jobs_list où id ORDER BY id 0 DESC LIMIT, 1") "? –

+0

Cela vient de sélectionner le dernier identifiant inséré dans la table 'jobs_list'. pour le contenu de l'email. – bob

+0

Comment il va sélectionner? 'WHERE id' n'est pas correct. Vous n'avez attribué aucune valeur à 'id'. –

Répondre

0

Insérer la liste des travaux dans le tableau job_listing. Trouver, job_listing ID de la table actuelle. Récupérez-le à nouveau et trouvez tous les candidats pour envoyer un e-mail.

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    require 'PHPMailer/PHPMailerAutoload.php'; 
    include 'db_connect.php'; 

    $contact_name = $_POST['contact_name']; 

    $latest_job_id = 0; 

    $stmt = $dbh->prepare("INSERT INTO jobs_list (jobTitle, company_name, job_details, salary_info, salary_extra, apply_link, company_email, company_phone, TimeStamp) VALUES (:jobTitle, :company_name, :job_details, :salary_info, :salary_extra, :apply_link, :company_email, :company_phone, NOW())"); 
    $stmt->bindParam(':jobTitle', $_POST['jobTitle'], PDO::PARAM_STR); 
    $stmt->bindParam(':company_name', $_POST['company_name'], PDO::PARAM_STR); 
    $stmt->bindParam(':job_details', $_POST['job_details'], PDO::PARAM_STR); 
    $stmt->bindParam(':salary_info', $_POST['salary_info'], PDO::PARAM_STR); 
    $stmt->bindParam(':salary_extra', $_POST['salary_extra'], PDO::PARAM_STR); 
    $stmt->bindParam(':apply_link', $_POST['apply_link'], PDO::PARAM_STR); 
    $stmt->bindParam(':company_email', $_POST['company_email'], PDO::PARAM_STR); 
    $stmt->bindParam(':company_phone', $_POST['company_phone'], PDO::PARAM_STR); 
    $stmt->execute(); 

    $latest_job_id = $dbh->lastInsertId(); //@Nana Comments: Get latest Job Listing ID 

    if($latest_job_id > 0){ 

    /*@Nana Comments: If Inserted Successfully, '$latest_job_id' will be greater than 0.*/ 

    $mail_error_text = ""; //@Nana Comments: If email not sent, then it will store the email address 

    /*@Nana Comments: Select recent job listing details.*/ 
    $sql = $dbh->prepare("SELECT * FROM jobs_list WHERE id = :id LIMIT 0, 1"); 
    $sql->bindParam(':id', $latest_job_id); 
    if ($sql->execute()) { 

     $sql->setFetchMode(PDO::FETCH_ASSOC); 

     $mail = new PHPMailer; 
     $mail->isSMTP(); // Set mailer to use SMTP 
     $mail->Host  = ''; // Specify main and backup SMTP servers 
     $mail->SMTPAuth = true; // Enable SMTP authentication 
     $mail->Username = ''; // SMTP username 
     $mail->Password = ''; // SMTP password 
     $mail->SMTPSecure = ''; // Enable TLS encryption, `ssl` also accepted 
     $mail->Port  = ''; // TCP port to connect to 
     $mail->setFrom('', 'sender'); 

     while ($row = $sql->fetch()) { 

     $new_company_email = trim($row['company_email']); 
     $new_company_name = trim($row['company_name']); 

     /*@Nana Comments: Select all candidates and send email */ 
     $load_candidate = $dbh->prepare("SELECT * FROM candidates_table"); 
     if ($load_candidate->execute()) { 
      $load_candidate->setFetchMode(PDO::FETCH_ASSOC); 
      while ($row = $load_candidate->fetch()) { 
      $mail->addAddress($row['email_address']); // Add a recipient 
      $mail->isHTML(true); // Set email format to HTML 
      $mail->Subject = 'New Vacancy'; 
      $mail->Body = 'mail body in here'; 
      $mail->AltBody = ''; 
      if(!$mail->send()){ 
       $mail_error_text .= "Mailer Error (" . str_replace("@", "&#64;", $row["email_address"]) . ') ' . $mail->ErrorInfo . '<br />'; 
      } 
      $mail->clearAddresses(); // Clear all addresses for next loop 
      $mail->clearAttachments(); // Clear all attachments for next loop 
      } 
     } 
     } 
    } 

    if($mail_error_text != ""){ 
     echo "<b>Email not sent to:</b><br>".$mail_error_text; 
    } 

    } else { 
    echo "Job Listing Insertion Fails"; 
    } 
} else { 
    echo "access denied"; 
}?> 
+0

Il n'est pas nécessaire de créer une nouvelle instance de PHPMailer à chaque fois autour de la boucle, il suffit de la réutiliser. Voir [cet exemple] (https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps) – Synchro

+0

Oui @Synchro. Merci de m'avoir signalé. Actualisé. –

+0

Cela a envoyé le même courriel 20 fois. – bob