Why is using wp_mail() with sendgrid api key setup not showing "via Sendgrid.com" at the sender name? - php

I noticed that when I use a library in nodejs or a plugin in wordpress, the sent email will have a sender section that look like this "name<example#gmail.com> via <smtp service name>". What does the "via <service name domain>" try to tell us, and why I can't not see it when using the same api key but using wp_mail() function.
define( 'SMTP_USERNAME', 'apikey' );
define( 'SMTP_PASSWORD', 'SG.xxxxxxxxxxxxxxxxxxxx' );
define( 'SMTP_SERVER', 'smtp.sendgrid.net' );
define( 'SMTP_FROM', 'xxxx#yyy.zzz' );
define( 'SMTP_NAME', 'abc' );
define( 'SMTP_PORT', '587' );
define( 'SMTP_SECURE', 'tls' );
define( 'SMTP_AUTH', true );
define( 'SMTP_DEBUG', 0 );
This is the config I used for both.

Related

How to debug in WooCommerce 3+

I am creating a custom shipping method for Woocommerce using this tutorial https://docs.woocommerce.com/document/shipping-method-api/ but I am having issues debugging. Whenever shipping methods get updated by user, Woocommerce calls calculate shipping. I have overridden this function with the following.
public function calculate_shipping( $package ) {
// This is where you'll add your rates
$rate = array(
'idea' => $this->id,
'label' => $this->title,
'cost' => '90.00',
'calc_tax' => 'per_item'
);
echo "<script>console.log('Calculating shipping');</script>";
$this->add_rate($rate);
}
In the end I have a fairly complex way of calculating the "cost" but I have no way of debugging it because that echo line produces no output in the chrome console. Any ideas what is going on here?
Any help would be much appreciated. Thank you.
As this is a background process on server side, don't use javascript.
1). WC Logs and the WC_Logger Class in WooCommerce for better debugging
To access the results of the log easily from the dashboard, you can log to a WC logger rather than the error log.
You can access error logs by going to WooCommerce > System Status > Logs.
Then you will be able to choose and "view"the error log file you need, giving you the debugging details that you need. Error logs are also located in the /wc-logs folder within your site install.
Running a stack trace on a caught exception (example):
// Log any exceptions to a WC logger
$log = new WC_Logger();
$log_entry = print_r( $e, true );
$log_entry .= 'Exception Trace: ' . print_r( $e->getTraceAsString(), true );
$log->log( 'new-woocommerce-log-name', $log_entry );
Notes:
WC_Logger methods have been updated since WooCommerce 3: So logging can be grouped by context and severity.
Use WC_Logger log() method instead of add() method due to upcoming deprecation (thanks to #Vizz85).
For example:
$logger = wc_get_logger();
$logger->debug( 'debug message', array( 'source' => 'my-extension' ) );
Related:
Develop WooCommerce blog (january 2017): Improved logging in WooCommerce 3
Documentation on the WC_Logger available methods
2). Debugging with WordPress WP_DEBUG Log (as an alternative)
a) First edit your wp-config.php file adding the following lines to enable debug (if these are already defined, edit the values):
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
As errors are logged, they should appear in wp-content/debug.log. You can open this file in a text editor.
b) On your code: Use the following (where $variable is the variable to be displayed in the error log:
error_log( print_r( $variable, true ) );
Now you will get the data for debugging.
First Creat PHP helper function
function console_output($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
Then you can use it like this:
public function calculate_shipping( $package ) {
// This is where you'll add your rates
$rate = array(
'idea' => $this->id,
'label' => $this->title,
'cost' => '90.00',
'calc_tax' => 'per_item'
);
console_output("Calculating shipping");
$this->add_rate($rate);
}
This will create an output like this:
Debug Objects: Calculating shipping

Upload Files From Wordpress Gravity Forms to Amazon S3

I have a form set up on my Wordpress site using Gravity Forms. I have found a script to upload the files to my Amazon S3 server.
Unfortunately I keep getting the following error:
The authorization mechanism you have provided is not supported. Please
use AWS4-HMAC-SHA256.
I've looked online and I can see that it needs updating to AWS new authentication methods. I'm not sure how to do this though. Is anyone able to offer me any assistance?
I've included my functions script and a link to the S3.php script i'm using to authenticate.
functions.php
include_once 'inc/S3.php';
//* AWS access info
define( 'awsAccessKey', 'MY ACCESS KEY' );
define( 'awsSecretKey', 'MY SECRET KEY' );
define( 'GFS3_BUCKET', 'MY BUCKET' );
//* Form constants
define( 'FORM_ID', 4 );
define( 'FILE_UPLOAD_FIELD_ID', 1 );
//* Upload the file after form is submitted (Product Edit)
add_action( 'gform_after_submission_' . FORM_ID, 'gf_submit_to_s3', 10, 2 );
function gf_submit_to_s3( $entry, $form ) {
// Bail if there is no file uploaded to the form
if ( empty( $entry[FILE_UPLOAD_FIELD_ID] ) )
return;
// Instantiate the S3 class
$s3 = new S3( awsAccessKey, awsSecretKey );
// Get the URL of the uploaded file
$file_url = $entry[FILE_UPLOAD_FIELD_ID];
// Retreive post variables
$file_name = $_FILES['input_' . FILE_UPLOAD_FIELD_ID]['name'];
/**
* File Permissions
*
* ACL_PRIVATE
* ACL_PUBLIC_READ
* ACL_PUBLIC_READ_WRITE
* ACL_AUTHENTICATED_READ
*/
// Create a new bucket if it does not exist (happens only once)
$s3->putBucket( GFS3_BUCKET, S3::ACL_AUTHENTICATED_READ );
// Parse the URL of the uploaded file
$url_parts = parse_url( $file_url );
// Full path to the file
$full_path = $_SERVER['DOCUMENT_ROOT'] . $url_parts['path'];
// Add the file to S3
$s3->putObjectFile( $full_path, GFS3_BUCKET, $file_name, S3::ACL_AUTHENTICATED_READ );
// Confirmation/Error
if ( $s3->putObjectFile( $full_path, GFS3_BUCKET, $file_name, S3::ACL_AUTHENTICATED_READ ) ) {
printf( 'Your file <strong>%1$s</strong> was successfully uploaded.', $file_name );
} else {
wp_die( __( 'It looks like something went wrong while uploading your file. Please try again. If you continue to experience this problem, please contact the site administrator.' ) );
}
}
S3.php
* #link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
* #version 0.5.0-dev

jQuery AJAX on Joomla! works on local but gives error 500 on the server

I've developed a statistics for a Joomla! component which has 3 AJAX calls. The whole app works on local perfect but when I test it on a live server, the AJAX files don't work at all. They return 500 while calling via component or directly.
My local app: Wamp Server
The Server: DirectAdmin
Live AJAX URLs:
http://spaline.pc-games.co.il/administrator/components/com_product/views/reports/funnel.ajax.php?callback=5
and
http://spaline.pc-games.co.il/administrator/components/com_product/views/reports/callback.php?callback=cart-filling&date=5
and
http://spaline.pc-games.co.il/administrator/components/com_product/views/reports/filters.php?box_id=5&filter=grey
In addition,
Regarding the paths of the AJAX files, I've defined them this way:
define( '_JEXEC', 1 );
// defining the base path.
if (stristr( $_SERVER['SERVER_SOFTWARE'], 'win32' )) {
define( 'JPATH_BASE', realpath(dirname(__FILE__).'..\..\..\..\..\..\' ));
} else define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../../../../../' ));
define( 'DS', DIRECTORY_SEPARATOR );
// including the main joomla files
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php' );
// Creating an app instance
$app = JFactory::getApplication('site');
$app->initialise();
jimport( 'joomla.user.user' );
jimport( 'joomla.user.helper' );
...
I checked the paths and there is no 404 error. The problem is with my AJAX files. I've no idea if the way I've defined the Joomla! header is making this problem or the problem is with the AJAX functions itself or so.
The problem was with the files paths. I figured it out this way:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../../../..' ));
// including the main joomla files
require_once ( JPATH_BASE . '/includes/defines.php' );
require_once ( JPATH_BASE . '/includes/framework.php' );
require_once ( JPATH_BASE . '/configuration.php' );
// Creating an app instance
$app = JFactory::getApplication('site');
$app->initialise();
jimport( 'joomla.user.user' );
jimport( 'joomla.user.helper' );
...

LimeSurvey Rc2 api function export_responses() give error while exporting PDF

I am trying to export response in PDF format using export_responses(). This function works perfectly when I export response either in CSV or DOC format, but it always gives error in PDF.
require_once 'src/org/jsonrpcphp/jsonRPCClient.php';
// with composer support just add the autoloader
// include_once 'vendor/autoload.php';
define( 'LS_BASEURL', 'http://localhost/limesurvey/'); // adjust this one to your actual LimeSurvey URL
define( 'LS_USER', 'admin' );
define( 'LS_PASSWORD', 'admin' );
// the survey to process
$survey_id=464658;
// instanciate a new client
$myJSONRPCClient = new org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );
// receive session key
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
$responses = $myJSONRPCClient->export_responses( $sessionKey, $survey_id, 'pdf', 'en','','','',11,11);
It always gives this error

How to interact with the Wordpress JSON API using PhP?

I'm trying to create a PhP script that will automatically post an article on my wordpress website (I'm using the wordpress CMS, it's not a wordpress.com website).
Here is what I've done so far:
Plugin installations
I've installed and activated the WordPress JSON API and the Basic Auth plugins on my website.
Library and code
I've downloaded the WP REST php Library and I've uploaded the files in src in the same folder as my script.
Here is the code of my php script:
require_once( 'class-wp-rest-client.php' );
require_once( 'class-wp-rest-request.php' );
require_once( 'class-wp-rest-exception.php' );
require_once( 'class-wp-rest-transport.php' );
require_once( 'class-wp-rest-transport-curl.php' );
require_once( 'class-wp-rest-transport-wp-http-api.php' );
require_once( 'class-wp-rest-object.php' );
require_once( 'class-wpapi-rest-object-post.php' );
require_once( 'class-wpapi-rest-object-posts.php' );
require_once( 'class-wpapi-rest-client.php' );
use WP_REST_Client;
use WP_REST_Request;
use WP_REST_Object;
use WP_REST_Exception;
use WP_REST_Transport;
use WP_REST_Transport_WP_HTTP_API;
use WP_REST_Transport_Curl;
use WPAPI_REST_Object_Post;
use WPAPI_REST_Object_Posts;
use WPAPI_REST_Basic_Auth_Client;
$post_data=array(
'title' => 'New Post',
'content_raw' => 'This is a test'
);
$wp_api_client = new WPAPI_REST_Basic_Auth_Client( 'http://example.com', 'login', 'password' );
if ($wp_api_client) echo "connected";
try {
$current_post = WPAPI_REST_Object_Post::initWithId( 1, $wp_api_client );
$current_post_data = $current_post->get();
echo 'Post Title:' . $current_post_data->ID;
} catch ( WP_REST_Exception $e ) { print_r($e); }
?>
And I'm getting this error:
WP_REST_Exception Object ( [message:protected] => HTTP error for request; response:
I'm not sure if the error is coming from me not using the library properly or else. Anyone can help me with this?
Thanks a lot guys!
For those having the same issue, it seems that it is the Basic Auth plugin that is not working.
For a better working API, connect your site with JetPack to Wordpress.com and use their API.
Cheers,
Arthur

Categories