How can I forward an email in laravel?

Viewed 1112

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?

4 Answers

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