Laravel 5.4 send mail with attachment giving error - using Mailtrap.io
Asked Answered
G

0

7

I am trying to send email with an attachment in a developing environment - using mailtrap for the same. This attachment can be of any file type. But, I always end up receiving the error:

Expected response code 250 but got code "502", 
with message "502 5.5.2 Error: command not recognized

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.

The attachment file resides inside the public/email-attachments/ folder

Here's the source code that I have tried so far:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $mailContent = cache('allEmailContents')->first();

    return $this->from('[email protected]')
                ->subject($mailContent->subject)
                ->view('emails.send')
                ->attach($mailContent->attachment, [
                    'as' => str_slug($mailContent->subject),
                    'mime' => File::mimeType($mailContent->attachment)
                ])
                ->with(['mailContent' => $mailContent]);
}

And in the routes/web.php file:

Route::get('/', function() {
    Mail::to('[email protected]')
            ->send(new SendWelcomeEmail());

    return view('welcome');
});

I have also tried the old way of sending mail with no success:

Route::get('/', function() {
    $mailContent = cache('allEmailContents')->first();
    $data = $mailContent->toArray();

    Mail::send('emails.send', $data, function($mail) use ($mailContent, $data) {    
        $mail->to('[email protected]')->subject($mailContent->subject);
        $mail->from('[email protected]');
        $mail->attach($mailContent->attachment, [
            'as' => str_slug($mailContent->subject),
            'mime' => File::mimeType($mailContent->attachment)
        ])
    });

    return view('welcome');
});

UPDATE 1:

Here's the .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=49xxxxxxx70
MAIL_PASSWORD=1fxxxxxxxxxx1e
MAIL_ENCRYPTION=null

UPDATE 2:

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:

1st. If I upload the file < 5 MB, the file is sent as an attachment without any issue.

2nd. If I upload the file > 5 MB, I get the following error:

Expected response code 250 but got code "552",
with message "552 5.7.0 Message exceeded max message size of 5242880 bytes"

Just in case, if reference needed for php.ini

I have the following config in php.ini

post_max_size = 20000M
upload_max_filesize = 10000M
max_file_uploads = 20
max_execution_time = 600000

Kindly help me out with this. Any help is highly appreciated.

Garrett answered 10/6, 2017 at 6:59 Comment(2)
This has nothing to do with your local setup, mailtrap simply refuses messages with a total size over 5M. Everything works as it should be on your end.Sicard
@SaiyanPrince It's like Robert said. From mailtrap FAQ: The max size of an email including attachments is 5MB. Exceeding this limit causes the error: '552 5.3.4 Error: message too big'Hehre

© 2022 - 2024 — McMap. All rights reserved.