fylzero's avatar
Level 67

Can I send an email without a database / user from the database?

Is it possible to use Laravel's Mail:to without actually passing it a database user or even having a database?

I just have a super small site I want to build out a contact form on. Wondering if there is a way to do this or if I'm doing something wrong before just breaking down and using SQLite.

My SendEmail.php Controller.

<?php

namespace App\Http\Controllers;

use App\Mail\ContactForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class SendEmail extends Controller
{

    public function send() {
        Mail::to(
            [
                'name' => 'Joe Schmoe',    
                'address' => '[email protected]'
            ]
        )->send(new ContactForm());
    }

}

When I try the above code, I get this...

Swift_RfcComplianceException
Address in mailbox given [Joe Schmoe] does not comply with RFC 2822, 3.6.2.
https://mywebsite.test/testemail
Hide solutions
Database name seems incorrect
You're using the default database name laravel. This database does not exist.

Edit the .env file and use the correct database name in the DB_DATABASE key

READ MORE
Database: Getting Started docs
0 likes
4 replies
fylzero's avatar
Level 67

@rodrigo.pedra I actually did try that both ways, using email instead of address and it comes back with the same message.

1 like
fylzero's avatar
fylzero
OP
Best Answer
Level 67

Well... this isn't the answer to what I asked, but it is the answer to the problem...

https://laravel.com/docs/6.x/mail#mail-and-local-development

In this section it shows how you can create a config area for the to address.

After doing that I can simply use...

Mail::send(new ContactForm());

That is all I really wanted to do, was to have the contact form always send to one user.

1 like
rodrigo.pedra's avatar

Great you find a solution, but just to add to your initial question, I use the Mail façade to send to a e-mail like this:

    public function __invoke(SendEmailRequest $request, Estimate $estimate)
    {
        Mail::to($request->input('email'))->send(new SendEstimate($estimate));

        return Response::noContent();
    }

And it works.

A thought it would work with an array because of the code int the Mailable@normalizeRecipient from the core.

But...

After inspecting the core code, the Mail façade uses the `Maile class to build the e-mail. And that class (after proxing thorugh the `PendingMai class) call the `Mailable@t with only one argument.

So for setting both name and email you should use an array of arrays:

Mail::to([
    [ 'name' => 'Joe Schmoe', 'email' => '[email protected]' ],
])->send(new ContactForm());

And it should work.

Please or to participate in this conversation.