Drupal email variables not appearing - php

Whenever a user registers an account, the automated emails (which I can set in admin/config/people/accounts) are not carrying over the defined variables for username, email, etc.
For example. Here's what I have set for one email:
[user:name],
Thank you for registering at [site:name]. Your application for an account is currently pending approval. Once it has been approved, you will receive another e-mail containing information about how to log in, set your password, and other details.
-- [site:name] team
Those variables in brackets just aren't appearing. The email sends as usual, but where those variables are, nothing gets inserted. Is there something I have to enable first?

The token replacement in mails happen in line 2815 of user.module (modules/user/user.module). You can add the following line of code
drupal_set_message("Text: $text, Variables: " . print_r($variables, TRUE) . ", Language: $language);
before the line that says
return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
That will tell you as messages what is happening with the token replacement issue. If you can post the messages that you see back here I can help solve the issue.

It seems that you have misconfiguration issues or losing sth .for seeing configs with html mail in here ,
see here
also try updating it using update.php!

-- READ FROM PHP.NET SPECIFICATIONS --,
I THINK THAT BY DEFAULT, SOME HOST PROVIDERS DO NOT SUPPORT FEW PHP FILTERS (OR LIKEWISE), SO (ITS JUST AN ASSUMPTION !) MAY BE, FILTER_SANITIZE_EMAIL THAT IS USED TO REMOVE ALL CHARACTERS EXCEPT LETTERS, DIGITS AND !#$%&'*+-/=?^_`{|}~#.[], IS TURNED ON.
SO JUST CHECK IT OUT !

Related

Google PHP API - How to update gmail signature "on reply/forward"?

I have created a method to update all email signatures in my company.
It works, the signature is automatically set on new emails but for replies/forwards there is no signature.
Is it possible to update the signature for reply/forward?
My code:
public function updateSignature(User $user, string $email, string $newSignature): SendAs
{
// Executes the request with the address of its own user
$this->client->setSubject($user->getPrimaryEmail());
$serviceSendAs = new SendAs();
$serviceSendAs->setDisplayName($user->getFullName());
$serviceSendAs->setSignature($newSignature);
$serviceGmail = new Gmail($this->client);
return $serviceGmail->users_settings_sendAs->update($user->getPrimaryEmail(), $email, $serviceSendAs);
}
Thank ;)
I was running into the same issue and just figured out what's going on. For some Gmail accounts, the signature was updating and assigning both the "Compose" and "Reply/Forward" settings correctly, but for others, it was creating a new signature and only updating the "Compose" setting.
What I found was if I deleted all saved signatures for the accounts that were not updating the "Reply/Forward" setting and then ran the script, the signatures got created and both the "Compose" and "Reply/Forward" settings were set. Subsequent runs of the script also reflected correctly.
This is likely a bug since the API documentation explicitly states it will only update the "Compose" setting, but a favorable bug for sure.
Hope this helps!

wp_new_user_notification not triggered

I am testing a simple function, to get the wp_new_user_notification email to send, but the standard ordpress function is not triggering.
I'm not trying to change the function, I just want to trigger it.
Any ideas of what I'm doing wrong?
My code:
function test_new_user_email() {
wp_new_user_notification ( '2', null, 'both' );
}
...which is as per:
https://developer.wordpress.org/reference/functions/wp_new_user_notification/
In this case, I'm testing the sending of the email to an existing User ID = 2
As far as I can tell, it doesn't look like there are any dependencies on this standard function, unless I'm missing something here.
Here's a modified version of the core file for "Import Users from CSV": import-users-from-csv.php.
Note that it's not a full upgrade; I modified it just to make sure it's warnings-free (i.e. it passes WP_DEBUG "on" mode); and secondly, I've tested the "Send to new users" option (which uses the wp_new_user_notification() function), and it worked well.
In line 354, you can, if you want, set the 3rd argument to both:
if ( $new_user_notification )
wp_new_user_notification( $user_id, null, 'user' );
Try that new file — download and upload it to wp-content/plugins/import-users-from-csv/ — and just let me know if you need further assistance.

How can I allow email addresses from two different domains using Regex?

I've got an small form to download some stuff. But I don't want every buddy to download it. Only from 2 specific domains you can download the stuff. So I made an form with an email validation. But How can I add two Domains in the test of the email?
What I was trying was:
$test = array(
'email' => '/^[\w.+-]{2,}\#[DOMAIN1][DOMAIN2]\.[a-z]{2,6}$/',
}
But that didn't worked …
Thanks for any help.
By grouping the evaluation () and placing an or operator in it | you'll be able to handle either domain. See the below modified regex:
'email' => '/^[\w.+-]{2,}\#(DOMAIN1|DOMAIN2)\.[a-z]{2,6}$/',

PHP LDAP to change Active Directory samAccountName?

Our policy for people who are terminated/separated or who go on leave of absence involves a handful of changes to their AD account for record keeping purposes and security. One of these changes is renaming the account (login name, display name and dn) to a value that includes the original name with the help desk ticket number appended.
I have been able to use ldap_rename() to change the active directory "name" attribute, thus changing the DN. I can change the displayName attribute using either ldap_modify() or ldap_mod_replace(). What I cannot seem to do is change the samAccountName using any of these. Below is the core of the code I'm using. The errors I get are dependent upon which function I use, and are listed below.
I know there are some nuances to using PHP LDAP with Active Directory, but I find it hard to believe that I have been able to do everything up to and including changing passwords and I can't change the samAccountName... help?
<?php
$connection=ldap_connect(domain.local,389);
ldap_set_option($connection,LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($connection,LDAP_OPT_REFERRALS,0);
ldap_start_tls($connection);
ldap_bind($connection,$username,$password);
$accountName=$_POST["accountName"];
$ticketNumber=$_POST["ticketNumber"];
$baseDn="dc=domain,dc=local";
$attribs=array("samaccountname","dn","name","displayname","description","info","memberof");
$search=ldap_search($connection,$baseDn,"(samaccountname=".$accountName.")",$attribs);
$result=ldap_get_entries($connection,$search);
// ldap_modify returns error 80: Internal (implementation specific) error.
foreach ($result as $account) {
$newValues=array("samaccountname"=>$account["samaccountname"][0]."-".$ticketNumber)
ldap_modify($connection,$account["dn"],$newValues);
}
// ldap_mod_replace returns error 80: Internal (implementation specific) error.)
foreach ($result as $account) {
$newValues=array("samaccountname"=>$account["samaccountname"][0]."-".$ticketNumber)
ldap_mod_replace($connection,$account["dn"],$newValues);
}
?>
So yeah, what is it I'm supposed to be doing to make this happen?
The "implementation specific" error message you're receiving means that your sAMAccountName is invalid because it doesn't meet specific AD restrictions on it. The sAMAccountName attribute cannot be more than 20 characters and cannot contain any of the following: " [ ] : ; | = + * ? < > / \ ,. It might be helpful to see an example username with the ticket number appended.

How to log error message in drupal

How to log our own error messages(for ex: error due to invalid user date entry) which is generated in php program to drupal error log.
You can use the watchdog function :
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
Quoting the manual, the parameters are :
$type The category to which this message belongs.
$message The message to store in the log.
$variables Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
$severity The severity of the message, as per RFC 3164
$link A link to associate with the message.
And the error levels can be found on the page of watchdog_severity_levels. For an error, you'll most probably use WATCHDOG_ERROR, or maybe even something more "critical", depending on the kind of error.
Drupal 8+
// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);
See more examples at How to Log Messages in Drupal 8.
1) Indeed, watchdog is a standard way to record own PHP errors.
2) Alternatively, if you need to immediately see error messages while debugging your Drupal pages, you may want to see them logged/printed right at the related page - in FireBug console.
Sometimes is this very convenient when you can see page-related just-in-time logs.
This requires - Devel module, Firebug extension to FireFox and possibly Firephp.
You can use the dfb() function to write log messages directly to the general Firebug console.
dfb($input, $label = NULL)
If you want to keep your Drupal-related log messages out of the normal Firebug console, you can write messages to the Drupal for Firebug log with the firep() function:
firep($item, $optional_title)
Watchdog is the way to go for a production system no doubt but during debugging I find the drupal_set_message function useful.
It outputs the message to the screen where the 'Operation Successful'-type messages are normally displayed (so make sure you remove them before making the site Live).
http://api.drupal.org/api/function/drupal_set_message/6
In drupal 7 we can log message by following method:
drupal watchdog function we can use to log message in database , make sure we have enabled optional core module for Database Logging at /admin/build/modules.
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
$type:
The category to which this message belongs , Example: PHP,cron.., we can filter message by type.
$message :
The message to store in the log,Example: 'The following module is missing from the file system: security_review'
$variables :
Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
to make message translated , do not pass dynamic value pass variables in the message should be added by using placeholder strings.
Example:
watchdog('cg_volunteer', 'cg in form_alter %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
$severity
The severity of the message,logs can be filter by severity as per RFC 3164. Possible values are WATCHDOG_ERROR, WATCHDOG_WARNING, etc.
For more example see https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/watchdog/7.x
$link:
A link to associate with the message.
Example
// for logs notices
watchdog('my_module', $message, array());
// for Loging Error
watchdog('my_module', $message, array(), WATCHDOG_ERROR);
In drupal 8 we used following method:
// For Logs a notice.
\Drupal::logger('my_module')->notice($message);
// For Logs an error.
\Drupal::logger('my_module')->error($message);
// For Alert, action must be taken immediately.
\Drupal::logger('my_module')->alert($message);
// For Critical message.
\Drupal::logger('my_module')->critical($message);
// For Debug-level messages.
\Drupal::logger('my_module')->debug($message);
//For Emergency, system is unusable.
\Drupal::logger('my_module')->emergency($message);
//For Warning
\Drupal::logger('my_module')->warning($message);
//For Informational messages.
\Drupal::logger('my_module')->info($message);
Also for translate we should not use t() function.
\Drupal::logger('my_module')->alert('Message from #module: #message.', [
'#module' => $module,
'#message' => $message,
]);
this will be translated on run time.
Example :
\Drupal::logger('content_entity_example')->notice('#type: deleted %title.',
array(
'#type' => $this->entity->bundle(),
'%title' => $this->entity->label(),
));
Both watchdog for D7 & \Drupal::logger for D8 will write log in watchdog table (in your database), and with HUGE data logged, you can imagine performance impact.
You can use error_log php function to do it (see PHP manual).
error_log("Your message", 3, "/path/to/your/log/file.log");
You need to have permission to write in your log file (/path/to/your/log/file.log)
drupal-8drupalphplog
// Get logger factory.
$logger = \Drupal::service('logger.factory');
// Log a message with dynamic variables.
$nodeType = 'Article';
$userName = 'Admin';
$logger->get($moduleName)->notice('A new "#nodeType" created by %userName.', [
'#nodeType' => $nodeType,
'%userName' => $userName,
]);
Source

Categories