问题
I will try to explain what I need to do as best as I can:
I have this remote SQL database, where are stored thousand os records and tables; I connected succesfully to this database with php and sqlsrv module.
The query I run is this:
$sql = "SELECT [FOURNI], [NU_INT], [NU_IMM], [NOM_EQP], [N_SERI], [TYP_MOD], [MARQUE], [NOM_UF], [NOM_ETAB], [DA_AP], [OBSERV], [OBSERV2], [INT_CM], [LIB_STATUT] FROM [INPROGRESS_WO_VIEW]
WHERE [FOURNI] <> 'NULL'
ORDER BY [FOURNI] ASC, [NU_INT] ASC";
and this is the result:
query result table
I also set up phpmailer and have been able so send email to selected adresses;
What I need to do:
Send an email for every [FOURNI] (1st column) in the query with a HTML table in the email body that return all the rows for that specific [FOURNI] (in the posted picture the column head is "Fornitore")
Most [FOURNI] have more than one rows associated and you can't predict how many; I have been able to associate the email address of every [FOURNI] in a different query, taking the parameter from a different table and using a LEFT JOIN.
I will need to be able to use variables and put them, for example, in the subject, like so:
$sub = "TEST EMAIL TO THIS " . $fourni;
Where $fourni is the result of a query that return the data of that single row, for example:
$sql = "SELECT [FOURNI] FROM [INPROGRESS_WO_VIEW]
WHERE [FOURNI] = 'GAMBRO'";
I'm sorry if this is too confusing, I will try to clarify as much as I can.
I'm using WAMP on windows 10 32bit - php 5.6.19 // remote database is SQL SERVER 2008 R2 on Windows server 2008
Thank you
EDIT 30/08/2016:
Thanks to @Synchro, I tried the mailinglist file in the example files of phpmailer, searched for hours,found something, and I've come up with this code, but when I run it it just loads the page undefinitely:
//connection details and query
$serverName = "**********"; //serverName\instanceName
$connectionInfo = array( "Database"=>"******", "UID"=>"*****", "PWD"=>"*****");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT [FOURNI], [NU_INT], [NU_IMM], [NOM_EQP], [N_SERI], [TYP_MOD], [MARQUE], [NOM_UF], [NOM_ETAB], [DA_AP], [OBSERV], [OBSERV2], [INT_CM], [LIB_STATUT] FROM [INPROGRESS_WO_VIEW]
WHERE [FOURNI] = <> 'NULL'
ORDER BY [NU_INT] ASC";
$sql2 = "SELECT DISTINCT FOURNI, AD_EMAIL FROM FOURNIS2
WHERE FOURNI = 'BARBAGALLO'
ORDER BY FOURNI";
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
$stmt2 = sqlsrv_query($conn, $sql2);
if( $stmt2 === false ) {
die( print_r( sqlsrv_errors(), true));
}
//here begins the mailing list
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isHTML(true);
$mail->isSMTP();
$mail->Host = 'smtps.aruba.it';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->Port = 465;
$mail->Username = '*****';
$mail->Password = '*****';
$mail->setFrom('*****');
$mail->Subject = "PHPMailer Simple database mailing list test";
include "sql.php";
$result = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC);
foreach ($result as $row) {
$mail->addAddress($result["AD_EMAIL"], $result["FOURNI"]);
while ($result = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$fornitore = $result['FOURNI'];
}
$body = "<table><tr><th>ODL</th><th>INV</th><th>APP.</th><th>SERIALE</th><th>MODELLO</th><th>MARCA</th></tr>";
while($row2 = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$body .= "<tr><td>".$row2['NU_INT']."</td><td>".$row2['NU_IMM']."</td><td>".$row2['NOM_EQP']."</td><td>".$row2['N_SERI']."</td><td>".$row2['TYP_MOD']."</td><td>".$row2['MARQUE']."</td></tr>";
}
$mail->Body = $body.'</table>';
$mail->set('X-Priority', '3'); //Priority 1 = High, 3 = Normal, 5 = low
$mail->Send();
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
From this example,I have a mailing list $sql2 made up of two columns: FOURNI, AD_MAIL From this table run the first foreach that should substitute the email address to the addAddress field;
For every email that I send I need to create a dynamically generated html table from the table $sql, that return all the rows associated with the column FOURNI (first column in both tables) in the table $sql2.
I've tried a hundred different ways with many errors, but I'm not able to figure out the proper algorithm to write the code.
来源:https://stackoverflow.com/questions/39190457/send-mass-email-with-phpmailer-where-email-body-is-the-result-of-sql-query-in-a