Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email? - api

So I'm using Laravel 5.1 and trying to integrate with Mailgun. Well, that's easy, but now I'm trying to send custom variables from my application along with my emails.
I'm actually switching our application over from Mandrill because of their "new direction" and such. With them, I could supply the variables and tags via the email headers, but with Mailgun, that only works when you send via SMTP. In Laravel, Mail::send() uses an API call, so in theory I'd add the metadata there with "v:my-custom-data" => "{"this_id": 123}", but I'd like to avoid altering core classes like that.
I also considered using Bogardo/Mailgun, but then I'd have to replace all the Mail::send()s with Mailgun::send(), and then I couldn't send emails locally (environment based email driver), and then the application would be "married" to Mailgun.
Anyone done this before? Please let me know if I'm not clear here.

I fixed my own problem. I was wrong, YOU CAN add custom variables via the SMTP method:
// Send email with custom variables and tags in Laravel
Mail::send('emails.blank',
['html' => 'This is a test of Mailgun. <strong>How did it work out?</strong>'],
function($message) {
$message->to('jack#mailinator.com');
$message->subject('Mailgun API Test');
$headers = $message->getHeaders();
$headers->addTextHeader('X-Mailgun-Variables', '{"msg_id": "666", "my_campaign_id": 1313}');
$headers->addTextHeader('X-Mailgun-Tag', 'test-tag');
});
My testing was just inadequate. Very good to know, however I think this is an undocumented feature, so I suggest using with caution.

Update for Laravel 5.4+
As you can read in the offical docs:
The withSwiftMessage method of the Mailable base class allows you to register a callback which will be invoked with the raw SwiftMailer message instance before sending the message. This gives you an opportunity to customize the message before it is delivered:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$this->view('emails.orders.shipped');
$this->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Custom-Header', 'HeaderValue');
});
}

You can just do like this in Laravel 5 :
Mail::send(['text' => 'my_template'], $data, function ($message) {
..
$message->getSwiftMessage()->getHeaders()->addTextHeader('X-Mailgun-Tag', 'my-tag');
});

Laravel implementation of the Mailgun Driver doesn't support options/parameters.
Here's a Gist which adds support to the native driver : https://gist.github.com/j0um/27df29e165a1c1e0634a8dee2122db99

Related

Failed to update Salesforce account because of DUPLICATES_DETECTED error even with Allow setting in Duplicate Rule

I'm using JSforce to integrate Salesforce into my app. When I update a Salesforce account from my end via JSforce update function, it got DUPLICATES_DETECTED error because Salesforce considers this account is duplicate with another one. Although I set Allow in Duplicate rule setting of the Salesforce page and we can edit successfully this account on it, It cannot be done via API (with using JSforce) from my end because of this error. Is this possible to bypass Duplicate Management via JSforce without disabling these rules?
const jsforce = require('jsforce');
/* Update to Salesforce */
update: function (updateObject, callback) {
this.getSObject().update(updateObject, callback);
}
Some documents suggest setting allowSave to true in header of request via API soap like this but I don't use API soap, I'm using JSforce instead.
You just got possibility to do this in REST API, which jsforce uses.
You can do it this way:
sfConnection.sobject("Account").create(mappedAccounts, {
allowRecursive: true,
headers: {
'Sforce-Duplicate-Rule-Header': 'allowSave=true'
}
});
This is not explicitly explained in documentation, but you can figure it out from their code.
Other possible values you can find here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/headers_duplicaterules.htm

Incorporating sendEmailVerification method in frontend handler or backend signup method?

I'm trying to incorporate an email verification step in the signing up process of my app. Looking at the docs online, seems like doing it in the front-end seems like the way to go.
Problem is the examples I see online have the entire sign in process done in the front-end, and just simply include the the sendEmailVerification method.
sendEmailVerification Method
firebase.auth().currentUser.sendEmailVerification().then(function() {
// Email sent.
}).catch(function(error) {
// An error happened.
});
I (instead) built my sign up method in the backend and its respective handler in the front.
Front-End Sign Up Handler
authHandler = () => {
const authData = {
email: this.state.controls.email.value,
password: this.state.controls.password.value
};
this.props.onTryAuth(authData, this.state.authMode);
// ontTryAuth is a backend action that creates new users in Firebase
};
Is it a good idea to include the sendEmailVerification method into this front-end handler code? If so, how do I go about doing it?
Yes you can use it. First this method is built by Firebase because people who use Firebase don't always/never have a server. You can yes build your own signup method in your server, but why you would use Firebase if you start to override all the stuff they give you? For me I see Firebase as a prototyping tool and so I use all the features they give, so that let me put an app working faster.
Let me know if that make sense.

Passing javascript variable to velocity variable templete

I have installed xwiki successfully and able to generate wiki pages using velocity template language.
Could anyone please tell me that how can I pass javascript varible to velocity templete. I have gone through few forums that I need to pass the parameter to server to get this but I have no idea. Please find the files below.
<script type="text/javascript">
function generateFunction()
{
var variable = document.getElementById('text').value;
}
</script>
#set($test = "variable")
$test
You have to make an ajax call from the client to the server.If you're using jquery, you would have something like:
$.post('/send/my/var', { 'variable' : value });
Without jquery, see this XmlHttpRequest documentation.
And then, on the server side, the /send/my/var URL should reach a template where you can do:
#set($test = $params.variable)
And you would do something useful with it on the server-side, like store it in the session, in the database, etc.
If you need to send back something from Velocity to Javascript, then you'll typically have to format JSON code, and add an asynchronous completion callback parameter to the ajax call:
$.post('/send/my/var', { 'variable' : value },
function(data)
{
// do something with data sent back from the server
});
It's also possible to have synchronous calls, that is to have javascript wait for the server response, but it's generally a bad idea to do so and I won't extrapolate on it here.
As a final note, you should also implement a proper error handling. With jQuery for instance, the syntax would be:
$(document).ajaxError(function(event, jqxhr, settings, message)
{
console.log(message);
});
It can't be done,
Apache Velocity template is a server side engine,
Meaning that on the server, Velocity will get the template and try to render, only after it finished to render the template, it will be returned to client which will execute client code as Javascript
Velocity alternative is freemarker, which I found similar question and answer , Question:
How to call freemarker function with param from javascript
Answer:
There's no way for the client side web browser code to call a server side Freemarker function

CakePHP Authentication on REST API

So I'm creating a REST API for a web app I'm developing, and I know the basic ways for authentication are either sending the credentials on each request or sending a token.
Since I have never used token before, I think I may send the credentials for each request. The point is I can't find any examples on how to handle this in the controller. Would it be something like this?
public function api_index() {
if(!$this->Auth->login()) return;
$this->set(array(
'models' => $this->Model->find('all'),
'_serialize' => array('models')
));
}
I don't really think this is the way AuthComponent::login() works, can I get some directions here please?
Alright, first a clarification about how AuthComponent::login works. In Cake 2.x that method does not do any authentication, but rather creates the Auth.User array in your session. You need to implement the actual authentication yourself (the User model is a natural place to do this). A basic authentication method might look like this:
App::uses('AuthComponent', 'Controller/Component');
public function authenticate($data) {
$user = $this->find('first', array(
'conditions' => array('User.login' => $data['login']),
));
if($user['User']['password'] !== AuthComponent::password($data['password']) {
return false;
}
unset($user['User']['password']); // don't forget this part
return $user;
// the reason I return the user is so I can pass it to Authcomponent::login if desired
}
Now you can use this from any controller as long as the User model is loaded. You may be aware that you can load it by calling Controller::loadModel('User').
If you want to authenticate every request, then you should then put in the beforeFilter method of AppController:
public function beforeFilter() {
$this->loadModel('User');
if(!$this->User->authenticate($this->request->data)) {
throw new UnauthorizedException(__('You don\'t belong here.'));
}
}
All of the above assumes that you pass POST values for login and password every time. I think token authentication is definitely the better way to go, but for getting up and running this should work. Some downsides include sending password in cleartext (unless you require ssl) every request and the probably high cpu usage of the hashing algorithm each time. Nevertheless, I hope this gives you a better idea of how to do authentication with cakephp.
Let me know if something needs clarifying.
Update:
Since posting this, I found out that you can actually use AuthComponent::login with no parameters, but I am not a fan of doing so. From the CakePHP documentation:
In 2.x $this->Auth->login($this->request->data) will log the user in with
whatever data is posted, whereas in 1.3 $this->Auth->login($this->data)
would try to identify the user first and only log in when successful.
AuthComponent::login() creates a session variable that stores the user data, so say you had data that was something like.
$data = array('User' => array('id' => 1, 'username' => 'johndoe'));
Then you would use
$this->Auth->login($data);
And access the data with
$this->Auth->user('User');
To get the user id
$this->Auth->user('User.id');
In your AppControllers beforefilter put $this->Auth->deny(); that will deny all actions to someone who is not logged in. Then in each controllers before filter you want to $this->Auth->allow(array('view')); 'view' being the name of an action you want to be public.
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
Cakephp 2.X, After lots of research about this on internet I did not find any satisfactory answer So I found a way myself to do this. May be this answer will help some folks in future. This answer is only applicable for REST API's in cake php.
Add following line in your logic action of REST API before checking for the $this->Auth->login().
$this->request->data['User'] = $this->request->data ;
if($this->Auth->login()){
echo "Hurray You are logged In from REST API.";
}
else
{
throw new UnauthorizedException(__('You don\'t belong here.'));
}
Desciption : As #threeve said in one of the answers that $this->Auth->login() does not do any authentication itself. but rather creates the Auth.User array in your session. For Authentication we require our $this->request->data to be inside User array since User is the model which will check the Credentials in Database. Therefore we have to pass the data to Auth as it is required when not using REST API's. Rest of the things will be handled by Cake and Auth itself.
Any enhancements or suggestions on this answer are most welcome.

Authentication with SMTP transport using Swiftmailer

I'm using Swiftmailer to send emails in my Yii based application and it works great, so there is no problems with the Swiftmailer extension wrapper. But now I want to use the SMPT transport to authenticate the users in my application against the mail server. The code I'm using:
$SM = Yii::app()->swiftMailer;
$mailHost = 'XXX';
$mailPort = XXX;
// New transport
$transport = $SM->smtpTransport($mailHost, $mailPort);
$transport->setUsername($username);
$transport->setPassword($password);
$transport->start();
if($transport->isStarted()){
// authenticated;
}else{
// error;
}
$transport->stop();
This isn't working. When I set real login credentials instantly logs in. But when I type wrong ones, it spends some time but at last it logs in too. I don't know why this happens because just when using real credentials Swiftmailer can send emails.
I'm using the right API functions? I'm doing something wrong?
Looking at the source, when you call start(), started is always set to True.
I'm not familiar with switfmailer, but it looks like you'll want to check the results of authenticate()