Have you any idea how to control the queue of Mandrill send emails in PHP ?
Well, I'm using Symfony as a Framework, & this is my send function in the Mandrill class :
public function sendMandrill()
{
$listTo = array();
foreach ($this->contacts as $contact)
{
$listTo[] = [
'name' => $contact->getFname(),
'email' => $contact->getEmail()
];
}
var_dump($listTo);
$email = array(
'html' => $this->message->getBodyHtml(),
'text' => $this->message->getBodyText(),
'subject' => $this->message->getSubject(),
'from_email' => $this->apiDom,
'from_name' => $this->message->getFromName(),
'to' => $listTo,
"preserve_recipients"=> false,
);
$this->senderAPI = new \Mandrill("$this->apiKey");
return $this->senderAPI->messages->send($email);
}
Now, I want to create a function so I can pause the sending for a while so I can change such things, then resume it whenever I want or may be to stop it at all !
which mean there will be 3 functions as following :
public function pauseMandrill()
{
...
}
public function resumeMandrill()
{
...
}
public function stopMandrill()
{
...
}
Related
How can i send auth header, when test codeception rest api?
What i have now:
Yii2 project
"codeception/module-yii2": "^1.0.0"
"codeception/module-rest": "^1.3"
Generated test class by command codecept generate:cest api TestName
My class with test
class CreateWorkspaceCest
{
public function _before(ApiTester $I)
{
}
public function successCreate(ApiTester $I)
{
$title = 'create test';
$description = 'test description';
$I->sendPost('/workspace/create', [
'title' => $title,
'description' => $description,
]);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseIsJson();
$I->seeResponseContainsJson([
'title' => $title,
'description' => $description,
'status' => 'active',
]);
}
}
Now it fails with 403 code, because backend expects header JWT-Key: <TOKEN>
How can i send auth header in sendPost
And where it is better to store auth token in one place to avoid code duplication, during writing tests?
Codeception has a method called haveHttpHeader you can add any header using it.
This is documented half-way down this page. There is also a section on authorization on this other page.
There are a few built-in authorization methods, like amBearerAuthenticated, amAWSAuthenticated, but I believe that there isn't a specific method for JWT.
class CreateWorkspaceCest
{
public function _before(ApiTester $I)
{
}
public function successCreate(ApiTester $I)
{
$title = 'create test';
$description = 'test description';
// You can add any header like this:
$I->haveHttpHeader('Content-Type', 'application/json');
$I->haveHttpHeader('Authorization', 'Bearer user-one-access-token');
// To add the header that you show in the question, you can use:
$I->haveHttpHeader('JWT-Key', '<TOKEN>');
$I->sendPost('/workspace/create', [
'title' => $title,
'description' => $description,
]);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseIsJson();
$I->seeResponseContainsJson([
'title' => $title,
'description' => $description,
'status' => 'active',
]);
}
}
I am trying to send mail with Mailgun. If I write this:
$mg = Mailgun::create('xxxx');
$mg->messages()->send('xxxx', [
'from' => 'dmt.akyol#gmail.com',
'to' => 'dmt.akyol#gmail.com',
'subject' => 'Your Link To Login!',
'text' => 'hello',
]);
it works but I want to send a view (blade) and I don't know how to do.
My code is:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
]);
}
public function sendContactForm(array $customer)
{
$aaa=$this->build($customer);
$mg = Mailgun::create('xxxxxx')
$mg->messages()->send('xxxx'), [
'from' => $customer['customerEmail'],
'to' => ' dmt.akyol#gmail.com',
'subject' => 'Contact Message',
'html' => $aaa,
]);
}
This does not work when I write html or text.
What should I do?
Add ->render() to your build call to store the contents of the view as a string:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
])->render();
}
I am trying to send automated mails via Mandrill in my Laravel 5.1 project. It was working but I was setting up my Mandrill Calls in my AuthController now I wanna have a class App\Marketing where all my functions for sending email will be stored. So in my controllers after an actions happens I can just call up the function with 1 line of code, but this line is giving me troubles I think.
my App\Marketing class looks like this now
class Marketing{
private $mandrill;
/**
* Via construct injection
*
*/
public function __construct(Mail $mandrill)
{
$this->mandrill = $mandrill;
}
public function sendRegistermail()
{
// In template content you write your dynamic content if you use <mc:edit> tags.
$template_content = [];
$message = array(
'subject' => 'Welkom bij SP*RK! - Jouw accountgegevens',
'from_email' => 'noreply#spark.com',
'from_name' => 'SP*RK',
'to' => array(
array(
'email' => $request->input('email'),
'name' => $request->input('name'),
'type' => 'to'
)
),
'merge_vars' => array(
array(
'rcpt' => $request->input('email'),
'vars' => array(
array(
'name' => 'NAME',
'content' => $request->input('name')
),
array(
'name' => 'EMAIL',
'content' => $request->input('email')
)
)
)
)
);
//email validation
if (str_contains($request['email'], "#kuleuven.be")) {
MandrillMail::messages()->sendTemplate('registration-mail', $template_content, $message);
} else {
MandrillMail::messages()->sendTemplate('registration-mail-notactive', $template_content, $message);
}
}
// ----- OR -------
/**
* Via method injection
*
*/
public function sendMail(Mail $mandrill, $data)
{
$mandrill->messages()->sendTemplate($data)
}
// ----- OR -------
/**
* Via the Facade
*
*/
public function sendMailByFacade($data)
{
\MandrillMail::messages()->sendTemplate($data);
}
}
This is how I try to call the function after registration in my postRegister function:
sendRegistermail();
return redirect($this->redirectPath());
sendRegistermail is a method of your Marketing class, you should call it on an instance of that object
So, first of all you have to create a Marketing object instance in your controller. A good way to do this it's by injecting the dependency in the constructor, like this:
//your controller class
class Controller
{
protected $marketing;
//Your controller's constructor
public function __construct(Marketing $marketing)
{
$this->marketing = $marketing;
}
}
Or you can use one of the other methods you have provided in your code to inject the instance.
Once you have an instance of the Marketing class, you only need to call the sendRegistermail method on that instance. In your controller method:
//call the method on the marketing instance
$this->marketing->sendRegistermail();
I have to create a REST API for mobile backend using CakePHP. After following the instructions here I have been able to setup the requirements for creating a REST API. The problem is that I am getting a null in my output when I use the _serialize variable. Here is my code:
class InvitationController extends AppController {
public $components = array('RequestHandler');
public function index() {
}
public function add() {
if ($this->request->is('post')) {
$this->loadModel('Organizer');
$numPeople = $this->request->data['numpeople'];
$share = $this->request->data['myshare'];
$organizer = $this->request->data['organizer'];
$organizerMobile = $this->request->data['organizermobile'];
$this->set('organizerMobile', $organizerMobile);
$deadline = $this->request->data['deadline'];
$links = array();
date_default_timezone_set("Asia/Calcutta");
$now = date('Y-m-d');
$lastOrganizer = $this->Organizer->find('first', array(
'order' => array('Organizer.id' => 'desc')
));
$checkOrganizer = $this->getOrganizerDetails($lastOrganizer);
$pass = //something;
// save data in organizers table
$this->organizer->create();
$this->organizer->save(
array(
'name' => $organizer,
'mobile' => $organizerMobile,
'p_id' => 1,
'share' => $share,
'deadline' => $deadline,
'password' => $pass,
'group_cardinality' => $numPeople,
'created_on' => $now,
)
);
$message = 1;
$this->set(array(
'message' => $message,
'_serialize' => array($message)
));
}
}
}
When I make a request to POST /invitation.json I get null in the ouput with status 200. On the other hand if I simply do echo json_encode($message) in the Controller or in the View I get the correct output. I think I am doing something wrong. Please help!
That's wrong:
'_serialize' => array($message)
Pay attention to the manual, it shows it correctly in the provided examples.
'_serialize' => array('message')
I am trying to set up a queued email in Laravel 4 using the Iron.io driver. I would like to pass some details to the email's Subject and From attributes but they seem to not be making it into the queue request and their presence causes the email to not be sent (not sure where to look for a log with errors). However, simply using Mail::Send() works fine.
Here is the code in question:
public function handleFeedbackForm()
{
$data = array(
'name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'),
'commentType' => Input::get('commentType'),
'testimonialPublish_answer' => Input::get('testimonialPublish_answer'),
'comment' => Input::get('message')
);
$rules = array(
'name_f' => 'required',
'name_l' => 'required',
'email' => 'required|email',
'message' => 'required'
);
$v = Validator::make(Input::all(), $rules);
if ($v->passes())
{
$emailInfo = array('name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'));
Mail::queue('emails.feedback', $data, function($message) use($emailInfo)
{
$recipients = array();
$form = MailType::find(1);
foreach ($form->users as $user)
{
$recipients[] = $user->email;
}
if (count($recipients) == 0)
{
// Nobody for this field, send to webmaster
$recipients[] = 'someone#somewhere.com';
}
$message->to($recipients)
->from($emailInfo['email'])
->subject('Foobar Feedback Form Message - ' . $emailInfo['name_f'] . ' ' . $emailInfo['name_l']);
});
return Redirect::to('contact')->with('feedbackSuccess', true);
}
else
{
return Redirect::to('contact')->with('feedbackError', true);
}
}
Any ideas? Thanks!