3

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?

Aziz Ahmed
  • 81
  • 12

4 Answers4

1

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 !!}

Jeremy
  • 179
  • 4
  • 13
0

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">
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • I retrieved the email from my mailbox using imap and I want to forward the emails to other address with smtp. I think your answer is not what I want. If it is can you please describe me how can I do that? – Aziz Ahmed Jun 14 '20 at 05:27
  • @AzizAhmed you have to remake the email, the auto embed makes easier move the attachments references to blobs to attachment them into the email (and not the hard references) – Benjamin RD Jun 14 '20 at 05:30
0

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

RYOK
  • 473
  • 7
  • 23
  • you might also get your specific email inside your sending function so that you don't have to pass $oMessage to it – RYOK Jun 15 '20 at 00:42
  • the 'emails.message' is a view? Am I right? If I am right can you please share a view sample for this? – Aziz Ahmed Jun 16 '20 at 14:30
  • yes thats true i will share from Medium website you may want to take a look at the link which i will share as well – RYOK Jun 16 '20 at 17:28
0
Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext], 
//           ^^^^

Also replace auto-escaped block {{ }} with unescaped {!! !!} in the template:

<p> {!! $text !!} </p>
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Shohanul Alam
  • 145
  • 1
  • 12