Greetings,
i need to send email to several recipients that are stored in a table named mail which has a field called email.
In my controller i created an action that Query the table mail for the emails.
Later i tried to use the implode() function separated by comma, but obviously it didn't work because of mailer policies.
It generated the wrong format -> "email1#mail.com, email2#mail.com, email3#mail.com".
Tried also a for each loop and the serialize() function without success.
The json_encode() function is close to what i need, separate an array of emails to something like -> "email1#mail.com", "email2#mail.com", "email3#mail.com".
But it appends the field name before the value and it is not accepted by mailer policies.
So far i'm stuck with the following code:
public function actionSucesso()
{
$query = new Query;
$query->select('email')
->from('mail');
$command = $query->createCommand();
$enderecos = $command->queryAll();
$enviar = json_encode($enderecos);
Yii::$app->mailer->compose()
->setFrom('atf#website.com')
->setTo($enviar)
->setSubject('Oferta de jogo no site da ATF.')
->setTextBody('Aceda em: http://atf.besaba.com/index.php?r=playschedule%2Findex2')
->send();
return $this->render('sucesso');
}
I think in order for the mailer to work and send the message the correct format needs to be: ->setTo("mail1#mail.com", "mail2#mail.com", "mail3#mail.com")
Is there a way of solving this problem?
Many thanks in advance.
To get array of email with numeric indexes call queryAll() method with $fetchMode parameter \PDO::FETCH_COLUMN, and then just pass returned array to mailer's setTo() method. Like this
$enderecos = $command->queryAll(\PDO::FETCH_COLUMN);
//$enviar = json_encode($enderecos); <- this line no needed
Yii::$app->mailer->compose()
->setFrom('atf#website.com')
->setTo($enderecos) //you pass an array of email addresses
->setSubject('Oferta de jogo no site da ATF.')
->setTextBody('Aceda em: http://atf.besaba.com/index.php?r=playschedule%2Findex2')
->send();
See queryAll() documentation and list of pdo constant including available fetch modes starting with PDO::FETCH_. Also assuming you are using yii2 default mailer, look for swiftmailer documentation about how to set recipients
Related
So this is my first time coding an actual project that isn't a small coding task. I've got a bot that runs and responds to a message if it says "hello". I've read the API documentation up and down and really only have a vague understanding of it and I'm not sure how to implement it.
My question right now is how would I go about creating a command that takes informationn from a message the command is replying to (sender's name, message content) and stores it as an object. Also, what would be the best way to store that information?
I want to learn while doing this and not just have the answers handed to me ofc, but I feel very lost. Not sure where to even begin.
I tried to find tutorials on coding discord bots that would have similar functions to what I want to do, but can't find anything.
Intro :
Hi NyssaDuke !
First of all, prefer to paste your code instead of a picture. It's easier for us to take your code and try to produce what you wish.
In second, I see an "issue" in your code since you declare twice the bot. You can specify the intents when you declare your bot as bot = commands.Bot(command_prefix="!", intents=intents)
Finally, as stated by #stijndcl , it's against TOS, but I will try to answer you at my best.
filesystem
My bot needs to store data, like users ID, language, and contents relative to a game, to get contacted further. Since we can have a quite big load of requests in a small time, I prefered to use a file to store instead of a list that would disappear on crash, and file allow us to make statistics later. So I decided to use pytables that you can get via pip install pytables. It looks like a small DB in a single file. The file format is HDF5.
Let say we want to create a table containing user name and user id in a file :
import tables
class CUsers (tables.IsDescription) :
user_name = StringCol(32)
user_id = IntCol()
with tables.open_file("UsersTable.h5", mode="w") as h5file :
groupUser = h5file.create_group("/", "Users", "users entries")
tableUser = h5file.create_table(groupUser, "Users", CUsers, "users table")
We have now a file UsersTable.h5 that has an internal table located in root/Users/Users that is accepting CUsers objects, where, therefore, user_name and user_id are the columns of that table.
getting user info and storing it
Let's now code a function that will register user infos, and i'll call it register. We will get the required data from the Context that is passed with the command, and we'll store it in file.
#bot.command(name='register')
async def FuncRegister (ctx) :
with tables.open_file("UsersTable.h5", mode="a") as h5file :
tableUser = h5file.root.Users.Users
particle = tableUser.row
particle['user_name'] = str(ctx.author)
particle['user_id'] = ctx.author.id
particle.append()
tableUser.flush()
The last two lines are sending the particle, that is the active row, so that is an object CUsers, into the file.
An issue I got here is that special characters in a nickname can make the code bug. It's true for "é", "ü", etc, but also cyrillic characters. What I did to counter is to encode the user name into bytes, you can do it by :
particle['user_name'] = str(ctx.author).encode()
reading file
It is where it starts to be interesting. The HFS5 file allows you to use kind of sql statements. Something you have to take in mind is that strings in the file are UTF-8 encoded, so when you extract them, you have to call for .decode(utf-8). Let's now code a function where a user can remove its entry, based on its id :
#bot.command(name="remove")
async def FuncRemove(ctx) :
with tables.open_file("UsersTable.h5", mode="a") as h5file :
tableUser = h5file.root.Users.Users
positions = tableUser.get_where_list("(user_id == '%d')" % ctx.author.id)
nameuser = tableUser[positions[0]]['user_name'].decode('utf-8')
tableUser.remove_row(positions[0])
.get_where_list() returns a list of positions in the file, that I later address to find the right position in the table.
bot.fetch_user(id)
If possible, prefer saving ID over name, as it complicates the code with encode() and decode(), and that bots have access to a wonderful function that is fetch_user(). Let's code a last function that will get you the last entry in your table, and with the id, print the username with the fetch method :
#bot.command(name="last")
async def FuncLast(ctx) :
with tables.open_file("UsersTable.h5", mode="r") as h5file :
tableUser = h5file.root.Users.Users
lastUserIndex = len(tableUser) - 1
iduser = tableUser[lastUserIndex]['user_id']
member = await bot.fetch_user(iduser)
await ctx.send(member.display_name)
For further documentation, check the manual of discord.py, this link to context in particular.
I am using Okta as IDP and I have configured the user attribute statements and group attribute statements like this
And by providing a custom samluserdetails I am able to retrieve user attributes but not group attributes.
public class CustomSamlUserDetails implements SAMLUserDetailsService {
#Override
public Object loadUserBySAML(SAMLCredential cred) throws UsernameNotFoundException {
AppUser user = new AppUser();
user.setFirstName(cred.getAttributeAsString("firstName"));
user.setLastName(cred.getAttributeAsString("lastName"));
user.setLoginId(cred.getAttributeAsString("loginId"));
String groupname = cred.getAttributeAsString("role"); // comes null
return user;
}
}
Is there some config I missed or am I retrieving the group info in a wrong way?
EDIT:
If I use contains filter with some characters for example I have 3 groups test1, test2 and other1.
If I use contains filter *, I get null.
However if I use contains filter with test , I get test1 (and test2, if user is path of both groups).
Is wildchar not supported in case of groups?
What if in above case user was part of all 3 groups?
I am not an expert of OKTA but I statred working couple of weeks for one of my clients. I tested with * but it only worked for me for filter Regex. For other filters I never succeeded with *. For example the configuration without * worked perfectly for me.
OKTA CONFIG
I used the code String str = credential.getAttributeAsString("Groups");
But I have one issue that when I have more then one group I still get one group. I want to have list of groups instead.
EDIT - O6 Feb
Finally I restested and I am able to implement wildcard entry with regex I used the regex filter :
In java I got the groups as you suggested :
String[] str = credential.getAttributeAsStringArray("groups");
for(int i = 0; i< str.length; i++){
System.out.println(str[i]);
}
The result is :
Have a great day
http://example.com/api/transfer/transfers/code/456/code/234
When using $this->get('code') on a url like above I expect the REST library to return an array or a list of the codes.
Instead it returns the last one.
Is there a way to return both values in a list, or is there another recommandation for formating the URL.
Thank you
I know it has been long since you posted the question. However it could help other people looking for the same thing.
Assuming that transfer is your controller and transfers is the function, another way to format your url could be:
http://example.com/api/transfer/transfers?code[]=456&code[]=234
This was you perform $this->get('code') you'll get an array back.
If you are creating the url via code then you may use http_build_query(). It handles the necessary escaping. It means it will replace [ for %5B and ] for %5D, in this case.
The code would be like:
$codes = array(456, 234);
$query = http_build_query(array('code' => $data));
$url = 'http://example.com/api/transfer/transfers?' . $query;
I am inserting object into salesforce using REST API. I have one field with Geolocation data-type.
How can inset value to this field using java? My example code is below.
parishud.put("gps__c",?????);
there are two fields to insert(gps(Latitude) gps(Longitude)) but in API i found only one field with name gps__c
HttpClient client = new httpclient.HttpClient();
JSONObject parishud = new JSONObject();
try {
parishud.put("Name","ParishudhTest4");
parishud.put("entitled__c","Yes");
parishud.put("Comments__c","Comments");
parishud.put("fallill_count_after__c","5");
parishud.put("fallill_count_before__c","7");
parishud.put("influencedcount__c","8");
parishud.put("toiletusaseperday__c","3");
parishud.put("parishudhid__c","002");
parishud.put("Received_NBA_Incentive__c","Yes");
parishud.put("Received_NREGA_Incentive__c","Yes");
//parishud.put("gps__c",?????);
PostMethod postaccount = new PostMethod("https://ap1.salesforce.com/services/data/v20.0/sobjects/Parishudh__c/");
postaccount.setRequestHeader("Authorization", "OAuth " + accessToken);
postaccount.setRequestEntity(new StringRequestEntity(parishud.toString(),"application/json", null));
client.executeMethod(postaccount);
The Geolocation field is actually composed out of 3 fields, 2 of which are accessible for us.
If your field name is gps then you can store your data in gps__Latitude__s and gps__Longitude__s (note the double underscore in the beginning like it's a managed package and the ending is "s", not "c").
Make sure you have set profile-level security for your Java integration user to be able to access this field.
If you need code samples - you can check my answer How do I integrate Salesforce with Google Maps? or for example http://blog.internetcreations.com/2012/09/creating-a-geolocation-trigger-in-salesforce-winter-13/
I'm pretty new to cakephp, I did the sample posts tutorial on the website and I'm now working on my first project.
Its pretty simple. The administrator pastes a list of emails and they become records in the database with a random password.
So, I want to register multiple emails by inputting em 'foo#bar.com; bar#foo.com; com#foo.bar' style in the same textbox. In the database they should become a record/email.
For one email my code is:
echo $this -> Form -> input('email');
echo $this -> Form -> input('code', array('default' => genRandomString()));
I also wan't to validate the adresses as emails later on. So could I use php's explode and then validate em on one way or another?
Greetings
Jeff
Cake itself has a neat method to explode them:
$emails = String::tokenize($emailList, ';');
( http://book.cakephp.org/2.0/en/core-utility-libraries/string.html#string )
yes, you can iterate over them and manually validating them one by one or putting them into an array saveAll() understands and use 'validate'=>'first' or 'only' with it.
( http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html#validating-data-from-the-controller )