How to connect css to php

    It is not possible to reference CSS stylesheets in HTML-based email, as you commonly would in a web-based application by using: .

    Depending on your emails, this might be quite problematic. Luckily, there’s a way to overcome this limitation, which you'll learn in this tutorial.

    Prerequisites

    To follow along with this article, you’ll need the following:

    • Basic knowledge of PHP.
    • PHP 7.4 [ideally version 8], with the zip extension.
    • Composer installed globally.
    • A SendGrid account.

    Using SendGrid with Composer

    In order to use SendGrid to send emails in PHP, you’ll need to install the SendGrid PHP API Library. Do this by running the command below.

    composer require sendgrid/sendgrid
    

    Retrieving a SendGrid API Key and Valid Sender identity

    You’ll need a SendGrid API key and a valid sender identity in order to send emails through PHP using SendGrid.

    First, to retrieve your SendGrid API key:

    1. Log into your SendGrid account.
    2. From the left side menu, click on Settings, then on API Keys.
    3. Click the Create API Key button on the top-right of the page to create a new API key. A new screen will appear.
    4. Inside the API Key Name field, enter a name for your API key.
    5. In the API Key Permissions section, select either Full Access or Restricted Access. If you decide to restrict access, be sure that the key has access to the Mail Send option.
    6. Click the Create & View button. You will be presented with your SendGrid API key.
    7. Take note of the SendGrid API key you’ve been provided.

    Create a verified sender identity

    To do that, log in to your SendGrid account, then going to Settings in the navigation bar. When there, click Sender Authentication. Then, proceed with Single Sender Verification by selecting Get Started under Verify an Address.

    You will be taken to the Single Sender Verification page. Under Create your First Sender, click Create New Sender to load a form modal.

    Fill in all of the fields in the form modal and then click Create.

    Form Fields:

    • From Name: This is a user-friendly name that is displayed to your recipient when they receive their email.
    • From Email Address: This the email address that sends the email. SendGrid sends a verification email to the address you enter in this field. If you do not receive it after a short time, please refer back to the Sender settings and confirm that the "From" email is a valid email address.
    • Reply To: If the recipient replies to the email, the reply will go to this email address.
    • Company Address, City, State, Zip Code, Country: Your business address.
    • Nickname: A label for your sender identity to help you identify it more quickly, later. This label is not visible to your recipients.

    Check the inbox of the email address that you entered, and click the link in the email to complete verification of the Sender email address.

    If, for any reason, you need to resend the verification email, click the action menu on the Single Sender Verification page, and select Resend Verification. SendGrid will deliver a new confirmation email to the address you are attempting to verify.

    If you have an authenticated domain and your sender email address matches that domain exactly, your sender identity will be verified automatically.

    You will now see a page confirming the verification of your address. Click Return to Single Sender Verification to add more addresses or make any changes to the address you just verified.

    Reference Inline stylesheets

    Now that you have everything set up, the limitation of not being able to reference stylesheets through email HTML can be solved using the file_get_contents[] function. This function gets the contents of files and stores them in a string.

    Before I demonstrate how to reference large CSS stylesheets using this method, to see how referencing stylesheets in email HTML works, create a new PHP file, named index.php, and paste the code below into the file.

Bài mới nhất

Chủ Đề