How to send mail in simple_jwt_login wordpress plugin hook? - wordpress

I am using this plugin for Wordpress:
https://simplejwtlogin.com/docs/hooks/#1-simple_jwt_login_login_hook
When someone logs in on my external app through rest api, I want wordpress to send email by triggering the hook:
functions.php
add_action('simple_jwt_login_login_hook', 'my_test');
function my_test() {
$to = 'you#yourmail.com';
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "me#email.com";
$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = "From:" . "me#email.com";
wp_mail($to,$subject,$message,$headers);
}
I also have "Easy SMTP" plugin installed and working properly (wp_mail() should go through my smtp mail)
I don't see any errors in error.log, nor anything in mail.log. Do you think the hook is getting triggered or not?

make sure you enable simple_jwt_login_login_hook
to test the hook, just throw an error in it and see if its captured on error log
like this
add_action('simple_jwt_login_login_hook', function( $user ) {
throw new Exception('I\'m here mortals');
}, 11 , 1);
or via init
add_action('init', function() {
add_action('simple_jwt_login_login_hook', function( $user ) {
throw new Exception('I\'m here mortals');
}, 11, 1);
}, 99)

Related

Wordpress smtp mail from address is not working

i am using wp_mail_smtp for sending mail in WordPress and its working fine expect one thing i have define the from email : abc#mysite.com and from name: my site and username password for smtp authentication is : someemail#example.com and password of my mail id.
now problem is that when i sending the mail than i receive mail header like
mysite< someemail#example.com > and it should be mysite< abc#mysite.com > i have also set the filter for from name and from mail like below code
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from($old) {
return 'abc#mysite.com';
}
function new_mail_from_name($old) {
return 'mysite.com';
}
and my mail code is
$headers = 'From: abc#mysite.com ' . "\r\n";
$to = "admin#test.com"
$send_subject = "Mail Subject"
$message .= "Some message";
wp_mail( $to, $send_subject, $message,$headers );
please tell me what i am doing wrong so that i can receive mail header correctly
Thanks

Contact form 7 email using SMTP

I created a web site using WordPress.
I added Contact Form 7 in my side bar.
After I filled the fields in the contact form, and click the Send button, I got an error message in a red box:
There was an error trying to send your message. Please try again
later.
I did not install phpmailer. I wrote a test php (testemail.php showed below). When I visit http://www.MyDomainName.com.au/testemail.php in the browser, it shows whole codes of the testemail.php!
Do I have to install a phpmailer plugin, for example, Easy WP SMTP?
What is the problem in testemail?
Finally how to make my contact form working?
The testemail.php code:
function sendMail($request) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = mail#MyDomainName.com.au;
$mail->SMTPAuth = true;
$mail->Username = 'mail#MyDomainName.com.au';
$mail->Password = 'myPassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('mail#MyDomainName.com.au', 'Title');
$mail->addAddress(xxx#hotmail.com);
$mail->addReplyTo('mail#MyDomainName.com.au');
$mail->isHTML(true);
$mail->Subject = '$Something';
$mail->Body = 'The body of the email';
$mail->AltBody = 'Alternative'; // this is mostly sent to your own mail
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}

PHPMailer & WordPress "Post by Email" - not creating post

I'm creating a script for a client using their PHP 5.4.16 (updating is not in the scope of this project), and therefore PHPMailer 5.2.25. My script (below) works if I change the SetAddress to my personal Gmail account (I get the email with subject, body, and attachment (the latter being the reason for using PHPMailer)), but if I change the SetAddress to my WordPress "Post by Email" address, nothing seems to be delivered.
Questions:
Is there anything wrong with my script? Missing headers? Badly formatted email?
If my script is apparently OK, what other avenues of investigation might there be?
Thanks
<?php
echo "<p>" . date("h:i:sa");
?>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/../www/phpmailer/class.phpmailer.php'); ?>
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/../www/phpmailer/class.smtp.php'); ?>
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
//$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "my#gmail.com";
$mail->Password = "mypassword";
$mail->From = "my#gmail.com";
$mail->FromName = "Me";
//$mail->AddAddress("caju317davu#post.wordpress.com");
$mail->AddAddress("my#gmail.com");
$mail->AddReplyTo("my#gmail.com","developer");
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'] . '/../www/_assets/articles/223/610/7f10120230e612e03eea9aa54a48a68f.jpg', 'attachment.jpg');
$mail->Subject = "My test email";
$mail->Body = "Hi! This is my first successful post created through email.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Now I am not sure if this will help at all but below is the code that I have for a working version of PHPMailer v5.2.9 that is running on a site using PHP version 5.4.16 so may help. The obvious differences I can see is the recipient's section don't have "set" or "Add" and also I can't seem to find the initiator ($mail->send();). Let me know if this helps and for any areas needing extra clarification.
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.host.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail#example.co.uk'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('email#example.co.uk', 'Company Name');
$mail->addAddress($email); //Gets the email the enquiry form field contains
$mail->addReplyTo('email#example.co.uk', 'Company Name');
$body='<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica,
sans-serif; font-size:12px; margin:0; padding:0;">
<p>Thank you for your enquiry, a member of the team will be in touch shortly to discuss your requirements. In the mean time, please double check the below details entered on our website. If any of these are incorrect or you wish to add to it, please contact us via email or over the phone.</p></div>';
//Content
$mail->isHTML(true);
$mail->Subject = $subject ;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo $thankyou_text;
} catch (Exception $e) {
echo $failed_text;
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
From a 'Happiness Engineer' at WordPress:
Hi there!
At WordPress.com we don't allow posts to be sent automatically from
scripts such as PHPMailer.
I guess that answers that! Now to forget PHPMailer, and try and get back to default PHP mail() :(
Sorry to have bothered you all

Adding a wordpress user to Salesforce Contact

I'm trying to do a relatively simple task by hooking into the Wordpress registration and adding the user that's being registered to a Salesforce db. When I run the Salesforce db code outside of Wordpress it works flawlessly, but when I test this by registering on my wordpress website, I get an error stating: INVALID_LOGIN: Invalid username, password, security token; or user locked out.
Additionally, I receive this error from Wordpress "Cannot modify header information - headers already sent" which doesn't allow me to view the entire object data that's being sent to Salesforce.
This is my code:
$SF_USERNAME = 'test';
$SF_PASSWORD = 'test';
define( 'CD_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
require_once (CD_PLUGIN_PATH . 'Toolkit/soapclient/SforceEnterpriseClient.php');
require_once (CD_PLUGIN_PATH . 'Toolkit/soapclient/SforceHeaderOptions.php');
function add_user_to_SF($user_id) {
$user = get_userdata($user_id);
try {
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
$mylogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);
print '<pre>';
print_r($mylogin);
print '</pre>';
print '<br/><br/>';
$sObject = new stdclass();
$sObject->FirstName = $user->first_name;
$sObject->LastName = $user->last_name;
$sObject->Email = $user->user_email;
//echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create(array($sObject), 'Contact');
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
$errors->add( 'demo_error', __(print_r($_POST),'mydomain') );
$errors->add( 'demo_error', __($mySforceConnection->getLastRequest(),'mydomain') );
$errors->add( 'demo_error', __($e->faultstring,'mydomain') );
return $errors;
}
}
add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );
This is a php scope issue.
Adding:
global $SF_USERNAME;
global $SF_PASSWORD;
inside the function fixed the problem.
It looks like you might be missing the security token. It's appended to the end of the password.
This link explains how to generate the token
https://login.salesforce.com/help/doc/en/user_security_token.htm
You need to add your salesforce account security token with password
eg. Password - "testing"
Security Token - "2321njjn32j32"
You need to pass as - "testing2321njjn32j32"
This will work properly.

wordpress wp_signon function not working

I am using wp_signon() function to login the user. I am doing this like
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
i want to send user to home page after login.
But i Am facing following error:
Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\wpmoodle\wp-content\themes\twentyten\header.php:12) in E:\xampp\htdocs\wpmoodle\wp-includes\pluggable.php on line 690.
Thanks in advance.
wp_signon() needs to run before you've sent any of your actual page to the browser.
This is because part of what wp_signon() does is to set your authentication cookies. It does this by outputting a "Set-Cookie: ..." header -- if you look at line 690 of pluggable.php, where your error comes from, you'll see that that line sets a cookie.
So, because wp_signon() outputs headers, you can't already have sent any content -- because headers must always be output before content.
However, the error indicates that you've already sent some output -- on line 12 of header.php, presumably some of the first HTML of the standard WordPress theme.
This basically indicates that you need to move your wp_signon() call to somewhere earlier in the WordPress processing, so it has a chance to output its headers before any page content is sent.
If someone needs it, here is my solution:
function custom_login() {
if (isset($_POST['submit'])) {
$login_data = array();
$login_data['user_login'] = sanitize_user($_POST['username']);
$login_data['user_password'] = esc_attr($_POST['password']);
$user = wp_signon( $login_data, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
} else {
wp_clear_auth_cookie();
do_action('wp_login', $user->ID);
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, true);
$redirect_to = $_SERVER['REQUEST_URI'];
wp_safe_redirect($redirect_to);
exit;
}
}
}
add_action( 'after_setup_theme', 'custom_login' );

Resources