Mail Attachment not send when using Cron - drupal

Email Attachment is sent correctly when I call my function by visiting the url that I set with the hook menu
EX :When I visit /admin/config/send the hook_menu will call main() method and send the email with the attachment.
but when I call the same function main() from inside the hook_cron() the mail is sent(body and message is set) but not with the attachment :
function hook_cron(){
main();
}

Sounds like you have to ensure the cron is executed with an authenticated/admin user loaded.
In Drupal 8 this would be like following (source).
use Drupal\user\Entity\User;
/**
* Implements hook_cron().
*/
function my_module_cron() {
// Login as user 1
$user = User::load(1);
user_login_finalize($user);
// Your custom cron functions
// Login as user 0 (anonymous)
$user = User::load(0);
user_login_finalize($user);
}

Related

Redirect to internal URL after login - Drupal 8

On my D8 site I want to redirect all users to an internal URL after they login. below is my snippet:
function mymodule_user_login($account) {
$url = Url::fromUri('internal:/dashboard-overview');
$response = new RedirectResponse($url->toString());
$response->send();
}
This snippet redirects to "Access denied" error page even for administrators. There is no permission set to visit this URL. Still the page is not loading even for admins. Any help ?
Have you tried this module? https://www.drupal.org/project/redirect_after_login
i think it will do the task you are looking for.
Drupal 8 and 9
You need to alter the login form and add a submit handler which will take care of the redirection. You cannot use $form_state->setRedirectUrl() directly in the form alter, since it will be overwritten by UserForm::submitForm().
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
$form['#submit'][] = 'mymodule_user_login_submit';
}
/**
* Form submission handler for user_login_form().
*
* Redirects the user to the dashboard after logging in.
*/
function mymodule_user_login_submit(&$form, FormStateInterface $form_state) {
$url = Url::fromRoute('mymodule.dashboard');
// Check if a destination was set, probably on an exception controller.
// #see \Drupal\user\Form\UserLoginForm::submitForm()
$request = \Drupal::service('request_stack')->getCurrentRequest();
if (!$request->request->has('destination')) {
$form_state->setRedirectUrl($url);
}
else {
$request->query->set('destination', $request->request->get('destination'));
}
}

Downloading a file with BrowserKit/Mink

Preface
Using Behat, I can write a scenario like this:
Scenario: I can get a template
When I send a GET request to "/products/template"
Then the response status code should be 200
that links to this Behatch step implemented by vendor/behatch/contexts/src/Context/RestContext.php:
/**
* Sends a HTTP request
*
* #Given I send a :method request to :url
*/
public function iSendARequestTo($method, $url, PyStringNode $body = null, $files = [])
{
return $this->request->send(
$method,
$this->locatePath($url),
[],
$files,
$body !== null ? $body->getRaw() : null
);
}
Request
In order to validate file responses, I would like to write a scenario like:
Scenario: I can get a template
When I send a GET download request to "/products/template" and save it to "/tmp"
Then the response status code should be 200
And the file "/tmp/template.csv" should exist
I would like to write a step that sends a GET request and downloads a file to a provided path, akin to the sink Guzzle functionality, similar to:
/**
* Sends a HTTP request
*
* #Given I send a :method download request to :url and save it to :path
*/
public function iSendADownloadRequestTo($method, $url, $path)
{
return $this->request->send(
$method,
$this->locatePath($url),
["sink" => __DIR__.$path]
);
}
The previous code succesfully sends the request but it doesn't save the file. How could I achieve this?
I would download the file with use of Guzzle or curl. Mink is used and designed to interact with pages, and to my knowledge and quick glance at its docs doesn't support functionality you are after.

contact form 7 wordpress plugin not working properly

I am using contact form 7 wordpress plugin for one of website and facing problem to adding action after sending mail.
I want to call some CRM Api when data submitted by the user and also sent mail to admin so i have tried following way.
I added action and function to function.php
1)
add_action('init', create_function('',
'add_action("wpcf7_admin_after_mail", "leads_integration_wp_cf7");'));
function leads_integration_wp_cf7($cf7 ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if($submission)
{
$posted_data = $submission->get_posted_data();
//using curl make request here
}
}
So using this way i got mail but i am thinking my function(leads_integration_wp_cf7) did not called and i did not get entry in CRM.
2)
add_action('wpcf7_before_send_mail', 'leads_integration_wp_cf7');
using this way i made successfully request to CRM but mail sending stop.on a form page ajax preloader loading,loading, and not redirect on url.
Anybody face this issue i am new in wordpress.
The action wpcf7_admin_after_mail is called in edit-contact-form.php file and it is used for form control ui purpose so it will not be helpful for this case.
The action wpcf7_before_send_mail is correct for doing some task when contact form is posted and email is sent, can you confirm that mail is working properly if this action hook is not applied?
Also try renaming the param $cf7 to $contact_form
function leads_integration_wp_cf7($cf7) {
To
function leads_integration_wp_cf7($contact_form) {
{code: 'invalid_json', message: 'The response is not a valid JSON response.'}
code: "invalid_json"
message: "The response is not a valid JSON response."

How to call wordpress function directly from url?

I have added custom payment method to woocommerce and its working fine . I just have one problem that it calls a callback url for saving transaction information to db. I have created new function for this in my plugin file but i cant excess it directly .
This is how i have done it:
//add_action('wp_ajax_nopriv_payment_callback_action', 'payment_callback_action');
//function
function payment_callback_action() {
echo "Its Working!";
}
I am trying to access it by :
url:"<?=site_url( '/' );?>wp-admin/admin-ajax.php?action=payment_callback_action
It seemd that it because of i dnt have privillage to use it directly but how can i do this ?.
Thanks
# for users not logged in
add_action('wp_ajax_nopriv_payment_callback_action', 'dixipay_callback_action');
# for users logged in
add_action('wp_ajax_payment_callback_action', 'dixipay_callback_action');
# Your callback
function dixipay_callback_action() {
echo "Its Working!";
}
read more: http://codex.wordpress.org/AJAX_in_Plugins

PreExecute() function in symfony2

I developed an application using symfony2, I want to know how to write preExecute() function symfony2 for following case:
when I log-in into the system; it redirect me on user profile section when I log-out from same screen, It killed session and redirect me on login screen, but when I hit browser's back button then it will redirect me on profile screen, which shows me all user information but when I click for next process from same screen then I redirect me on login page.
I just want to add preExecute function like symfony1.4 for this case, so I checked session and if it is null then it will redirect me on login page if I hit browser's back button when I alrady log-out from the system.
I already added following code in profileController.php files,
public function indexAction() {
$session = $this->get('request')->getSession();
$userId = $session->get('id');
if ($userId == 0 || $userId == '') {
return $this->redirect($this->generateUrl('_security_login'));
} else {
//my code
}
}
//logout action
public function dologoutAction(){
$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();
$this->container->get('request')->getSession('session')->clear();
return $this->redirect($this->generateUrl('_security_login'));
}
if there is any other way to handle this case then please help me for the same.
Thank you.
Just require the ROLE_USER role on the profile action and the firewall will do the redirect-to-the-login-form-and-then-redirect-back stuff:
/**
* #Secure("ROLE_USER")
*/
public function profileAction()
{
// ...
}
For more information read the Security chapter of the Symfony book.

Resources