1
請原諒我的'新手',這是我在php中的第一個項目。我相信這是一個非常簡單的問題...希望對某些人來說簡單點!建設mysqli_fetch_assoc陣列
我想從我的數據庫中獲取一個數組,運行Swift_Plugins_DecoratorPlugin並創建一系列個性化的消息來後臺處理。
問題是,該腳本使用靜態數據數組,但是當我嘗試從數據庫中引入實時數據時,我迷路了。我不知道如何從數據庫創建一個數組以適合正確的方式。
如果有人能幫忙,我會非常感激!
我有什麼至今:
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'apptwo');
$query = "SELECT id, username, email FROM users WHERE status=1";
$result = mysqli_query($dbc, $query);
$users = array();
while ($row = mysqli_fetch_assoc($result)) {
$users[$row['email']] = $row['email'];
$users[$row['username']] = $row['username'];
$users[$row['id']] = $row['id'];
}
// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user["id"]] = array (
"{fullname}" => $user["username"],
"{transactions}" => $user["email"]
);
}
$spool = new Swift_FileSpool(__DIR__."/spool");
// Setup the transport and mailer
$transport = Swift_SpoolTransport::newInstance($spool);
// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.");
$message->setFrom("[email protected]", "exampleco");
// Send the email
foreach($users as $user) {
$message->setTo($user["email"], $user["fullname"]);
$result = $mailer->send($message);
}
echo "SPOOLED $result emails";
?>
有靜態數據的工作版本陣:
<?php
$users = array(
array(
"fullname" => "name",
"operations" => 100,
"email" => "[email protected]"
),
array(
"fullname" => "name2",
"operations" => 50,
"email" => "[email protected]"
)
);
// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user["email"]] = array (
"{fullname}" => $user["fullname"],
"{transactions}" => $user["operations"]
);
}
$spool = new Swift_FileSpool(__DIR__."/spool");
// Setup the transport and mailer
$transport = Swift_SpoolTransport::newInstance($spool);
// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.");
$message->setFrom("[email protected]", "exampleco");
// Send the email
foreach($users as $user) {
$message->setTo($user["email"], $user["fullname"]);
$result = $mailer->send($message);
}
echo "SPOOLED $result emails";
?>
太謝謝你了,簡。
SOLUTION:
與誼PHP框架。
您需要Swiftmailer上傳到本地目錄和路徑 '類' 指向Swiftmailer.php在我的config/main.php
在保護/配置/ main.php ...
'components'=>array(
.....
'mailer' => array(
'class' => 'application.modules.swiftmailer.SwiftMailer',
// Using SMTP:
'mailer' => 'smtp',
// security is optional
// 'ssl' for "SSL/TLS" or 'tls' for 'STARTTLS'
'security' => '',
'host'=>'mail.example.com',
'from'=>'[email protected]',
'username'=>'[email protected]',
'password'=>'pw',
// Using sendmail:
//'mailer'=>'sendmail',
// Logging
// logs brief messages about message success or failhure
'logMailerActivity' => 'true',
// logs additional info from SwiftMailer about connection details
// must be used in conjunction with logMailerActivity == true
// check the send() method for realtime logging to console if required
'logMailerDebug' => 'true',
),
),
decorator.php - 這使得電子郵件,並將它們發送到'假脫機'文件。
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database', $con) or die(mysql_error()) ;
$sql = "SELECT * FROM tablename";
$result = mysql_query($sql, $con);
if($result){
while ($row = mysql_fetch_assoc($result)) {
$user = array(
'email' => $row['email'],
'username' => $row['username'],
'id' => $row['id']
);
$users[] = $user;
}
}
else{
/* error! */
};
// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user["email"]] = array (
"{username}" => $user["username"],
"{transactions}" => $user["id"]
);
}
$spool = new Swift_FileSpool(__DIR__."/spool");
// Setup the transport and mailer
$transport = Swift_SpoolTransport::newInstance($spool);
// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("Subject text");
$message->setBody("<b>Dear {username}</b>,<br><br>Perhaps you would like to try xyz circle. " .
" thank you for the {transactions} transactions you made with us.", 'text/html');
$message->setFrom("[email protected]", "example name");
$message->addPart("Plain text HTML Body You {username}, are our best client ever thanks " .
" thank you for the {transactions} transactions you made with us.", 'text/plain');
// Send the email
foreach($users as $user) {
$message->setTo($user["email"], $user["username"]);
$result = $mailer->send($message);
}
echo "SPOOLED $result emails";
?>
spoolsend.php - 這個運行卷紙隊列發送郵件:
<?php
//create an instance of the spool object pointing to the right position in the filesystem
$spool = new Swift_FileSpool(__DIR__."/spool");
//create a new instance of Swift_SpoolTransport that accept an argument as Swift_FileSpool
$transport = Swift_SpoolTransport::newInstance($spool);
//now create an instance of the transport you usually use with swiftmailer
//to send real-time email
$realTransport = Swift_SmtpTransport::newInstance(
"mail.example.com",
"25"
)
->setUsername("[email protected]")
->setPassword("pw");
$spool = $transport->getSpool();
$spool->setMessageLimit(10);
$spool->setTimeLimit(100);
$sent = $spool->flushQueue($realTransport);
echo "SENT $sent emails";
?>
如果任何人有任何問題,讓我知道,我也許可以幫忙。
謝謝!上面提供了我的解決方案,你的例子幫助我到達那裏。 –