Attach a file with contentId from inside an email Layout - cakephp

What I have
In my Controller (CakePHP 3.8):
$email = new \Cake\Mailer\Email();
$email
->emailFormat('html')
->setLayout('mylayout')
->setTo($to)
->setSubject($subject)
->addAttachments([
'logo.png' => [
'file' => $logoImage,
'mimetype' => mime_content_type($logoImage),
'contentId' => 'mylogo'
]
])
->send($message);
And then in src/Template/Layout/Email/html/mylayout.ctp:
<img src="cid:mylogo">
That works.
What I want
I want to embed that image from within the Layout file.
So I don't have to repeat ->addAttachments() every time I'm sending an e-mail
If I replace the image I can do it in the Layout only once
If I use another Layout that doesn't need the image, it won't be attached and sent
Using contentId looks like an option. But how do I do it from inside the Layout?
What I tried
Sending the image as data/uri, but that didn't work
Searching for some Cake\Mailer instance under $this when in Layout, but couldn't find any

Related

Cakephp not sending attachment, only send plain html

I am trying to send an attachment with cakephp email, but only plain html sent and not a attachment.
The following code I am using.
$nmessage ="Hello Test\r\n\r\n";
$email = new CakeEmail();
$email->from(array('abc#example.com' => 'Test'));
$email->filePaths = array('/screenshots/');
$email->attachments =array('Google-Maps-9.22.2.jpg');
$email->to('user4#gmail.com');
$email->subject('Register a visit ');
$email->emailFormat('html');
$email->send($nmessage); // or use a template etc
To send attachments you can do it the following ways, first a string with the full path (notice there is no equals symbol, it's a function of the CakeEmail class).
$email->attachments('/full/file/path/file.jpg');
Secondly is the same but wrapped in an array
$email->attachments(array('/full/file/path/file.png'));
Thirdly an array with keys to rename the file
$Email->attachments(array('photo.png' => '/full/some_hash.png'))
And finally you can use nested arrays
$email->attachments(array(
'photo.png' => array(
'file' => '/full/some_hash.png',
'mimetype' => 'image/png',
'contentId' => 'my-unique-id'
)
));
So in summary, don't use $email->attachments = and make sure to provide the full path.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments

How to use Auth component of Cakephp 3 in Event

I'm using CakePHP 3.2 and proffer plugin for image uploading.
I want to rewrite the default path of proffer plugin to upload image and change image name before save.
As per the documentation of proffer from github. I have created an event in /src/Event
Now I want to rename the file like
$this->Auth->user('id').'-'.$row('id').date('dmyhis').ext
this is what I have done
$newFilename = $this->Auth->user('id').'-'.$event->subject()->get('id') . '_' . Inflector::slug($event->subject()->get('name')) . date('ymdhis') . $ext;
But this is giving error that Auth can not be used here. Is there any way to use Auth Component outside controller ?
You can access the logged in user id by loading the session.
use Cake\Network\Session;
$session = new Session();
$userData = $session->read('Auth.User.id');
Use this as a reference: Reading & Writing Session Data
With cake 3.x and after, you should using this:
$this->request->session()->read('Auth');
When debug it
debug( $this->request->session()->read('Auth') );
[
'id' => (int) 2,
'username' => 'admin',
'nice_name' => 'Tommy Do',
'first_name' => 'Huy',
'last_name' => 'Đỗ',
...//and more info in UserTable
]
And access to each element.
$this->request->session()->read('Auth.nice_name');
Print:
Tommy Do
Hope it will help you :D

CakePHP Clicking PDF Link and View it on New Tab

Ok, so I have a web app that uploads a file to the webserver. My input fields in my upload form include: type of upload (dropdown list), title, description, and the file to be uploaded, which is a PDF.
Once the PDF file is uploaded, the download link will appear in another page for the public to see. In addition, the title typed in the input field is the download link.
Now, I want to change my code. Instead of downloading it directly when the link is clicked I want it to open in a new tab, so the users can first look at the PDF file then download it from there.
Here are my codes.
Controller:
public function sendFile(){
$id = $this->request->params['pass'][0];
$staffup = $this->StaffUpload->find('first', array('conditions' => array('iduploads'=>$id)));
$this->response->file($staffup['StaffUpload']['dest'], array('download' => true, 'name' => $staffup['StaffUpload']['title']));
return $this->response;
}
The code above is the download function.
public function resources() {
$this->layout = 'website';
$this->set('staff_uploads', $this->StaffUpload->find('all', array('conditions' => array('type' => 'Resource'))));
}
The code above is the view wherein I show all uploaded files which type is Resources.
View:
<?php
foreach ($staff_uploads as $staff_uploads) {
?>
ul>
<li>
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array('controller' => 'websites', 'action' => 'sendFile', $staff_uploads['StaffUpload']['iduploads']));
?>
</li>
</ul>
<?php
}
?>
The code above shows the view.
So yeah, back to the question. I want to change the download link to a link in which when clicked, will show the PDF file in a new tab. How do I do that? And by the way, the codes posted above are all working properly. I just want to change my code so that it will be viewed in a new tab when clicked.
Thank you!
According to the docs:
echo $this->Html->link(
'Enter',
'/pages/home',
array('target' => '_blank')
);

Custom theme admin login Drupal 7

I have a problem using Drupal 7 admin login in my custom theme. After creating a custom theme when I try to submit the login form, I receive the following error:
"Access denied You are not authorized to access this page."
When I revert back to a default theme (eg. Bartik), everything works fine again.
After reading a lot of solutions, I can't seem to find the right one for me. Any ideas?
EDIT:
The problem seems to be the "drupal_render(drupal_get_form('search_block_form'))" in my page.tpl.php. For my theme i need a form markup without any container, so i used this function to render the form. The solution for me was to change it like:
$form_array= drupal_get_form("user_login");
$form = drupal_render($form_array);
echo $form;
First take a look at this:
your-drupal-parh/user -if this link takes to the login form, then you just need to customize the login form.
If it doesn't help, try create a login.
Update templete.php
function themename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'themename') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'themename_preprocess_user_login'
),
);
return $items;
}
function themename_preprocess_user_login(&$vars) {
$vars['intro_text'] = t('Custom login form');
}
themename is the name of your theme.
path points to the path where the user-login.tpl.php file exists in your theme directory[here drupal_get_path('theme', 'themename') . '/templates' means that the user-login.tpl.php file is in the theme's template directory].
Code for user-login.tpl.php:
<p><?php print render($intro_text); ?></p>
<div class="redha-user-login-form-wrapper">
<?php print drupal_render_children($form) ?>
</div>
Clear cache and see what happens.

title_for_layout while viewing pdf with cakeResponse

i'm viewing pdf files from database using this function:
public function pdfPreview($taskId){
$file = $this->StoredFile->find('first', array(
'conditions' => array(
'StoredFile.id' => $taskId
),
'contain' => false
));
$this->response->body($file['StoredFile']['data']);
$this->response->type('pdf');
//$this->response->header('Content-Disposition', 'inline');
//Return response object to prevent controller from trying to render a view
return $this->response;
}
Is there any way to set title for layout with CakeResponse while not rendering layout, because i'm trying to set filename as title, but i didn't found solution?
Usually the $title_for_layout view var is passed to the html and put between <title> tags.
However, as I understand, you are outputting a PDF using raw data from de database. This is then interpreted by the browser to render a PDF view. The browser thinks it is displaying a file, so when you set the filename in the Content-Disposition header it will display this in its title. Like they do on https://stackoverflow.com/a/2016016/1535656

Resources