Faites quelque chose après avoir envoyé un e-mail
1 réponses
- votes
-
- 2015-06-09
Utilisation de la classe PHPMailer avec un rappel d'action:
J'aifouillé dans
PHPMailer.
et a découvert qu'elleprenden charge une classe actionpersonnalisée .Voici comment le rappelest activé avec le
doCall/code>
dans la classe.Ilexiste également un
PHPMailer
sur GitHuben utilisant cettefonctionnalité via le rappelcallbackAction ()
.Nouspouvons le configurer dans WordPress avec:
$phpmailer- > action_function='wpse_mail_action';
où
wpse_mail_action ()
est le rappel d'action.Voici unexemple de lafaçon dontnouspouvons appliquer cela:
/** * Rappel d'actionpersonnalisé PHPMailer */ function wpse_mail_action ($is_sent,$to,$ cc,$bcc,$ subject,$body,$from) { do_action ('wpse_mail_action',$is_sent,$to,$ cc,$bcc,$ subject,$body,$from); return $is_sent;//n'apas vraimentbesoin de ce retour! } /** * Configurer un rappel d'action PHPMailerpersonnalisé */ add_action ('phpmailer_init',fonction ($phpmailer) { $phpmailer- > action_function='wpse_mail_action'; });
Nous avonsmaintenant accès au hook
wpse_mail_action
.Nouspourrions alors ajouternotrepropreenregistreur de courrieret vérifier si lesmessages ont étéenvoyés avec succès ounon.
Exemple:
/** * Faites quelque chose après l'envoi dumessage "Réinitialisation dumot depasse": */ add_action ('wpse_mail_action',function ($is_sent,$to,$ cc,$bcc,$ subject,$body,$from) { if ($is_sent & amp; & amp;false!==stripos ($ subject,'Password Reset')) //faire des choses },10,7);
oùnouspourrions ajouter d'autres restrictionset intégrer d'autres actions sinécessaire,comme le hook
retrieve_password
.Using the PHPMailer class with an action callback:
I did some digging into the
PHPMailer
class and found that it supports a custom action.Here's how the callback is activated with the
doCallback()
method in the class.There's also a
PHPMailer
test on GitHub using this feature via thecallbackAction()
callback.We can set it up in WordPress with:
$phpmailer->action_function = 'wpse_mail_action';
where
wpse_mail_action()
is the action callback.Here's an example how we can apply this:
/** * Custom PHPMailer action callback */ function wpse_mail_action( $is_sent, $to, $cc, $bcc, $subject, $body, $from ) { do_action( 'wpse_mail_action', $is_sent, $to, $cc, $bcc, $subject, $body, $from ); return $is_sent; // don't actually need this return! } /** * Setup a custom PHPMailer action callback */ add_action( 'phpmailer_init', function( $phpmailer ) { $phpmailer->action_function = 'wpse_mail_action'; } );
Now we have access to the
wpse_mail_action
hook.We could then add our own mail logger and check if the mails were successfully sent or not.
Example:
Here's an (untested) example how we could do something after "Password Reset" posts are sent:
/** * Do something after the "Password Reset" post has been successfully sent: */ add_action( 'wpse_mail_action', function( $is_sent, $to, $cc, $bcc, $subject, $body, $from ) { if( $is_sent && false !== stripos( $subject, 'Password Reset' ) ) // do stuff }, 10, 7 );
where we could add some further restrictions and wrap into other actions if neccessary, like the
retrieve_password
hook.-
Merci àtous.Je viens de rendre lepluginet ilestmaintenant accessible àpartir du répertoire duplugin wordpress: https://wordpress.org/plugins/telegram-for-wpThank you at all. I've just made the plugin and its now accessible from wordpress plugin directory: https://wordpress.org/plugins/telegram-for-wp
- 0
- 2015-06-25
- Ameer Mousavi
-
Bienvenueet félicitations avec lenouveauplugin @AmirMousaviYou're welcome and congratulation with the new plugin @AmirMousavi
- 1
- 2015-06-25
- birgire
-
Il y a un conflit avec leplugin Contact Form 7.Ilne peutpas recevoir le résultat de l'envoi.Son chargeur Ajaxtourne depuistoujours;même lorsque l'e-mailestenvoyé avec succès.There is a conflict with Contact Form 7 plugin. It can't receive the result of sending. Its Ajax loader has been spinning forever; even when email sent successfully.
- 0
- 2015-07-05
- Ameer Mousavi
-
J'aitesté cetteméthode avec le CF7et cela afonctionné commeprévu.Si l'ajaxloadertourneindéfiniment,celapourraitindiquer deserreursjavascript/PHP/Server sans rapportet donc `wp_mail ()`pourraitne jamais être appelé dans CF7.Notez que lespluginstiers sont hors sujetici sur WPSE.@AmirMousaviI tested this method with the CF7 and it worked as expected. If the ajaxloader is spinning forever, it could indicate unrelated javascript/PHP/Server errors and therefore `wp_mail()` might never be called within CF7. Note that 3rd party plugins are off topic here on WPSE. @AmirMousavi
- 2
- 2015-07-05
- birgire
Je veuxfaire quelque chose après que WordPress aenvoyé une-mail.Parexemple,après avoirenvoyé une-mail "Réinitialiser lemot depasse" à l'aide de lafonction
wp_mail()
.