Drupal Autoresponder with Token Module - php

I'm using Drupal autoresponder module - and I want to use tokens so I can include the username who has subscribed within the emails being sent...
Does anybody know how this can be achieved?
Thanks for any help.
Shane

Thanks for that - your answer was very close....
The autoresponder module UID is not related to the user UID so your code was bringing in a different username... So I changed it to find the user from the email address instead.
// load the full user object
$user = user_load(array('mail' => $u->mail));
// Replace user tokens in mail body
$mail->body = token_replace($mail->body, 'user', $user);
Yes indeed, I'll submit it as a patch to my other request, and hopefully it may help somebody else.
Many Thanks
Shane

EDIT after separate answer by OP: The following was based on the assumption that the $u->uid refers to the 'standard' Drupal user id, which is not correct! So one has to find the corresponding Drupal user by other means, if possible - see the OPs answer for details on this...
I have not tested it, but looking at the autoresponder source code, you should be able to add (user) token replacement in the autoresponder_mail() function by inserting the following code before the preparation of the plain text body (before line 392 in the 6.x-1.0-alpha1 release):
// load the full user object
$user = user_load($u->uid);
// Replace user tokens in mail body
$mail->body = token_replace($mail->body, 'user', $user);
Alternatively, you could do it one function call earlier in autoresponder_cron(), within the while ($message db_fetch_object($result_messages)) loop, before the if (autoresponder_mail($u, $message)) call (line 366), using $message instead of $mail:
// load the full user object
$user = user_load($u->uid);
// Replace user tokens in mail body
$message->body = token_replace($message->body, 'user', $user);
In case this works, you might want to submit it as a patch to the already existing feature request for this. (I guess you are the 'keyzo'/'shane' who already answered there ;)
If it works and you are going to create a patch, it would be 'standard' practice to add the hint on possible token replacement to the message definition form(s) in autoresponder_mail_edit_create_form().

Related

How to get the ID of the current Contact with PHP Mautic API?

As I couldn't find anything in the documentation I might have a general understanding issue. But what I want to achieve is getting the ID of the current Contact browsing the Site. To get user details I always have to know the ID, like documented here: https://developer.mautic.org/#contacts
$api = new MauticApi();
$contactApi = $api->newApi("contacts", $auth, $settings['baseUrl']);
$contact = $contactApi->get(3);
Is there a way to the ID of the current Contact? I want to play highly customized content on the website and an entry point to get user details.
It's a REST API, so you should get a response in any case. What's the content of the response (eg. what contains $contact)? What's the HTTP code of the response? Error messages?
However... I'm not familiar with this particular API, but the principles are always the same
https://developer.mautic.org/?php#list-contacts
Without any query parameters it should give the full list of contacts. Each of the contact records should come with an ID for the details:
https://developer.mautic.org/?php#get-contact
As said, I'm not familiar with the API, so it could be that you get a contact list only from a higher entity, like a Company or so.
I can recommend Postman (https://www.postman.com/), a neat tool to make REST API requests while having full control over all the details.
Assuming that you're trying to get the details of the current logged in user.
You can use themautic.helper.user helper to do that as shown below:
<?php
$user = $this->get('mautic.helper.user')->getUser();
$firstName = $user->getFirstname();
$lastName = $user->getLastname();
$email = $user->getEmail();
$profile = $user->getProfile();
$role = $user->getRole()->getName();
if ($role->isAdmin()) {
// do something
}
Ref: https://developer.mautic.org/#services44
In order to do what you want here I do not think you actually need the API to get the user id. Mautic has dynamic content and as long as the user has a cookie dropped on his browser you can use Dynamic Content to say "Hi {contactfield=firstname}, welcome back.
You can actually call any custom field into the dynamic content slot.
Check out dynamic content which is found under the Components section in the left menu

How to store and retrieve string mix with PHP variable name from MySQL database?

I have a question and try searching on Google and Stakoverflow and could't find any perfect solution. I am using phpmailer and want to let the user to customize it's body message. So I have given and option to edit the email body text by providing a texarea input box in back end where user will input the email text with some my own provided php variable.
For example body text is "Dear {$username}, thank you for signup.". Once I store this input into database and want to retrieve this in php:
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
echo $message;
OUTPUT DATA: "Dear {$username}, thank you for signup."
I want to get it like this "Dear ABC, thank you for signup.". How is it possible or is there any other option I can use it with php varialbe or defined type?
You're not getting an answer you're expecting because contents of $row["message"] don't get evaluated again. And that's actually a good thing because users could use any variable name they want, which would create a security threat.
There is a quality solution, though. You should check out template engines for PHP, e.g. Smarty and Twig. They allow you to create templates with placeholders, for example something like:
Dear {{username}}, thank you for sign-up!
which you could store in a file or let your users define their own templates. Template engine will then read the template and, using the values you send it, replace those placeholders with actual values.
To see how this is done in practice, choose a template engine and check its documentation.
I have found a solution in PHP with str_replace function. This function is really very helpful. I have tried with modified input message "Dear user_name, thank you for signup.":
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
$message_new = str_replace("user_name", $username, $message);
echo $message_new;
OUTPUT DATA: "Dear ABC, thank you for signup."
You can use PHP eval() function.
$msg = "$cust_dt->cus_name, Your order placed successfully. ID: $sms_oid Total: $sms_ord_tot";
echo $msg;//output1
eval("\$msg= \"$msg\";");
echo $msg;//output2
Output 1: $cust_dt->cus_name, Your order placed successfully. ID:
$sms_oid Total: $sms_ord_tot
Output 2: John Doe, Your order placed successfully. ID: 454554545
Total: 1600
You can store a strings like $msg in a database and use eval() function to evaluate string into php code.
For More:
https://www.w3schools.com/php/func_misc_eval.asp
https://www.php.net/manual/en/function.eval.php
Just wanted to say THANK YOU!!!!
This also works by putting ((name)), and using that inside str_replace - instead of just using "user_name"
Which is a little more user-friendly - and keeps from accidentally replacing things if someone were to type "user_name", for instance lol
Plus it'll make it look cool when having your "insert" legend at the top saying:
Choose one of the following:
((name)) = Clients Name
You saved me so much time!

What is the difference between Kohana::message and Kohana::config?

I Learning a framework Kohana.
What is difference between Kohana::message and Kohana::config?
They perform the same function.
Maybe if there is a difference between the concept?
Kohana:message
These are generally used to store messages that will be displayed to the user. For example if you have a method that tries to create a user and it fails, you can get the relevant you can you may have the following in a User Controller:
$create = $this->create_user($user);
if($create)
{
// user created
$message = Kohana::message('user', 'create_success');
}
else
{
// failed to create user
$message = Kohana::message('user', 'create_error');
}
Kohana:config
This is used for configuration information such as the hash_method used in auth module and you can access it using Kohana::$config->load('auth.hash_method')
One is for configuration information. The other is for reusable text: Kohana::message('registration.error.email') could say something like "There is already an account using the email address you entered, an email with instruction on how to reset you password has been sent in case you forgot it.".

"Issue viewing email? View email online?" unique link in Codeigniter email

I'm not sure where to begin with this task, so I'm looking for an answer on just the idea of how to go about doing this.
When a new user creates an account on my Codeigniter site I send him/her an email about signing up (very typical). Here is how I'm sending the email...
...
$subject = 'Welcome to __________, ' . $firstName . '!';
$emailData = array(
'name' => $name,
'blah' => $blah,
'blah' => $blah,
// etc.
);
$html_email = $this->load->view('emails/signup_html_view', $emailData, true);
$text_email = $this->load->view('emails/signup_text_view', $emailData, true);
$this->email->from('team#_________.com', '________ Team');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($html_email);
$this->email->set_alt_message($text_email);
$this->email->send();
...
As you can see, I'm passing data to those views to send the email. In the email view, at the top, I have a link that says "Problem viewing email? Click here to view it online.". That is common practice for emails on newsletters, signups emails, etc. so that the user can view the email online if it renders weird or something goes wrong.
Where I'm getting lost is how do I generate that unique "...view it online." link so that when the user clicks it, they see an online version of the email and the online version still has all the data still passed to it? Do I need to create a separate controller or what is the best way to handle that? How do I generate that unique link?
#zach,
yes, what you do is:
1) create a separate controller that can display this email, just like you were making a page
2) I'm assuming the user is already created in the db, but is not yet activated or taken steps to be able to login. So, backing up a bit, when you make your user record, also create a random hashtag & store that.
Now, in the email link, set it to www.mysite.com/welcome/hashtag
This way you are allowing them to get a unique record without using an id that they could just use to go look at everyone else
This welcome page, of course, doesn't require them to be logged in. Probably you give them a submit (maybe after they fill out some more info) that will then activate their account
Hope that was close enough to what you were asking to get you through

formmail to email in database

Here's what I need to setup...and I am not well versed in PHP/SQL...but this is what I'm trying to do.
On the New User database, I will have a section where they can have information sent to their phone using the provider's default e-mail to text msg (i.e. 2225551212#txt.att.net.)
New User:
Input number: ______________(2225551212 format, no hyphens, etc.)
Select Provider: (Drop-down menu with proper #provider.ext...)
Then the formmail, when sent, if for specific user will get ($phone".#."$provider); or something like that and send a preset message like:
$user."requested information on ".$product."on".$date." at ".$time.".";
Is this possible?
The $user, $product, $date, $time all are generated directly from the most recent input page for a database.
Is this possible?
Please help!
Assuming your question is "can I send a text message from PHP", then yes; if the server where you are running PHP is configured to send email, then within PHP you can try:
mail('2225551212#txt.att.net', 'New User', $user."requested information on ".$product."on".$date." at ".$time.".");
For more help with the mail() function, check out http://php.net/mail .

Categories