Wordpress wp_mail() not working when user not logged in? - wordpress

This is my code to send mail.
$htmlcontent = "Quick Contact";
$to = get_option( 'admin_email' );
$subject = "Quick Contact";
$headers = "Quick Contact";
wp_mail( $to, $subject, $htmlcontent );
This code works fine when the user is logged in. When the user is not logged in the mail is not sent.

of try this code.
add_action('wp_ajax_my_ajax', 'my_ajax');
add_action('wp_ajax_nopriv_my_ajax', 'my_ajax');
add ajax to register or non register user allow to access.

Related

Wordpress How to send an "Set Password" E-Mail to users that got imported by script

I try to send an "Set your Password" mail, which I can customise, for a Site with a custom user role "Händler", in WordPress. These users get imported via script and XML-File.
So I want, when the script adds these ≈1200 users and they aren´t already present, to automatically or on button click receive an email with a link, where they can set a password for that user.
So it needs to work like a "Verification-process", because only when these users have set their password and accept that way, that they show up on the website.
I tried some Plugins already, but none of them fits my needs completely.
Plugins I already tried:
Import and export users and customers
User Verification (thats almost right, but their is no chance to let the user set an password, instead of just activating their account)
Can anyone point me in the right direction here, cause I googled for some time now, but nothing helped me yet.
Thanks for your help, appreciate it.
I found a solution for this particular problem.
I loop through the users with the custom user role and send them via wp_mail() the "Reset Password" Mail with custom content.
function mm_send_password_reset_mail(){
$users = get_users( array( 'role' => array( 'haendler' ) ) );
$output = '';
foreach ( $users as $useri ) {
$user = get_user_by('id', $useri->ID);
$firstname = $user->first_name;
$email = $user->user_email;
$adt_rp_key = get_password_reset_key( $user );
$user_login = $user->user_login;
$rp_link = 'Passwort vergeben';
$output .= $user->user_email;
if ($firstname == "") $firstname = "Händler";
$message = "Hallo ".$firstname.",<br>";
$message .= "Sie wurden auf der Seite ".get_bloginfo( 'name' )." als Händler angelegt mit der E-Mail-Adresse ".$email."<br>";
$message .= "Klicken Sie hier, wenn Sie damit einverstanden sind, das Sie auf der Webseite als Händler öffentlich aufgelistet werden: <br>";
$message .= $rp_link.'<br>';
$subject = __("Vergeben Sie ein Passwort für ".get_bloginfo( 'name'));
$headers = array();
add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
wp_mail( $email, $subject, $message, $headers);
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
echo json_encode(array('user' => $output));
die();
}
I added a custom link in the admin toolbar and call the above function via ajax. That works like a charm.
Thanks anyway.

Wordpress: Send a notification when a user registers, but only for users with Subscriber role

I'm trying to send have my site send me a notification email when someone registers with the role subscriber. I can probably achieve that with a hook on mu-plugins but I don't know where to start or which hook to use. Tried adding an if statement inside a plugin but it's probably overkill to install a plugin and modify it just for this functionality. Thanks!
For your information the default register user role is "Subscriber" role, if you have set the other role you have to change the role after registered.
Try this one:
function send_welcome_email_to_new_user($user_id) {
$user = get_userdata($user_id);
$user_email = $user->user_email;
// email will send only for "Subscriber" registers
if ( in_array( 'subscriber', $user->roles )) {
$to = $user_email;
$subject = "Hi";
$body = '
<p>your message </p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}
}
// email will send only for "Other Role" registers
if ( in_array( 'other_role', $user->roles )) {
$to = $user_email;
$subject = "Hi";
$body = '
<p>your message </p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}
}
}
add_action('user_register', 'send_welcome_email_to_new_user');
It is enabled by default in WordPress. Check that WordPress is sending emails at all, you might have problem with the SMTP settings, you need to configure the SMTP. Where is your website hosted? Did you check the spam?

How to change the language of the email dynamically according to user's language in WordPress?

I'm looking for a way to send a simple WordPress email according to user's language using wp_mail() function via a custom-built plugin. I'm able translate the email and receive the email in the right language once I switch WordPress language via Settings. But how to change the language of the email dynamically according to user's language?
I have saved a list of users with their preferred languages. The subject and email content is translated into different languages. For testing purposes, I have been trying to force the locale, but it does not seem to be doing anything and the email is still arriving in the default WordPress en_US locale.
$locale = 'es_ES';
switch_to_locale($locale);
$to = 'test#email.com';
$subject = __('Email subject', 'textdomain');
$message = __('My email content', 'textdomain');
wp_mail( $to, $subject, $message, $headers );
restore_previous_locale();
I would be very grateful to receive some feedback, experience or knowledge how to briefly switch the locale for some strings and then restore it back to the original?
Seems like I finally got this to work.
First - make sure you do have .mo translation files ready with the appropriate translations
After calling switch_to_locale($locale); you have to also load textdomain. I used load_plugin_textdomain() function.
Here is the updated code if anyone comes across this threads:
$locale = 'es_ES';
switch_to_locale($locale);
load_plugin_textdomain('textdomain', false, basename( plugin_dir_path( __DIR__ ) ) . '/languages');
$to = 'test#email.com';
$subject = __('Email subject', 'textdomain');
$message = __('My email content', 'textdomain');
wp_mail( $to, $subject, $message, $headers );
restore_previous_locale();

retrieve_password_message is not working as i use for password retrive message

I have used the bellow code. But it is not working.
function site_change_password_mail_message( $message, $key ) {
$message = __( 'Hi ###USERNAME###,
This notice confirms that your password was changed on JDRF. If you did not change your password, please
contact JDRF Support at siteurl.com/inquiry. This email has been sent to ###EMAIL###.
Regards,
All at siteurl.com
[Application Homepage, ex. ###SITEURL###]');
return $message;
}
add_filter( 'retrieve_password_message', 'site_change_password_mail_message', 10, 3 );
Should i change this ? I am still getting this message.
Hi ranjit,
Someone has requested a new password for the following account on K Gems & Crystals:
Username: username
If you didn't make this request, just ignore this email. If you'd like to proceed:
Click here to reset your password
Thanks for reading.
If you are going to send an email after registering a user and you want to change the default email from wordpress. you should use this little code.
add_fileter("wp_new_user_notification_email", "callback",10,3);
function callback( $message, $user, $blogname) {
$message['headers'] = array('Content-Type: text/html; charset=UTF-8');
$message['subject'] = sprintf( '[%s] Site Name.', $blogname );
$message['message'] = sprintf( "%s ( %s ) has registerd to your blog %s.",
$user->user_login, $user->user_email, $blogname );
return $message;
}
If it is for the email sending the change of password
function filter_retrieve_password_message( $message, $key, $user_login, $user_data ) {
// make filter magic happen here...
return $message;
};
// add the filter
add_filter( 'retrieve_password_message', 'filter_retrieve_password_message', 10, 4 );
I can see that you are not using proper arguments to the filter what you are using. You need to use proper argument to the filter. Try this code
add_filter( 'retrieve_password_message', 'retrive_reset_password_msg', 10, 4 );
function retrive_reset_password_msg( $message, $key, $user_login, $user_data ) {
$message = __( 'Hi ###USERNAME###,
This notice confirms that your password was changed on JDRF. If you did not change your password, please
contact JDRF Support at siteurl.com/inquiry. This email has been sent to ###EMAIL###.
Regards,
All at siteurl.com
[Application Homepage, ex. ###SITEURL###]');
return $message;
}

wp_mail vs php mail()

I'm trying to solve a problem with email being sent from a WordPress site, hosted on GoDaddy with its MX record set to a Dreamhost.com mail server.
Email addressed to the site's domain (me#mysitesdomain.com) will not go through, yet email to any #gmail.com address will go through. I'm testing this with the following script ...
<?php
$mailResult = false;
$from = "info#mysitesdomain.com";
$to = "me#mysitesdomain.com";
$subject = "PHP Mail Test script";
$message = "This is a test to check the wp_mail functionality.";
$headers = "From:" . $from;
$mailResult = wp_mail( $to, $subject, $message, $headers );
echo "mail result: ".$mailResult;
?>
For both the me#mysitesdomain.com and the me#gmail.com address, the script's result is 1 (true).
As a further test, I use a similar very simple script outside of Wordpress that uses PHP mail()...
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "info#mysitesdomain.com";
$to = "me#mysitesdomain.com";
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality.;
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
That script successfully sends to both addresses. So, PHP mail() works but wp_mail() only works for #gmail.com addresses. What's going on within the wp_mail class that's different PHP php mail()?

Resources