Hi,
Hope this makes sense and hope someone can help me.
I have a database table with the following columns: person_id, firstname, surname, email_address.
Periodically I am given a set of files to send to some of the contacts in the database table. The filenames contain the person_id and they are always of the format: person_id.pdf or person_id-2.pdf. Sometimes there is only one of the files available or sometimes it is both of the files. The files are stored in a directory on our server
Example.
directory: letters
files: 1111.pdf, 1111-2.pdf, 2222.pdf, 3333-2.pdf
The user with person_id 1111: should receive an email with two attachements – 1111.pdf and 1111-2.pdf
The user with person_id 2222: should receive an email with one attachement – 2222.pdf
The user with person_id 3333-2: should receive an email with one attachement – 3333-2.pdf
This is just a small example, in real world scenario it could be hundreds of users to send these files to so I need an automated way using PHP to acheive this.
I have got to a point where:
The user with person_id 2222: receives the correct email
The user with person_id 3333: receives the correct email
However the user with person_id 1111: always receives two emails containing the same content.
I think this is because both files exist so whilst executing the loop it is doing it once for each file so my logic is flawed and I can’t figure it out.
The way my file is built is:
Use PHPMailer
Make data connection
Submit button – once clicked:
loops through my letters directory to find the files
set filenames as variables to use
use substr on the filename to extract the person_id
run db query to retrieve relevant record where person_id = “filename” (minus the .pdf or -2.pdf)
attach relevant files and send email.
I have attached a copy of my code to hopefully show what I am trying to acheive.
Please ask if you would like further clarification.
I am aware my PHP may have all sorts of errors or non best practice methods but I am learning all time
Kind Regards
Mr Dello
<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
use setasignFpdiFpdi;require ‘PHPMailer/src/Exception.php’;
require ‘PHPMailer/src/PHPMailer.php’;
require ‘PHPMailer/src/SMTP.php’;//configure database access
$dbuser = ‘user’;
$dbpass = ‘pass’;
$database = ‘database’;
$dbserver = ‘servername’;
$db = mysqli_connect($dbserver, $dbuser, $dbpass, $database)
or die (‘Database not available’);if(!isset($_POST[‘submitted’])){
//display form if not submitted
print “<p><strong>Press submit to send letters</p>”;
?>
<form name=”” action=”test.php” method=”POST”>
<input name=”submitted” type=”hidden” value=”submitted”>
<input name=”submit” type=”submit” value=”Post Letters”>
</form>
<?php
}else{
$dir = “letters”;
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if (strstr($file,”.pdf”)){
$number++;
$letter1 = “letters/”.substr($file,0,4).”.pdf”;
$letter2 = “letters/”.substr($file,0,4).”-2.pdf”;
if(strstr($file,”-2.pdf”)){}else{
sendMail($db,$letter1,$letter2);
}}
}
closedir($dh);
}
}else{
echo “No directory found”;
}
}function sendMail($db,$letter1,$letter2){
$personid = substr($letter1,8,4);
$query = “SELECT person_id, forename, surname, email_address FROM people_table WHERE person_id = “.$personid;
$result = mysqli_query($db,$query) or die (mysqli_error().”<br> Query error”);
while($row = mysqli_fetch_array($result)){
$sforename = $row[‘forename’];
$ssurname = $row[‘surname’];
$emailaddress = $row[’email_address’];
}$bodytext = ”
<p>
Dear “.$sforename.” “.$ssurname.”,
</p>
<p>
HTML Formatted email with attachments
</p>
Best wishes,<br><br>
“;$email = new PHPMailer();
$email->IsHTML(true);
$email->From = ‘[email protected]’;
$email->FromName = ‘Admin Office’;
$email->Subject = ‘Important Letter for: ‘.$personid;
$email->Body = $bodytext;
$email->AddAddress = $emailaddress;if(file_exists($letter2)){
$email->AddAttachment( $letter2 , $personid.”-letter2.pdf” );
$email->AddAttachment( $letter1, $personid.”-letter1.pdf” );}else{
$email->AddAttachment( $letter1, $personid.”-letter1.pdf” );
}return $email->Send();
}?>
hiii