Retrieving email config - cakephp

Can you tell me how to retrieve email config values (as set up in Config/email.php)? The documentation appears to tell me how to load or set values when creating the CakeEmail object, but I just want to display to the user default values like the "from" address BEFORE they override them or send the email.

You've got a couple of options. You can either get all the email config and extract the bits you need from that using CakeEmail::config() or extract just the bit you need, for example the from email using CakeEmail::from():-
<?php
$Email = new CakeEmail('default');
// Get all the email config
$config = $Email->config();
debug($config['from']);
// Get just the 'from' email config
$from = $Email->from();
debug($from);

OK - I think I have stumbled on the answer; CakeEmail has a (public) "from()" method which returns the (protected) "from" property - this is covered in the "API" documentation, but not in the "book" documentation.

Related

How can I set a default value for partner_ids on mail.compose.message in Odoo14?

iam trying to pass a default value for partner_ids field on mail.compose.message wizard. The wizard is generated from purchase.order, by the send by email function. I have created a new many2many field on purchase order and it holds the res.partner ids that i want to set as the default value of partner_ids on the mail wizard, I basically want to set the ids from the many2many field as the recipients of the mail. I have already tried putting it into context like this, but to no success:
can you please help me?
'recipient_ids = self.recipient_ids.ids
'context.update({'
''default_partner_ids': recipient_ids,')}

how to access cakephp model value in contoller

I want to mail verify key field value on mail when customer used forgot password option. My query is working perfect. when i debug in view i am able to see filed value but in controller i am not able to access that value.
Here is my controller query.
public function forgot() {
if ($this->User->validates() ) {
$auserlogin = $this->User->forgot($this->data['user']['email']);
$this->set('auserlogin', $auserlogin);
$message="Someone requested to reset password \n\n";
$message=$message."Verify Key : ".$auserlogin['0']['user']['verifykey']."\n\n";
$message=$message."Thanks\n\n";
$Email = new CakeEmail();
$Email->from(array('mail#gmail.in' => 'mail'))
->to($this->data['user']['email'])
->subject('Forgot Password')
->send($message);
return $this->redirect(array('controller'=>'users','action'=>'password'));
}
}
In view part i am able to see query is working via this
$auserlogin['0']['user']['verifykey'];
but i want to mail this verifykey to user so i used
$message="Verify Key : ".$auserlogin['0']['user']['verifykey']."\n\n";
OR
$this->set('verifykey', $auserlogin['0']['user']['verifykey']);
$message="Verify Key : ".$verifykey."\n\n";
both are not working in mail i am not receiving variable value on mail i received only
Verify Key :
Model name should be in capitalised inside controller. $auserlogin['0']['User']['verifykey']
Credit goes newbee-dev

How to load a user profile using entity_metadata_wrapper in Drupal 7

I am trying to get the user profile loaded with entity_metadata_wrapper:
$user_profile = entity_metadata_wrapper('user', $uid);
dpm($user_profile);
but I get nothing back. The user fields load fine with user_load($uid); What am I doing wrong?
The information provided by Linas is provably incorrect. The use of entity_metadata_wrapper with an entity id is completely okay. The function uses uid internally only if you pass an object to it to begin with, otherwise it passes the argument directly to EntityDrupalWrapper.
Eventually the set method will be called, which states: "Overridden to support setting the entity by either the object or the id."
This basic code will demonstrate a working call to entity_metadata_wrapper with just a user id.
$wrapper = entity_metadata_wrapper('user', 1);
dpm($wrapper->value());
The output will be an array of all the data belonging to the admin user.
The problem you are having is unrelated to your arguments being passed in (assuming $uid is valid), and requires more information to troubleshoot.
entity_metadata_wrapper expects second parameter to be an object, in this case, user object. According to your variable naming, it seems that you are passing user id instead. You can use user_load to fetch user object by ID.
I have the same problem the code not work return empty (Object) EntityDrupalWrapper
global $user;
$user_profile = entity_metadata_wrapper('user', $user->uid);
dpm($user_profile);
But that code work well and return (Object) stdClass with all users fields like name password and other
global $user;
$user = user_load($user->uid);
$wrapper = entity_metadata_wrapper('user', $user);
dpm($wrapper->value());

setWhatId in salesforce using email template

i have to send an email to a user in salesforce using email template.this template contain merge field type of custom object.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(user.get(0).id);
mail.setTargetObjectId(user.get(0).Id)
mail.setTemplateId(specifier.get(0).Template_id__c);
mail.saveAsActivity = false;
mail.setWhatId(custom_object.Id);
i read in documentation
If you specify a contact for the targetObjectId field, you can specify a whatId as well. This helps to further ensure that merge fields in the template contain the correct data. The value must be one of the following types:
Account
Asset
Campaign
Case
Contract
Opportunity
Order
Product
Solution
Custom
but if we are sending email to a user not to contact then how to assign a custom object for merge field type in custom objects as in the above code
This is a GIGANTIC whole in their email methods, and one that has annoyed me for years. Particularly given workflow email alerts seem to have no problem sending an email template for a user. Alas, you can't use setWhatId() if your target is a user. But you can vote for them to add that functionality,
I've worked around this I typically create a contact with the same name and email as the user, use it to send the email, and then delete it. This works well, although dealing with validation rules on the contact object can be a challenge. See their dev boards for a full discussion.
You can get the template and replace the merge fields as follows:
EmailTemplate template = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'Case Update'];
Case modifiedCase = [SELECT Account.Id, Account.Name, Owner.FirstName, Owner.LastName, CaseNumber, Subject, LastModifiedBy.FirstName, LastModifiedBy.LastName from Case where Id=:modifiedCaseId];
String subject = template.Subject;
subject = subject.replace('{!Case.Account}', modifiedCase.Account.Name);
subject = subject.replace('{!Case.CaseNumber}', modifiedCase.CaseNumber);
subject = subject.replace('{!Case.Subject}', modifiedCase.Subject);
String htmlBody = template.HtmlValue;
htmlBody = htmlBody.replace('{!Case.Account}', modifiedCase.Account.Name);
htmlBody = htmlBody.replace('{!Case.OwnerFullName}', ownerFullName);
...
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject(subject);
email.setHtmlBody(htmlBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
As far as no new fields are added in the template this will work fine. The admin can mess with the format of the email without the need for code changes.
Not sure this is possible to do, but it depends on the relationship between your custom object and your users that will be receiving the merged emails. Do you have a 1-to-1 relationship between User and CustomObject? If so, perhaps adding a reference to the single custom object instance that each user object references and then adding custom formula fields on your user object with CustomObject__r.CustomField__c would do the trick.
In a custom formula field on your User object:
TEXT(CustomObject__r.CustomField__c)
Then your template could be changed into a User template and the merge fields would be the formula fields that actually pointed to your custom object instance. But if you have some other relationship like 1-to-many or many-to-many between User and CustomObject__c, I think you're out of luck.

Backbone - fetched model, set an attribute(modify), then save model, it should update but sending POST request

I created my web site having 2 types of users: admin and user. So, I created 3 pages mainpag.html, admin.html, user.html. and separate models, views, collections, routers.js files for each of them. After logging in, as I am sending users to separate HTML pages with different models, I can't automatically get user model. so I did like this:
First, I made AJAX call to server, asking for the _id (username in session, so I can get id)
from the id, I fetched the model, by model.fetch(), then I got my usermodel with all attributes.
then in the success callback of fetch, I did model.save({weight: "somevalue"}). According to me, it should update right, as the model is already available, that attribute weight also available with some old value, but it is sending POST request, also when I tried model.isNew(), it returned true. Where am I wrong? how can I update my model? I will post more details if required.
More details:
If I remove that save method, then I am getting correct attributes in the model.
If I don't remove that save method, that success and error callbacks are also appearing as attributes in the model.
Code:
addWeight : (e)->
arr=new Array()
arr['_id']=app._id
console.log "asdasd"
console.log arr
console.log arr['_id']
#user_model =new UserModel(arr)
#user_model.fetch({
success : (model,res,options) =>
console.log model
console.log res
arr=new Array()
arr['_id']=e.target.id
#arr['action']='weight' #means , update weight
##user_model.setArr(arr)
##user_model.set({weight : arr['_id']})
console.log "new : "+#user_model.isNew()
#user_model.save({weight : e.target.id})
##user_model.save({
# success : (model,res,options) =>
# console.log "model updated: "+JSON.stringify(model)
# console.log "Res : "+JSON.stringify(res)
# error : (model,res,options) =>
# console.log "Error : "+JSON.stringify(res)
#})
error : (model,res,options) =>
console.log "Error "
})
the above code is written in coffeescript, so even if you don't know coffeescript, don't worry, you can understand easily, and those # mean, it is a comment. here we follow indentation instead of braces.
one more doubt, a model's URL must be changed dynamically according to the requirement, right? what is the best way to achieve that? I am doing like this:
I am populating "array" containing the required fields that should be present in the URL. In model, s init func, I am using #arr=arr, then in URLs function, I check like this.
url : ->
if #arr['id']
"/user/#{#id}"
Is my approach right, or any better approach is there for dynamically setting URLs. Or can I directly set the URLs like this:
#user_model.setUrl "/someurl/someid" //this setUrl method is available in model's definition
#user_model.fetch() or save() or watever that needs url
Just a hunch, but you mentioned that you call model.fetch() to retrieve the _id field. Be sure to either return an id field instead _id (notice the underscore).
The call to model.isNew() returning true is an indicator that the id property was never set from the model.fetch() call.
I look forward to a possible further explanation with your code...
Looking at your code:
/* The model needs an 'id' attribute in order to marked as not new */
#user_model = new UserModel(id: arr['_id'])
Actually if you call
model.set({weight: "somevalue"});
It will update the value in the model, but it won't send a POST request
model.save(attribute);
Actually calls Backbone.sync as you probably know.
EDIT :
You might want ot set
m = Backbone.Model.extend({
idAttribute: '_id'
});
to every model, because the isNew method actually checks if the model has id attribute
Regarding to this you could see here that .set doesn't call backbone.sync here : http://jsfiddle.net/5M9HH/1/

Resources