Home > Software engineering >  how to receive email through laravel?
how to receive email through laravel?

Time:01-03

i know how to send email but i want to know how to receive email after submitting the form from many user?

this is .env file

    MAIL_DRIVER=imap
    MAIL_HOST=imap.gmail.com
    MAIL_PORT=993
    [email protected]
    MAIL_PASSWORD=mypassword
    MAIL_ENCRYPTION=tls
    [email protected]
    MAIL_FROM_NAME="${APP_NAME}"

this is controller

    <?php
        namespace App\Http\Controllers;
        use Illuminate\Http\Request;
        use Illuminate\Support\Facades\Mail;
        use App\Mail\SendMail;

        class SendEmailController extends Controller
        {
            public function index()
            {
                return view('send_email');
            }

            function send(Request $request)
            {
                 $this->validate($request, [
                    'name'     =>  'required',
                    'email'  =>  'required|email',
                    'message' =>  'required'
                 ]);
         
                 $data = array(
                     'name'      =>  $request->name,
                     'message'   =>   $request->message
                 );

                 Mail::to('[email protected]')->send(
                     new SendMail($data));
                 return back()->with(
                      'success', 
                      'Thanks for contacting us!');
            }
    }

?>

sendMail.php

    <?php
        namespace App\Mail;

        use Illuminate\Bus\Queueable;
        use Illuminate\Mail\Mailable;
        use Illuminate\Queue\SerializesModels;
        use Illuminate\Contracts\Queue\ShouldQueue;

        class SendMail extends Mailable
        {
           use Queueable, SerializesModels;
           public $data;
           /**
            * Create a new message instance.
            *
            * @return void
            */
           public function __construct($data)
           {
               $this->data = $data;
           }

           /**
            * Build the message.
            *
            * @return $this
            */
           public function build()
           {
               return $this->subject('form')
                      ->view('dynamic_email_template')
                    -  >with('data', $this->data);
           }
     }

?>

When I try to submit, it says

Expected response code 220

...but got an empty response.

Also how to write code for sender email from my email in controller?

What is the error?

Do I need to change some setting in gmail or mail.php file?

CodePudding user response:

When you are sending email, the user does something on the website, which runs some PHP code, which then accesses an SMTP server to send email. Everything starts at the website.

If you want to do something in response to incoming mail, then the activity starts (on your system) when the SMTP server receives the email. It isn't triggered by the website.

You need to turn the problem around and address it from the email side and not the website side.

A basic email set up goes something like this:

  1. Email arrives at SMTP server
  2. SMTP server stores that email

And then later, an IMAP server can be used to read data from that store.

A slightly more complex approach would look like:

  1. Email arrives at SMTP server
  2. SMTP server passes email through a spam checker
  3. SMTP server stores that email or discards it depending on the result of the spam checker

You can insert extra steps there, such as calling a PHP program with the content of the email. A common way to do this would be to use procmail. If you use procmail or something else depends on your particular email server set up. See this question for a procmail recipe to run a PHP program.


Another approach (which is much less efficient and not so quick to trigger) would be the poll the IMAP server.

This requires that the PHP be triggered on a schedule rather than from the website. e.g. instead of using procmail you might use cron.

You would need to access the mailbox and then determine which emails in it you haven't dealt with yet.

PHP has an IMAP extension that would allow you to scan the mailbox and read mail from it.

CodePudding user response:

There is no email driver named imap. It must be smtp. Example gmail settings;

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl

You can find available mail drivers in config/mail.php

  • Related