public function send_newsletter()
{
global $wpdb;
$recipients = $wpdb->get_results("SELECT email FROM {$wpdb->prefix}zero_newsletter_email");
$object = get_option('zero_newsletter_object', 'Newsletter');
$content = get_option('zero_newsletter_content', 'Mon contenu');
$sender = get_option('zero_newsletter_sender', '[email protected]');
// Enverra la valeur de l'option et s'il n'a pas trouvé qu'il envoie le second paramètre
$header = array('From: '.$sender);
$result = wp_mail($recipients[0]->email, $object, $content, $header);
var_dump($result); // false
}
2 Answers
Have you verified the hosting environment can send emails? If it is a local test environment, your email may be getting caught up on your own computer. Many of the hosting environments prevent emailing to prevent spam.
I would probably try using the default emailing capabilities of a working plugin first to test this. Or, try a very simple script:
var_dump(wp_mail('[email protected]', 'my test subject', 'here is the message'));
die;
If this isn't working, I'd try to remove the environment/install from being an issue. Try this simple script on a fresh install or new environment. Do it until you see success then replace it with your own. From there, you can use a series of var_dump() commands to see what the variables are holding and you will be able to pinpoint what is going wrong.
-
Thank you for your reply, and i confirm that it a local test environnement so if the email is caught by my own computer how can i change the processus ? But for now i will try you script. Sep 21 at 10:43
-
Howeover, there is Wp Mail SMTP plugin ( this is the link wordpress.org/plugins/wp-mail-smtp/#description if you want to test it) that can do it but what i'm doing it is without plugin Sep 21 at 11:05
-
The script above should be able to send an email if it is setup very plain/clean and your computer is able to send. If it is your environment that is the issue, you could go through the steps of troubleshooting your environment, or find a different hosting solution that allows email. It starts to be a different question here because it is server related, not code related. You can use that plugin, it will likely work in the same manner if you choose PHP emailer, but it may not use the WP specific code that you've listed above.– AdamSep 22 at 17:02
-
i think you right because i used PHP Mailer for a project only made with php code not with the wordpress framework and the library work very well i can send email very quickly et clearly with my variables so i think i will configure to implement in WordPress functionality so if you can help me to show how to do it or maybe put on paths. I will be very greateful. Sep 24 at 16:28
-
If you do a new install of WP and open your functions file, use: var_dump('working here');die; Once that shows on the screen and only that, you know your code is being hit. Then replace with: var_dump(wp_mail('[email protected]', 'my test subject', 'here is the message')); die; The result should tell you if it worked. If it works, substitute one variable at a time with what you are trying to do. If this helps, please remember to mark it as the answer to help my reputation in here.– Adam2 days ago
First, you don't ask a question. But I assume that you are getting a false return from the mail() command. That can happen for a number of reasons.
I'd check to make sure that your query is actually returning results. If the 'recipient' value is empty, the mail() will fail, for instance.
So, dump your query results. Make sure they work. And change your code to test for valid results. If the results are empty for some reason, or the record doesn't have an email returned, then you need to test for that and not do the mail() command.
-
thank you for your reply , it was because i was very quick to ask but i see you understood what i mean. In fact, i dump each parameter of the wp_mail function but they are all good it's only $result that send me false. moreover, about my code from the begining of my test i used a foreach loop to send several mails to emails in $recipients . But it did'nt work so i use only the first email sent by my request with that syntaxe $result = wp_mail($recipients[0]->email, $object, $content, $header); Sep 21 at 10:59