# Contact

IMPORTANT

  • Since version 9.35.x, Laravel uses a new syntax for sending emails (opens new window)
  • If you're using an older version, first update to the latest version with composer update
  • We'll use the integrated MailHog service to send (fake) mails in a development and testing environments
  • You can access the MailHog mailbox in the browser at http://localhost:8025 (or click on the @-icon in the footer menu)

Mailhog

  • The contact page contains some static content and a (dynamic) contact form
  • In web.php, we keep the route as a view route to resources/views/contact.blade.php and embed the Livewire component in that view

# Preparation

# Create a ContactForm component

  • Create a new Livewire ContactForm component with php artisan make:livewire ContactForm
    • app/Http/Livewire/Admin/ContactForm.php (the component class)
    • resources/views/livewire/admin/contact-form.blade.php (the component view)
  • Refactor the contact.blade.php view and embed the ContactForm component in it

# Basic scaffolding

  • Open app/Http/Livewire/ContactForm.php and resources/views/livewire/contact-form.php

# Real-time form validation

# Disable the submit button

  • Remember that we modified the Jetstream button component a few chapters ago
    We added a disabled and the color attributes to the component
  • We can use the disabled attribute on the submit button to disable the button when the validation is not successful

# Sending email

  • After configuring our mail settings, we set up a mail template, pass some data to this template and send the mail itself

# configure mail settings

  • The mail is already set up for you in the .env file
  • The .env file contains the following settings for the mail service:
    • Line 2: replace mailhog with localhost
    • Line 7: replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"

 




 


MAIL_MAILER=smtp
MAIL_HOST=localhost                        # replace 'mailhog' with 'localhost'
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="info@vinyl_shop.test"   # replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"
MAIL_FROM_NAME="${APP_NAME}"
1
2
3
4
5
6
7
8

WARNINGS

  • If your variable contains spaces, surround it with quotes. E.g. MAIL_FROM_NAME="John Doe"
  • If you want a REAL email address to test certain functionalities, use Mailinator (opens new window) e.g. john.doe@mailinator.com.
    Emails sent to Mailinator are public but temporary and will disappear after a few hours.

# Create mailable

  • In Laravel, each type of email sent by your application is represented as a mailable (opens new window) class
  • Create a new mailable class with an associated view with the command:
    php artisan make:mail ContactMail --markdown=emails.contact-mail
    • The ContactMail mailable class is located in the app/Mail directory
    • The contact-mail view is located in the resources/views/emails directory
    • The --markdown option will create a markdown based view (opens new window)
      (Besides a Markdown-based mail template, used in this site, you can also opt for HTML or text-based templates for the mail)
php artisan make:mail ContactMail --markdown=emails.contact
1

# Send your first email

  • Send an email inside the sendMail() method from our ContactForm component and open MailHog to see the result

REMARKS

  • You are not limited to theto() recipients. You can also add the cc() and bcc() recipients
  • All these methodes can handle multiple recipients with an array of recipients
  • The second parameter (the name) of the new Address() method is optional
  • If you don't need the name of the recipient, you can omit the new Address() and just pass the email address as a string,
    e.g. $to = 'john.doe@exmpale.com'; is the same as $to = new Address('john.doe@exmpale.com');
  • Example:


 
 
 
 
 
 


// send email
$template = new ContactMail();
$to_1 = new Address($this->email, $this->name);     // email + name
$to_2 = new Address('user2@example.com');           // email only
$to_3 = 'user3@example.com';                        // email only (same as above)
Mail::to([$to_1, $to_2, $to_3])
    ->cc(['user3@example.com', 'user4@example.com'])
    ->bcc(['user5@example.com', 'user6@example.com'])
    ->send($template);
1
2
3
4
5
6
7
8
9
  • All other mail settings (subject, body, from, replayTo, ...) can be configured in the ContactMail class

# Pass data to the mailable and update the mail template

  • Add the data you want to use in the email as parameters to new ContactMail() to inject it into the constructor of mailable class and make it available in the mail template
    1. ContactForm component: add an array with the data you want to inject into the constructor of the mailable (and use in the mail template)
    2. ContactMail mailable: add a public property (e.g. $data) and assign the injected data via te constructor to it
    3. Update the Contact view and use the data from the mailable in the mail template

# Extra features

# Show error bag (optional)

  • Instead of showing each error message individually, we can show all errors in one single error block
  • To reuse the error block on different pages, we can best make a component for it
  • Create a new errorbag component inside the component's folder: resources/views/components/tmk/errorbag.blade.php
  • The component don't need extra properties or a slot because all the logic is inside the component itself

# EXERCISE: Add a contact field to the form

  • Add a dropdown list to the form in order to choose a specific contact
  • The list contains four options: Select a contact (value=""), Info, Billing and Support
  • This is a required field, so add it to your validation
  • Depending on the choice of the user, the from (and carbon copy) address should be adjusted:
    • info@thevinylshop.com with the name The Vinyl Shop - Info
    • billing@thevinylshop.com with the name The Vinyl Shop - Billing
    • or support@thevinylshop.com with the name The Vinyl Shop - Support
Last Updated: 5/4/2023, 2:27:29 PM