28

I need to send an email via the mail() PHP function. I've read somewhere that I have to change driver parameter in config/mail.php to sendmail.

By default, it looks like this:

'driver' => env('MAIL_DRIVER', 'smtp'),

Now, it looks like this:

'driver' => 'sendmail',

Also tried this:

'driver' => 'mail',

But still, the mail() function doesn't work. What do I need to change?

Sven
  • 1,450
  • 3
  • 33
  • 58
Oleg
  • 383
  • 1
  • 3
  • 5

4 Answers4

69

To do the same as mail() PHP function does, in most cases you should configure Laravel in the following way:

Use sendmail, at .env:

MAIL_DRIVER=sendmail

If you are using Larvel 7 or above

MAIL_MAILER=sendmail

Laravel 7 replaced MAIL_DRIVER by MAIL_MAILER

Host, user, password, port and encryption are not needed.

At this point, you may check if it already works, but sometimes the next step is also needed.

Set a new .env option in config/mail.php:

'sendmail' => env('MAIL_SENDMAIL', '/usr/sbin/sendmail -bs')

Set the sendmail path in .env. You can check sendmail_path at phpinfo(), but it's usually this one:

MAIL_SENDMAIL='/usr/sbin/sendmail -t -i'
STA
  • 30,729
  • 8
  • 45
  • 59
Max Oriola
  • 1,296
  • 12
  • 8
  • This, here, is the correct answer. Especially that tidbit about setting the options to "sendmail -t -i" in my case. I hate to have to use this basic php mail function but in one of my projects I'm forced to... Thank you. – Chris Mar 13 '18 at 19:36
  • 3
    needs to be `'sendmail' => env('MAIL_SENDMAIL', '/usr/sbin/sendmail -bs'),` in `config/mail.php` if you're going to edit it there. Don't forget the quotes and final comma – Chris Mar 13 '18 at 20:53
  • Oh! Thanks Chris, I've just fixed it in my answer. – Max Oriola Mar 16 '18 at 08:41
  • 1
    Note that the `sendmail`driver requires that PHP's `proc_open()` function is not disabled for security reasons using `disabled_functions` in ´php.ini`. – Christopher K. Oct 25 '18 at 19:06
  • 3
    For Laravel 7 and up, use `MAIL_MAILER=sendmail` – Erin Mar 25 '21 at 11:14
  • 1
    Have anyone a solution for using sendmail on windows machines? – SiL3NC3 Feb 23 '22 at 08:03
  • Connection to localhost:25 Timed Out – Soubhagya Kumar Barik Jun 16 '22 at 12:26
  • confirming my `sendmail_path` in phpinfo was the solution for me. should be '/usr/sbin/sendmail -t -i' instead of '/usr/sbin/sendmail -bs'. Thanks @MaxOriola – emmaakachukwu Nov 22 '22 at 14:30
29

To use the email server running on localhost, your .env file should look like this (The PHP mail function doesn't need a username or a password)

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null

Then, update the configuration cache:

php artisan config:cache
Ikbel
  • 7,721
  • 3
  • 34
  • 45
  • 3
    Thank you very very much, friend!!! I have searched this answer so long time and this is bad, that laravel official documentation doesn't give this info. You are best, @lbel! – Vaha Mar 02 '18 at 08:54
  • 1
    Thank you, just like @Vaha I couldn't find this anywhere. I've been searching for hours and nowhere is it said you should use it like this. – Stefan Teunissen Dec 30 '18 at 13:46
  • 1
    The php artisan config:cache part is vital, don't forget it as I did! – AdamJones Sep 10 '19 at 13:36
  • 1
    `php artisan config:clear` is the actual command that should be run I think – Arvind K. Oct 11 '19 at 06:20
  • 1
    For a localhost you can just setup like this `MAIL_DRIVER=log` and it will print all mail in the log file. Very useful! – Mārcis P Mar 22 '20 at 22:35
  • 1
    Note that from Laravel 7, use `MAIL_MAILER` in place of `MAIL_DRIVER` – Erin Mar 25 '21 at 11:00
  • username and passwords what we will add here is they will be empty? by same above settings, it gives me a success message but email doesn't receive – Muhammad Ikram UL Mustafa Sep 10 '21 at 06:17
  • Beautiful! this is a very useful and practical answer for testing and using in small projects. – jgarcias Aug 29 '23 at 00:48
-3

You have to set your mail Configuration in .env file. Here you have to set all your mail driver and all details. Plase see this documentation https://laravel.com/docs/5.0/mail

  • The link you posted explains how to configure a bunch of API drivers, but it doesn't explain how to configure sendmail, which is likely the reason OP posted this question. – Hayden Bech Dec 08 '18 at 01:14
-32

You can set your mail configuration .env file like

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD="password"
MAIL_ENCRYPTION=tls

also set configuration in config/mail.php like:

'from' => ['address' => 'youremail@gmail.com', 'name' => 'Test'],

then you can clear the cache:

php artisan config:cache
php artisan cache:clear
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • 10
    These are not the correct settings for using PHP's mail() function. That function does not need username and email. These settings are used if you want to send the emails from another server like Gmail. – wmac Jun 16 '17 at 07:39
  • 3
    @wmacYes- Thank you! I have exactly the same problem as the OP, it's unbelievable how much time I've wasted with this now, and how little information there is on this. **How do you use PHP's mail() function with Laravel 5.5**? Is it possible? *That* is the question. I keep getting told to change to `MAIL_DRIVER=smtp` and enter my gmail account credentials but I don't want that! I have downvoted this as it doesn't answer the question. – Inigo Oct 04 '17 at 12:56
  • 1
    Link to thread where I am essentially asking the same thing: https://stackoverflow.com/a/46550390/521392 – Inigo Oct 04 '17 at 13:06
  • I had everything set up with my own mailserver (using smtp) and it wasn't working. Until I set `MAIL_ENCRYPTION`'s value to `tls` instead of the default value `null`... – Derk Jan Speelman Jan 11 '18 at 14:03
  • 3
    this is not for php's sendmail configuration. it's an SMTP. – Ashish Patel Aug 24 '18 at 11:32
  • This may solve the problem, but is a false answer to the question because this way Laravel will not use php's built-in mail function as was asked. – jacmkno Sep 17 '19 at 21:41
  • This answer should not have been accepted. It is not a correct answer to this question, it's misleading. – Mcload Jan 28 '20 at 17:16