How can I forward an email in laravel? - laravel

I am using https://github.com/Webklex/laravel-imap to retrieve email from my mail server.
Now I need to forward the exact mail with all body(text, html, attachments). How can I forward the email?

I was working on the same thing, and wanted to give a more concrete answer related to using Laravel-IMAP to get emails and then forward them.
See this answer which helped out a lot.
use Illuminate\Mail\Mailable;
public function build()
{
$this->view('emails.emails.received')->with('body', $this->message->getHTMLBody())
->from($this->message->getFrom()->first()->mail, $this->message->getFrom()->first()->personal)
->to('<forwarded_email>')
->replyTo($this->message->getReplyTo()->first()->mail, $this->message->getReplyTo()->first()->personal)
->subject($this->message->getSubject());
foreach ($this->message->getAttachments() as $attachment) {
$this->attach($attachment);
}
$this->withSwiftMessage(function ($msg) {
$msg->getHeaders()->addTextHeader('In-Reply-To', $this->message->getMessageId());
$msg->getHeaders()->addTextHeader('references', $this->message->getReferences());
});
return $this;
}
Also - My template emails.emails.received contains only the following:
{!! $body !!}

Sure, that's possible, I recommend you to use:
Laravel Mail Auto Embed
Its use is very simple, you write your markdown normally:
<!-- eg: resources/vendor/mail/markdown/order-shipped.blade.php -->
#component('mail::message')
# Order Shipped
Your order has been shipped!
#component('mail::button', ['url' => $url])
View Order
#endcomponent
Purchased product:
![product](https://domain .com/products/product-1.png)
Thanks,<br>
{{ config('app.name') }}
#endcomponent
And when you send the email, it replace the url image for inline data, most simple to handle and to forward an email
When sending, it will replace the link that would normally be generated:
<img src="https://example.com/products/product-1.png">
by an embedded inline attachment of the image:
<img src="cid:3991f143cf1a86257f8671883736613c#Swift.generated">

I will give you the idea
first you need to take this email data which you have taken from laravel-imap and store in a variable
first you need to specify the wanted message lets say you are looking for a message that contains specific information which can be specified
like this
foreach($aFolder as $oFolder){
//Get all messages by custom search criteria
/** #var \Webklex\IMAP\Support\MessageCollection $aMessage */
$aMessage = $oFolder->query()->where(["CUSTOM_Word" => "Hello"]])->get();
}
now you have a specific email with all of its components
now send it to the desired email or list of emails (use foreach)
pass $aMessage variable to your send function then
$receiver_email = 'john#gmail.com';
$data = array ('subject' => '$aMessage->getSubject().'<br />'' ,
'Attachments' => '$aMessage->getAttachments()->count().'<br />'',
'body' => '$aMessage->getHTMLBody(true)';
)
Mail::send('emails.message', $data, function ($message) {
$message->to($receiver_email)
->subject($aMessage->getSubject());
$message->from('youremail#app.com' , 'your name')
});
and in your emails/message don't forget to put your custom message with subject , attachments and body as an output
in emails/message it will be the message which will be sent to the client and you can design it using html , css just like any other file it uses the laravel blade template here is an example from Medium
Hello <strong>{{ $subject}}</strong>
<p>{{$body}}</p>
Note : you might find some typos or errors because like what i have told you i gave you the idea but can't give you exactly what you want.
and here you can find another question about sending emails you might want to take a look at it

Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext],
// ^^^^
Also replace auto-escaped block {{ }} with unescaped {!! !!} in the template:
<p> {!! $text !!} </p>

Related

Laravel: how to create a rendered view from a string instead of a blade file?

I've some html with {{ soimething }} placeholders in a table.
I would like to get the rendered view from this custom html.
I would like to avoid to manually do string replace.
Is it possible?
Note : I seen suggested questions but I ended finding a more concise way to reach my goal. So I post an answer to this question. Please keep this open.
You can use Blade Facade.
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Blade;
public function __invoke()
{
$name='Peter Pan';
return Blade::render("
<h1> Hello {$name} </h1>
",['name'=>$name]);
}
Found
We can use \Illuminate\View\Compilers\BladeCompiler::render($string, $data)
Where
$string is the text to parse, for example
Hi {{$username}}
$data is the same associate array we could normally pass down to view() helper, for example [ 'username' => $this->email ]
I was missing this from the official doc: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates
So we can also use
use Illuminate\Support\Facades\Blade;
Blade::render($string, $data)

Sending html content as mail body laravel mail sending application

My controller is
public function basic_email() {
$data = array('name'=>"Virat Gandhi",'roll'=>"123");
Mail::send(['text'=>'mail'], $data, function($message) {
$message->to('xyz#gmail.com', 'Basil Baby')->subject
('Laravel Basic Testing Mail');
$message->from('abc#yahoo.com','Virat Gandhi');
});
echo "Basic Email Sent. Check your inbox.";
}
My blade is
Hi, {{ $name }}
your roll number is {{$roll}}
please click on the link to verify your account
Mail is being received. but the mail body is displaying html content as such. How to make verify you account a html link in mail body
Change your key text to html in your send function.
text key send data as a plain text
Mail::send( ['html' => 'mail']...
Also change {{}} to {!! !!}
Reference:
Laravel -> Mail
If you are trying to verify registered users, I would suggest you to use Laravel's build-in verification system.
You can follow the link:
Laravel email verification

Laravel 5.1/5.6 How to send a url in email blade

I am using Mail::send function to send emails to my end users with dynamically generated links in them Below is my sample code for the function -
$myemail = "some email adderess";
$myurl = "some url that will be emailed";
Mail::send('emails.mybladetemplate', ['myemail' => $myemail] , function ($message) use ($myemail){
$message->subject('Your favorite links');
$message->from('someone#company.com', 'Company');
$message->to($myemail);
});
I am having trouble passing $myurl to the blade template and publishing it. I even used HTML{{ }} in my blade template but no success. I have tested Mail::send with other templates and it works fine. Just not when I pass the variables.
Added ['myemail' => $myemail, 'myurl' => $myurl] and use ($myemail, $myurl) to solve for it.

Laravel 5.4^ - How to customize notification email layout?

I am trying to customize the HTML email layout that is used when sending notifications via email.
I have published both the mail and notification views.
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
If I modify the /resources/views/vendor/notifications/email.blade.php file, I can only change the BODY content of the emails that get sent. I am looking to modify the footer, header, and every other part of the email layout as well.
I tried also modifying the views inside /resources/vendor/mail/html/, but whenever the notification gets sent, it is not even using these views and instead uses the default laravel framework ones.
I am aware I can set a view on the MailMessage returned by my Notification class, but I want to keep the standard line(), greeting(), etc. functions.
Does anyone know how I can get my notifications to send email using the views in /resources/vendor/mail/html ?
The following is my /resources/views/vendor/notifications/email.blade.php file, but it does not have anywhere to customize the header/footer/ overall layout.
#component('mail::message')
{{-- Greeting --}}
#if (! empty($greeting))
# {{ $greeting }}
#else
#if ($level == 'error')
# Whoops!
#else
# Hello!
#endif
#endif
{{-- Intro Lines --}}
#foreach ($introLines as $line)
{{ $line }}
#endforeach
{{-- Action Button --}}
#if (isset($actionText))
<?php
switch ($level) {
case 'success':
$color = 'green';
break;
case 'error':
$color = 'red';
break;
default:
$color = 'blue';
}
?>
#component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
#endcomponent
#endif
{{-- Outro Lines --}}
#foreach ($outroLines as $line)
{{ $line }}
#endforeach
<!-- Salutation -->
#if (! empty($salutation))
{{ $salutation }}
#else
Regards,<br>{{ config('app.name') }}
#endif
<!-- Subcopy -->
#if (isset($actionText))
#component('mail::subcopy')
If you’re having trouble clicking the "{{ $actionText }}" button, copy and paste the URL below
into your web browser: [{{ $actionUrl }}]({{ $actionUrl }})
#endcomponent
#endif
#endcomponent
Run this command
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
update for laravel 5.7+
php artisan vendor:publish
and then you will get:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
and then just type in that number in front to publish the file for editing
and then in
/resources/views/vendor/mail/html/
you can edit all the components and customize anything you want.
For example i have edited the sentence "All rights reserved". to "All test reserved" at the bottom of that image inside this file:
/resources/views/vendor/mail/html/message.blade.php
and this is what i got:
Make sure to have the right configuration in your config/mail.php :
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
]
],
I wrote an article on how to create a notification and modify your template including the header and footer.
It includes the explanation on how the Laravel components work and how to pass your data to a new email template.
https://medium.com/#adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c
The most important part is placing the following code inside your email template:
#component('mail::layout')
{{-- Header --}}
#slot('header')
#component('mail::header', ['url' => config('app.url')])
Header Title
#endcomponent
#endslot
{{-- Body --}}
This is our main message {{ $user }}
{{-- Subcopy --}}
#isset($subcopy)
#slot('subcopy')
#component('mail::subcopy')
{{ $subcopy }}
#endcomponent
#endslot
#endisset
{{-- Footer --}}
#slot('footer')
#component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
#endcomponent
#endslot
#endcomponent
You can check the medium article in case you want more details on how the components work and how to properly pass the data.
#Brian
You can just make change to the #component directives in your template file to use your custom templates. For example:
Replace #component('mail::message') with #component('vendor.mail.html.message'), assuming your template is located at /resources/views/vendor/mail/html/message.blade.php
I ended up just using a custom view rather than trying to get the built in Laravel ones to work.
I added the following use statement to my Notification class
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
Then in the toMail method:
public function toMail($notifiable)
{
$view_file = 'emails.teamInvitation';
$view = View::make($view_file, ['sender' => $this->sender, 'invitationToken' => $this->invitationToken, 'team' => $this->team ]);
$view = new HtmlString(with(new CssToInlineStyles)->convert($view));
return (new MailMessage)
->subject('PreSource Invitation From ' . $this->sender->name )
->view('emails.htmlBlank', ['bodyContent' => $view]);
}
emails.teamInvitation is my actual email template.
I compile the view in to a string, and then convert the stylesheets to be inline.
emails.htmlBlank is a view file but all it does is echo out bodyContent. This is necessary because the MailMessage->view method expects a view file, and not an HtmlString.
Do NOT do what is suggested here.
This works. Just remember that you should edit the templates contained in the 'vendor/mail/html' folder AND NOT the contents of the 'vendor/mail/markdown' folder, unless of course you are using markdown instead of the line() / greeting() email building functions
Instead, run the artisan commands and then edit the generated files in your resources folder that you end up with. Never overwrite the vendor files, as if you are working on a local version, then push it to a live server and run composer install, you will not have those changes anymore.
Laravel's inheritance allows you to easily overwrite pre-defined methods and files, so take advantage of that for cleaner version control and better ability to roll back changes to core functionality.
You are making email based on component #component('mail::message') This is a default and this is only one described in documentation. This component does not allow you to modify header. However if you look into it's file,
\vendor\laravel\framework\src\Illuminate\Mail\resources\views\markdown\message.blade.php
you will see that it uses another component #component('mail::layout'),
Just copy content of message.blade.php file into your .blade.php and replace {{ $slot }} with what you had in your file before.
And now you have all the flexibility in your file.
Plus
if you want to modify styles, go to file \config\mail.php
and change markdown section like so
'markdown' => [
'theme' => 'default0',
'paths' => [
resource_path('views/vendor/mail'),
base_path('resources/views/emails/vendor'),
],
],
In this case I replaced default theme with my own \resources\views\emails\vendor\html\themes\default0.css
or, if you don't want customising paths - put your default0.css into /resources/views/vendor/mail/html/themes - it is a default path and you don't need to mention it.
Tested on Laravel 5.7
Laravel 5.8
I found email layout in file -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php.
Like I don't use markdown to send my emails, i need of layout default of laravel (yes, because i want :)).
What i did? I sent for me email for me of reset password, saved the email like html and then copied html to my editor and it ready to changes \o/.

Blade Form without action or url

How to generate a form by Blade template engine without generating action attribute?
I would like to generate a form just for ajax request.
1) If you dont want the form to submit
{{ Form::open(['onsubmit' => 'return false']) }}
2) If you have a ajax function, you can call it like so
{{ Form::open(['onsubmit' => 'yourAjaxFunction(); return false']) }}
3) If you want to include Angular JS directive to the form
{{ Form::open(['ng-submit' => 'submit()', 'onsubmit' => 'return false']) }}
No, it's actually not possible to tell the Form builder to omit the action attribute. Some attributes will be set in any case, and the action-attribute is one of them. Here's the relevant part from the function:
public function open(array $options = array())
{
//....
$attributes['method'] = $this->getMethod($method);
$attributes['action'] = $this->getAction($options);
$attributes['accept-charset'] = 'UTF-8';
//....
return '<form'.$attributes.'>'.$append;
}
Source: https://github.com/illuminate/html/blob/master/FormBuilder.php#L104
But you can easily overwrite it by just passing in a 'url':
Form::open(['url' => '#'])
Note: Overwriting the action like Form::open(['action' => '#']) would throw an error because this specifies the name of a route. url specifies the raw url.

Resources