NewsletterController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\User;
  5. use App\Models\Subscriber;
  6. use Mail;
  7. use App\Mail\EmailManager;
  8. class NewsletterController extends Controller
  9. {
  10. public function index(Request $request)
  11. {
  12. $users = User::all();
  13. $subscribers = Subscriber::all();
  14. return view('backend.marketing.newsletters.index', compact('users', 'subscribers'));
  15. }
  16. public function send(Request $request)
  17. {
  18. if (env('MAIL_USERNAME') != null) {
  19. //sends newsletter to selected users
  20. if ($request->has('user_emails')) {
  21. foreach ($request->user_emails as $key => $email) {
  22. $array['view'] = 'emails.newsletter';
  23. $array['subject'] = $request->subject;
  24. $array['from'] = env('MAIL_FROM_ADDRESS');
  25. $array['content'] = $request->content;
  26. try {
  27. Mail::to($email)->queue(new EmailManager($array));
  28. } catch (\Exception $e) {
  29. //dd($e);
  30. }
  31. }
  32. }
  33. //sends newsletter to subscribers
  34. if ($request->has('subscriber_emails')) {
  35. foreach ($request->subscriber_emails as $key => $email) {
  36. $array['view'] = 'emails.newsletter';
  37. $array['subject'] = $request->subject;
  38. $array['from'] = env('MAIL_FROM_ADDRESS');
  39. $array['content'] = $request->content;
  40. try {
  41. Mail::to($email)->queue(new EmailManager($array));
  42. } catch (\Exception $e) {
  43. //dd($e);
  44. }
  45. }
  46. }
  47. }
  48. else {
  49. flash(translate('Please configure SMTP first'))->error();
  50. return back();
  51. }
  52. flash(translate('Newsletter has been send'))->success();
  53. return redirect()->route('admin.dashboard');
  54. }
  55. public function testEmail(Request $request){
  56. $array['view'] = 'emails.newsletter';
  57. $array['subject'] = "SMTP Test";
  58. $array['from'] = env('MAIL_FROM_ADDRESS');
  59. $array['content'] = "This is a test email.";
  60. try {
  61. Mail::to($request->email)->queue(new EmailManager($array));
  62. } catch (\Exception $e) {
  63. dd($e);
  64. }
  65. flash(translate('An email has been sent.'))->success();
  66. return back();
  67. }
  68. }