简体   繁体   中英

mail() function doesn't work inside laravel 4

I was testing out my mail configuration. First I did a normal mail() call in a separate .php file. That worked fine. Then I copied the exact same code inside a route callback in Laravel 4 and now it doesn't work. How is that possible?

This is the code that works:

$from_add = "name@your-web-site.com"; 

$to_add = "someone@gmail.com"; //<-- put your yahoo/gmail email address here

$subject = "Test Subject";
$message = "Test Message";

$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";


mail($to_add,$subject,$message,$headers);

The adresses don't matter because I am testing with Test Mail Server Tool

When working with Laravel 4, it's always advisable to use the built-in Mail function .

This has a couple of advantages I wouldn't want to miss:

  • You can easily test mail features by setting the pretend option to true
  • It will allow you to send HTML mails using Blade views
  • You can choose which way your mails should be sent depending on your server config (sendmail, phpMailer)
  • You can queue mails so they will be sent later when there's less server load
  • You can do stuff after mail delivery in a callback function

If you still insist on using the php mail() function, set your driver in app/config/mail.php to "mail".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM