我尝试使用SMTP在Wordpress中使用以下代码发送电子邮件,并且可以正常工作。
function wow_phpmailer_init( $phpmailer ) {
$phpmailer->SMTPAuth = true;
$phpmailer->Host = 'mail.gmail.com';
$phpmailer->Port = '465';
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'mypassword';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->Mailer = 'smtp';
}
add_action( 'phpmailer_init', 'wow_phpmailer_init' );
现在,我需要为每个SMTP信息使用动态值,例如:
function wow_phpmailer_init( $phpmailer ) {
$phpmailer->SMTPAuth = true;
$phpmailer->Host = get_post_meta( $id, 'smtp_host', true );
$phpmailer->Port = get_post_meta( $id, 'smtp_port', true );
$phpmailer->Username = get_post_meta( $id, 'smtp_usr', true );
$phpmailer->Password = get_post_meta( $id, 'smtp_pass', true );
$phpmailer->SMTPSecure = get_post_meta( $id, 'smtp_encrypt', true );
$phpmailer->Mailer = 'smtp';
}
add_action( 'phpmailer_init', 'wow_phpmailer_init' );
问题是如何在函数wow_phpmailer_init($ phpmailer)中传递动态$ id而不使用GLOBAL var(f或example global $ id;)等?
由于我不确定do_action('phpmailer_init')发生在哪里,因此缺少一些上下文。它最有可能在您循环中的某个位置或正在计算帖子ID的位置。您可以根据需要将尽可能多的变量传递给do_action,只要增加add_action函数的参数数量即可。
<?php
// function signature
function wow_phpmailer_init( $phpmailer, $id ) {
// do stuff
}
// argument count is the 4th parameter
add_action('phpmailer_init', 'wow_phpmailer_init', 10, 2);
// somewhere else in your code you have something like this
do_action('phpmailer_init', $phpmailer, $post_id);
编辑:另一个选择是使用闭包。
<?php
$id = ;// calculated id
add_action('phpmailer_init', function($phpmailer) use ($id) {
// do stuff
});
wp_mail(); // send mail, which calls do_action('phpmailer_init')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句