クリックイベントでpersonalizetest.php
(電子メールを送信するswiftmailerコードがあります)をロードしています。下のようなjqueryコードのhtmlファイルのボタンをクリックしてロードすると、動作し、電子メールを送信します。しかし、WordPressのPHPファイルに同じコードを追加してボタンをクリックすると、コンソールで「アクションが正常に実行されました」と表示されますが、電子メールは表示されません。クリックボタンで読み込むPHPファイル
私はちょっと調べて、function.phpに次のコードを追加してWordpressでファイルを実行する必要があることを発見しましたが、今はすべてのページの更新時に電子メールを送信しています。私はウェブサイトを開いたり、ボタンをクリックするのではなく、あるページから別のページに移動したりします。
function.php
<?php
function enqueue_scripts_styles_init() {
wp_enqueue_script('ajax-script', get_template_directory_uri().'./personalizetest.php', array('jquery'), 1.0); // jQuery will be included automatically
wp_localize_script('ajax-script', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php'))); // setting ajaxurl
}
add_action('init', 'enqueue_scripts_styles_init');
?>
jQueryのコード
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'personalizetest.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
console.log("action performed successfully");
});
});
<input type="submit" class="button" name="insert" value="insert" />
personalizetest.php(ではSwiftMailerコード)
:要するに、私はclass=button
でボタンをクリックし、この
personalizetest.php
ファイルをロードしています
<?php
require_once '/lib/config.php';
require_once 'MyDecoratortest.php';
function send_my_mail() {
try {
//connect to database
$conn = new mysqli($root, $dbun, $dbps, $dbnm);
//get receipents
$recipients =[];
$sql = 'SELECT email, firstname, order_id, FROM order_details order by _id desc limit 0, 1';
$result = $conn->query($sql);
$i = 0;
while ($row = $result->fetch_assoc()) {
$recipients[$i]['email'] = $row['email'];
$recipients[$i]['firstname'] = $row['firstname'];
$recipients[$i]['order_id'] = $row['order_id'];
$i++;
}
// create the transport
$transport = Swift_SmtpTransport::newInstance($smtp_server, 587, 'tls')
->setUsername($username)
->setPassword($password);
$mailer = Swift_Mailer::newInstance($transport);
// create and register decorator
$decorator = new Swift_Plugins_DecoratorPlugin(new MyDecorator($conn));
$mailer->registerPlugin($decorator);
//email
$html_message="My email text";
// prepare email message
$message = Swift_Message::newInstance()
->setSubject('Your Order at Dissertation Sage -- #order_id')
->setFrom($from)
->setBcc($bcc)
->setBody($html_message, 'text/html');
// tracking variables
$sent = 0;
$failures = [];
// send the personalized message to each recipient
foreach ($recipients as $recipient) {
$message->setTo([$recipient['email'] => $recipient['firstname']]);
$sent += $mailer->send($message);
}
// display result
if ($sent) {
echo "Number of emails sent: $sent<br>";
}
if ($failures) {
echo "Couldn't send to the following addresses:<br>";
foreach ($failures as $failure) {
echo $failure . '<br>';
}
}
} catch (Exception $e) {
echo $e->getMessage();
}}?>
おかげでステップアップの修正のために。私は初心者で英語ではうまくありません。あなたの書いたように書くつもりです。 –