title: Laravel 5.4 send mail with attachment giving error - using Mailtrap.io
body: <p>I am trying to send email with an attachment in a developing environment - using <code>mailtrap</code> for the same. This attachment can be of any file type. But, I always end up receiving the error:</p>
  
  <blockquote>
  <pre><code>Expected response code 250 but got code "502", 
  with message "502 5.5.2 Error: command not recognized
  </code></pre>
  </blockquote>
  
  <p>I don't know where and what mistake I am doing, but I am failing to achieve the desired result and that is send the mail with an attachment.</p>
  
  <p>The attachment file resides inside the <code>public/email-attachments/</code> folder</p>
  
  <p>Here's the source code that I have tried so far:</p>
  
  <pre><code>/**
   * Build the message.
   *
   * @return $this
   */
  public function build()
  {
      $mailContent = cache('allEmailContents')-&gt;first();
  
      return $this-&gt;from('welcome@dprefumry.com')
                  -&gt;subject($mailContent-&gt;subject)
                  -&gt;view('emails.send')
                  -&gt;attach($mailContent-&gt;attachment, [
                      'as' =&gt; str_slug($mailContent-&gt;subject),
                      'mime' =&gt; File::mimeType($mailContent-&gt;attachment)
                  ])
                  -&gt;with(['mailContent' =&gt; $mailContent]);
  }
  </code></pre>
  
  <p>And in the <code>routes/web.php</code> file:</p>
  
  <pre><code>Route::get('/', function() {
      Mail::to('maddy@example.com')
              -&gt;send(new SendWelcomeEmail());
  
      return view('welcome');
  });
  </code></pre>
  
  <p>I have also tried the old way of sending mail with no success:</p>
  
  <pre><code>Route::get('/', function() {
      $mailContent = cache('allEmailContents')-&gt;first();
      $data = $mailContent-&gt;toArray();
  
      Mail::send('emails.send', $data, function($mail) use ($mailContent, $data) {    
          $mail-&gt;to('maddy@example.com')-&gt;subject($mailContent-&gt;subject);
          $mail-&gt;from('welcome@example.com');
          $mail-&gt;attach($mailContent-&gt;attachment, [
              'as' =&gt; str_slug($mailContent-&gt;subject),
              'mime' =&gt; File::mimeType($mailContent-&gt;attachment)
          ])
      });
  
      return view('welcome');
  });
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Here's the <code>.env</code> file:</p>
  
  <pre><code>MAIL_DRIVER=smtp
  MAIL_HOST=smtp.mailtrap.io
  MAIL_PORT=2525
  MAIL_USERNAME=49xxxxxxx70
  MAIL_PASSWORD=1fxxxxxxxxxx1e
  MAIL_ENCRYPTION=null
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>I was just playing around with the uploading of files and storing it.. And then calling that file as an attachment and came up with the following conclusion:</p>
  
  <p>1st. If I upload the file &lt; 5 MB, the file is sent as an attachment without any issue.</p>
  
  <p>2nd. If I upload the file > 5 MB, I get the following error:</p>
  
  <pre><code>Expected response code 250 but got code "552",
  with message "552 5.7.0 Message exceeded max message size of 5242880 bytes"
  </code></pre>
  
  <p><strong>Just in case, if reference needed for <code>php.ini</code></strong></p>
  
  <p>I have the following config in <code>php.ini</code></p>
  
  <pre><code>post_max_size = 20000M
  upload_max_filesize = 10000M
  max_file_uploads = 20
  max_execution_time = 600000
  </code></pre>
  
  <hr>
  
  <p>Kindly help me out with this. Any help is highly appreciated.</p>