Pourquoi wp_mail () ne me laisse-t-il pas définir l'en-tête From: alors que l'ancien PHP mail () le fera?
5 réponses
- votes
-
- 2011-02-10
Bonjour @helenyhou:
Vouspouvez définir l'en-tête,maispas avec unparamètre.WordPress utilise des "hooks" et les hooks dont vous avezbesoin sont les hooks
'wp_mail_from'
et'wp_mail_from_name'
.Voici les hooks que vouspouvez ajouter aufichier
functions.php
de votrethèmepourmodifier l'en-tête"From:"
lors de l'utilisation dewp_mail()
à l'adressee-mailHelen Hou-Sandi <[email protected]>
:add_filter('wp_mail_from','yoursite_wp_mail_from'); function yoursite_wp_mail_from($content_type) { return '[email protected]'; } add_filter('wp_mail_from_name','yoursite_wp_mail_from_name'); function yoursite_wp_mail_from_name($name) { return 'Helen Hou-Sandi'; }
Hi @helenyhou:
You can set the header, just not with a parameter. WordPress uses "hooks" and the hooks you need are
'wp_mail_from'
and'wp_mail_from_name'
hooks.Here are the hooks you might add to your theme's
functions.php
file to modify the"From:"
header when usingwp_mail()
to the email addressHelen Hou-Sandi <[email protected]>
:add_filter('wp_mail_from','yoursite_wp_mail_from'); function yoursite_wp_mail_from($content_type) { return '[email protected]'; } add_filter('wp_mail_from_name','yoursite_wp_mail_from_name'); function yoursite_wp_mail_from_name($name) { return 'Helen Hou-Sandi'; }
-
Je regardaisjuste lesfiltreset vous avez raison,cela résout leproblème.Je suppose que le Codex devrait êtremis àjour?http://codex.wordpress.org/Function_Reference/wp_mail Également lié: cela semble affecter denombreuxplugins deformulaires,y compris lespuissants GravityForms.J'écris actuellement unformulairepersonnalisé,mais si c'est ainsi que WPest censé se comporter,pourquoi la définition desen-têtes à l'aide de l'ancienneméthodenormalefonctionne-t-ellepour d'autres (etmêmepour certains demes autres sites)?I was just looking at the filters and you are right, it does fix the problem. I suppose the Codex should be updated? http://codex.wordpress.org/Function_Reference/wp_mail Also related: this seems to affect lots of form plugins out there, including the mighty GravityForms. I'm currently writing a custom form, but if this is how WP is supposed to behave, why does setting the headers using the regular old method work for others (and even some of my other sites)?
- 0
- 2011-02-10
- helenhousandi
-
@helenyhou - Pour éviter d'affecter d'autresformulaires,vous devez ajouter lesfiltres dans votrepluginjuste avant appelé `wp_mail ()`,puis les supprimerjuste après.Quant à savoirpourquoi la configuration desen-têtesfonctionne,je ne saispas quels sont vos autres cas d'utilisation,maisje serais surpris si `wp_mail ()`fonctionnerait de cettefaçon.@helenyhou - To keep from affecting other forms you'd need to add the filters in your plugin just before called `wp_mail()` and then remove them right after. As for why setting the headers work, I don't know what your other use-cases are but I'd be surprised if `wp_mail()` would work that way.
- 0
- 2011-02-10
- MikeSchinkel
-
Ahhhhj'aitrouvé ce qui sepassait - un autreplugin ajoutait lesfiltresglobalementpour quelque chose queje n'aipas demandéet queje nepeuxpas désactiver -mauvais,mauvais,mauvais.Alorsmaintenant,lesen-têtesnormauxfonctionnent aprèstout.On dirait que l'utilisation de cesfiltres serait lameilleurepratique,s'ils yfigurent.Ahhhh I found what was happening - another plugin was adding the filters globally for something I didn't ask for and can't opt out of - bad, bad, bad. So now the regular headers do work after all. Seems like using those filters would be best practice though, if they're in there.
- 3
- 2011-02-10
- helenhousandi
-
Il suffit denoterici quetoutfiltre qu'unplugin ajoutepeut être supprimé via remove_filter (HOOK,FUNCTION).Just a note here that any filter a plugin is adding can be removed via remove_filter(HOOK, FUNCTION).
- 0
- 2012-09-16
- Jason Coleman
-
celane fonctionne que si le domaine de l'e-mailestidentique au site.it only works if the from email domain as same as the site.
- 0
- 2018-04-28
- Omer
-
- 2011-02-10
Ehbien,si vous utilisez leformat
From: "Your Name" <[email protected]>\r\n
dans vosen-têtes,vousne devriezpas avoir deproblème (sauf si vous avezinstallé unplugin qui remplace lafonction wp_mail).Cependant,comme Mike l'a dit,vouspouvezfiltrer les valeurs ultimes avec cesfiltres,ou vouspouvez simplementinstaller ceplugin:
Il vous donnera unparamètre d'optionspour déterminer lenomet l'adressee-mail à utiliser dans
wp_mail()
.Well, if you're using the
From: "Your Name" <[email protected]>\r\n
format in your headers, you shouldn't be having a problem (unless you have a plugin installed which overrides the wp_mail function).However, as Mike said, you can filter the ultimate values with those filters, or you can just install this plugin:
It'll give you an options setting to determine what name and email to use in
wp_mail()
. -
- 2011-04-15
Désolé de réactiver une vieille question,maisn'est-ilpaspréférable de la définir via lesen-têtes comme ceci:
$subject = "MyPlugin: Alert (".get_bloginfo('wpurl').")"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type: text/html; charset=".get_bloginfo('charset')."" . "\r\n"; $headers .= "From: MyPlugin <".$this->settings['from_address'].">" . "\r\n"; wp_mail($this->settings['notify_address'], $subject, $alertMessage, $headers);
De cettefaçon,vousn'avezpas à vous soucier d'utiliser unfiltre,puis de le supprimer après
wp_mail()
.Sorry to revive an old question but isn't it better to set via the headers like so:
$subject = "MyPlugin: Alert (".get_bloginfo('wpurl').")"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type: text/html; charset=".get_bloginfo('charset')."" . "\r\n"; $headers .= "From: MyPlugin <".$this->settings['from_address'].">" . "\r\n"; wp_mail($this->settings['notify_address'], $subject, $alertMessage, $headers);
That way you don't have to worry about using a filter and then removing at after
wp_mail()
.-
Jen'aipas défini lefiltre,un autreplugin l'afait.Lefiltre apriorité sur l'en-tête.Aussi,vous devriez vraiment utiliser `site_url ()` au lieu de `get_bloginfo ('wpurl')`I did not set the filter, another plugin did. The filter takes precedence over the header. Also, you really should use `site_url()` instead of `get_bloginfo('wpurl')`
- 2
- 2011-04-16
- helenhousandi
-
Mais alors sûrement cepluginestmauvaispourne pas s'éclaircir après lui-même .... vousne pouvezpasfairegrand chose à ce sujet cependant: (dans ce cas,je feraismieux de changermonpluginpour utiliser lefiltrejuste au cas où un autreplugin l'aurait cassé.But then surely that plugin is bad for not clearing up after itself.... not alot you can do about it though :( in that case I better change my plugin to use filter just in case another plugin has broken it.
- 0
- 2011-04-16
- Scott
-
@helenyhou - +1pourm'avoir rappelé site_url ().J'ai également changémonpluginpour utiliser desfiltresplutôt que desen-têtes $.Deplus,je me suis assuré de supprimer cesfiltres unefois quej'aienvoyé l'e-mail afin dene pasgêner quoi que ce soit d'autre :)@helenyhou - +1 for reminding me about site_url(). I've also changed my plugin to use filters rather than using $headers. Plus I've made sure to remove those filters once I've sent the email so not to get in the way of anything else :)
- 0
- 2011-04-16
- Scott
-
oui,cepluginestmauvaispourne pasme donner lapossibilité dene pas utiliser cesfiltres,mais heureusementen commentant lesfiltres élaboréspourmon cas.Je vaisprobablement demander autour de vouspour voir ce quiestpréférable d'utiliser à l'avenir - unfiltrepar utilisation ou lesen-têtes lesplusfamiliers.Lesen-têtes sontprobablementplusefficaces,mais alorspourquoi cesfiltres sont-ils là?Toujoursplus de questions :)yes, it's that plugin's bad for not giving me an option not to use those filters, but luckily commenting out the filters worked out for my case. I'll probably ask around to see which is better to use in the future - a per-use filter or the more familiar headers. The headers are probably more efficient, but then why are those filters there? Always more questions :)
- 0
- 2011-04-26
- helenhousandi
-
Apartir de la version actuelle,ilesttout àfait acceptable d'utiliser `get_bloginfo ('wpurl')`: Cela renvoie déjà `site_url ()`.`get_bloginfo ('home')`et `get_bloginfo ('siteurl')` sont obsolètes.https://developer.wordpress.org/reference/functions/get_bloginfo/As of the current version, it is totally acceptable to use `get_bloginfo('wpurl')`: This returns `site_url()` already. `get_bloginfo('home')` and `get_bloginfo('siteurl')` are deprecated. https://developer.wordpress.org/reference/functions/get_bloginfo/
- 0
- 2016-12-25
- James M
-
- 2015-09-14
C'est unpeu un hackmais vouspouvez également utiliser labalise d'en-tête
Reply To
:$headers = 'Reply-To: "Aaren A. Aarenson" <[email protected]>';
Malheureusement,cela ajoute l'adressee-mail à la liste De,et la réponse signifie que vous devrez supprimermanuellement l'adresse configurée dans lefiltre
wp_mail_from
.It's a bit of a hack but you can also use the
Reply To
header tag:$headers = 'Reply-To: "Aaren A. Aarenson" <[email protected]>';
Sadly this adds the email address to the From list, and replying means you will have to manually remove the address configured in the
wp_mail_from
filter.-
Cela a réglé leproblèmepourmoi.Vous avez sauvé un compagnon de vie!;)This fixed it for me. You saved a life mate! ;)
- 0
- 2017-06-26
- user382738
-
- 2015-08-14
J'aieu lemêmeproblème.Pourmoi,ilest apparu que lefournisseur d'hébergement (BlueHost)empêchait de changer le champ de.Ici,ils l'expliquent https://my.bluehost.com/cgi/help/206 .
J'ai résolu leproblèmeen ajoutant l'e-mail auxboîtes aux lettres cPanel comme on dit.
I had the same problem. For me it came out that the hosting provider (BlueHost) was preventing from changing the from field. Here they explain it https://my.bluehost.com/cgi/help/206 .
I fixed the problem by adding the email to cPanel mailboxes just as they say.
Lorsquej'utilise
wp_mail( $to, $subject, $message, $headers )
(avec des valeursen place,bien sûr),l'e-mailestenvoyé avec unnomet une adressee-mailnon 't définipartout oùje peuxtrouver (mêmepas dans lesparamètres PHP ou Apache).Cependant,utilisermail( $to, $subject, $message, $headers )
fonctionneplutôtbien.Quepourrait-il sepasser avecwp_mail()
pourprovoquer cela?