PHPmailer parseAddresses - How to get rid of "notice" messages - phpmailer

I'm trying to get rid of the following message in one of my scripts when using the PHPmailer parseAddresses function:
Notice: Unknown: Must use comma to separate addresses: xxx (errflg=3) in Unknown on line 0
$mailer = new PHPMailer(true);
try {
$a = $mailer->parseAddresses('aaa#aaa.aaa xxx');
}
finally {
...
}
I'm using PHP 7.0.8 with the following error handling presets:
declare(strict_types = 1);
error_reporting(E_ALL);
ini_set('display_errors', 'stdout');
I know that I can just stop the errors from being displayed but this doesn't seem to be the proper way to do. And of course I know that the provided email addresses in my example are not correct...

I'm not sure what you're complaining about: it's telling you you have malformed input when you provide malformed input! The way to avoid the error is not to pass in malformed input!
As the error says, it's expecting one or more addresses in comma-delimited RFC822 format (not what you provided), which might be something like:
xxx <aaa#aaa.aaa>, yyy <bbb#aaa.aaa>
If you don't provide data in that format, it will complain, as you're seeing. This is covered in the docs on the parseAddress method.
Are you expecting it to do something else?

PHPMailer writes notices to output, so you could start an output buffer and just flush it after your call. Something like:
$mailer = new PHPMailer(true);
try {
ob_start();
$a = $mailer->parseAddresses('aaa#aaa.aaa xxx');
//$notices = ob_get_contents();
ob_end_clean();
}
finally {
...
}

I had to deal with the same issues. Simply created a homemade solution that does mostly the same thing in a more flexible way. For anyone that is interested:
/**
* split_addresses Split a list of email addresses with potential validation
* Similar concept as PHPMailer->parseAddresses, but more forgiving
* #param $list - a list of comma delimited email addresses in simple or RFC822 format
* #param $default_name an optional name to add if not present
* #param $validate logical var to indicate to only accept validated emails
* #author Jacques Amar
* #copyright 2019 Amar Micro Inc.
*/
function split_addresses($list, $default_name='', $validate=true) {
$valid_arr = [];
if (empty($list)) { return $valid_arr; }
$split_arr = preg_split('/\s*,\s*/', $list);
foreach ($split_arr as $email_candidate) {
// Validate email_candidate if the form "first last <adrs#email.com>"
$actual_name = $default_name;
$actual_email = $email_candidate;
if (preg_match('/\s*(.+)\s*\<\s*([^#]+#.+)\s*\>/', $email_candidate, $actual_emails) == 1) {
$actual_name = $actual_emails[1];
$actual_email = $actual_emails[2];
}
if ($validate) {
if (filter_var($actual_email, FILTER_VALIDATE_EMAIL)) {
$valid_arr[] = ['address' => $actual_email, 'name' => $actual_name];
}
} else {
$valid_arr[] = ['address' => $actual_email, 'name' => $actual_name];
}
}
return $valid_arr;
}
Should be self explanatory

Related

Passing a JSON array to fulfillmentText

I have an array of JSON objects that looks like this...
JSONarray =
[ { name: 'Steven' },
{ name: 'Clark' },
{ name: 'Kensington' } ]
With respect to dialogflow and google assistant I would need to send a webhook response that looks something like this...
"fulfillmentText": JSONarray}),
"fulfillmentMessages" : [
{
"text":
{"text": [JSONarray]
}
}
]}
The issue I have is this piece "fulfillmentText": JSONarray. 'fulfillmentText' is the part that Google Assistant reads back and ideally I want to have it read back each of the three names but if I just pass the array it fails. Is there away I can build the JSON that will let this happen?
First, a minor semantic point. That is not a "JSON Array". It is an array of JavaScript Objects. JSON refers to the string representation of those objects (JavaScript Object Notation).
This is important because "fulfillmentText" expects a string. You can turn JavaScript objects into a string by using the JSON.stringify() method, so you'd do something like
const text = JSON.stringify( JSONarray );
However, this probably isn't what you want, since the Assistant would read this out as something like
Open bracket open brace quote name quote colon quote Steven quote close bracket, ...
and so forth.
Instead, you probably should write a function that takes the array and turns it into a string. Simplistically, you could call join() on just the values of the names. Something like this:
const names = JSONarray.map( o => o.name ); // Gets just an array of the names
const namesString = names.join(', '); // Joins them into a single string, separated by commas
const text = `The names are: ${namesString}`;
This is better, and what would be read out is something like
The names are Steven, Clark, Kensington
But this isn't how people actually read (or listen to) names. We expect it to be something more like
The names are Steven, Clark, and Kensington
This is so common that multivocal includes a template function to do this based on a list of strings. So your template would just be something like:
"The names are: {{Oxford names}}"
If you're not using multivocal, something like this would work:
/**
* Similar to Array.join(), but using rules for the Oxford comma.
* #param values The array to return joined values for
* #param sep1 The normal separator for each element (defaults to ", ")
* #param sep2 The separator before the final element (defaults to "and ")
* #returns {*}
*/
function oxford( values, sep1=", ", sep2="and " ){
if( !Array.isArray( values ) ){
return values;
} else if( values.length === 0 ){
return '';
} else if( values.length === 1 ){
return values[0];
} else if( values.length === 2 ){
return values[0]+' '+sep2+values[1];
}
var ret = '';
for( var co=0; co<values.length-1; co++ ){
ret += values[co]+sep1;
}
ret += sep2;
ret += values[values.length-1];
return ret;
};
const namesString = oxford( names );
const text = `The names are: ${namesString}`;

node.js - Error: SQLITE_ERROR: near "users": syntax error

I'm trying to put a JSON object "Synced" (Which you will see in the code)
This is the code for a function "addServer(userid, serverid)"
The function is being required from another javascript file
db.all(`SELECT * FROM Users WHERE Tag = ? LIMIT 1`, userid, async(error,element) => {
if(element[0].Synced === '') {
var sJSON = {
users:{
[userid]:4,
},
servers:[`${serverid}`]
}
var serverJSON = JSON.stringify(sJSON)
console.log(serverJSON)
} else {
//Else statement not done yet
}
db.run(`UPDATE Users SET Synced = "${serverJSON}" WHERE Tag = "${userid}"`)
})
Solved. Needed to change quoting.
As Dave Newton said, I had to check my quoting. What I did was change my double quotes to single quotes which solved the problem.

Regex Matching Description part of a JS Doc string

I am trying to match the description part (line without a # symbol) of JS doc string. Here are a couple of exmaples of doc strings.
/**
* This is a doc string
* #param withArg description of withArg
* #param withArg2 description of withArg2
*/
/**
* This is an other doc string
* This is second line of description.
*/
This seems to work in regex editor:
/\*\S*(.*?)(#)/
see: https://regexr.com/4dfbn
But in javascript:
https://jsbin.com/qekisogula/1/edit?html,js,console
Any ideas on what is going on ?
Thanks a lot
UPDATE:
Expected output
Example 1:
This is an other doc string
Example 2:
This is an other doc string, This is second line of description.
I would actually take the approach of reading the file line by line and using some rudimentary parsing logic:
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
var start = false;
lineReader.on('line', function (line) {
if (line.startsWith("/**") {
start = true;
}
else if (line.startsWith("*/")) {
start = false;
}
else if (start && !line.includes("#param")) {
console.log("doc string: ", line);
}
});
The logic here is that we use a boolean flag start to keep track of whether we are inside a comment with doc strings. Upon hitting /** the flag is turned on, and upon hitting */ the flag is turned off. If we encounter a line which does not contain #param, then we echo it to the console.

What's the "official" way to pass variables in SS2.0?

Run into a kind of silly problem.
I want to pass a variable between stages on a map/reduce script. Is it there an "official" or best way to do this (rather than sending it with the returned results).
This is my last approach:
/**
* #NApiVersion 2.0
* #NScriptType MapReduceScript
*/
define(["N/search", "N/record", "N/email", "N/runtime", "N/task", "/SuiteScripts/Libraries/tools_lib"],
function (search, record, email, runtime, task, tools) {
var ss = runtime.getCurrentSession();
var conf = {};
/**
* Retrieve the CSV contents and return as an object
* #return {Array|*}
*/
function getInputData() {
log.debug("setting", "foo");
ss.set({name: "foo", value: "bar"});
//Session
var foo = ss.get({name: "foo"});
log.debug("foo 1", foo);
//var pass
conf["foo"] = "bar";
return [1, 2, 3];
}
/**
* Search and group by type, all records with matching entries on the CSV field
* #param context
* #return {boolean}
*/
function map(context) {
//Session
var foo = ss.get({name: "foo"});
log.debug("foo 2", foo);
//Var pass
log.debug("foo 3", conf["foo"]);
return false;
}
foo 1 = bar
foo2 = null
foo3 = null
NetSuite stores whatever you return from the previous phase in context.value.
No matter what data type you return, it will always get sent to the next phase as a String, so you need to JSON.parse it if you want to work with a different data type.
function getInputData() {
return [1,2,3];
}
function map(context) {
log.debug(JSON.parse(context.value));
}
You cannot get access to specific variables from previous phases. If you want to pass data along, you need to build an appropriate data structure with the values you want to pass and return it, then parse it out of context.value in the subsequent phase.
This is coming a little late, but one solution/workaround I found is using a Scheduled Script to call a Map/Reduce Script.
You can dynamically set script parameters within the Scheduled Script when you initiate the map/reduce script (using the 'N/task' module).

How to render block programmatically with standard theme

I know how to get block data by module_invoke(),
but how to use standard block theme for rendering it.
I tried to use theme() function but with no success.
Could somebody give me advice?
Regards
Taken from the API comments for theme_block
// setup vars
$module = 'system';
$delta = 0; // could also be a string
// renders the "Powered by Drupal" block
// #see hook_block()
// #see module_invoke()
$block = module_invoke($module, 'block', 'view', $delta);
// must be converted to an object
$block = !empty($block) ? (object)$block : new stdclass;
$block->module = $module;
$block->delta = $delta;
$block->region = 'whateverYouWant';
echo theme('block',$block);
Haven't tested it but it seems to be doing what you want. This uses the regular theme function to theme the block you are retrieving

Resources