• Javascript
  • Python
  • Go

Sending HTML mails with CSS using PHPMailer

HTML (Hypertext Markup Language) is the standard language used for creating web pages. It allows for the inclusion of tags that define the s...

HTML (Hypertext Markup Language) is the standard language used for creating web pages. It allows for the inclusion of tags that define the structure and content of a webpage. These tags not only help in organizing the content but also allow for the addition of formatting and styling to make the webpage visually appealing. HTML is not only limited to creating web pages, but it can also be used to send emails. In this article, we will explore how to send HTML mails with CSS using PHPMailer.

PHPMailer is a popular open-source library used for sending emails from PHP applications. It provides a simple and reliable way to send emails with support for HTML formatting and attachments. Using PHPMailer, we can easily send HTML emails with CSS to make them more visually appealing.

To begin with, we need to download the latest version of PHPMailer from their official website. Once downloaded, we can extract the files and place them in our project directory. Next, we need to create a new PHP file and include the necessary files from the PHPMailer library.

```php

<?php

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';

require 'path/to/PHPMailer/src/PHPMailer.php';

require 'path/to/PHPMailer/src/SMTP.php';

```

Now, we can create a new instance of the PHPMailer class and set the necessary parameters for sending an email.

```php

<?php

$mail = new PHPMailer(TRUE);

// Set the mail server details

$mail->IsSMTP();

$mail->Host = "smtp.example.com";

$mail->SMTPAuth = TRUE;

$mail->Username = "your_email@example.com";

$mail->Password = "your_email_password";

$mail->SMTPSecure = "tls";

$mail->Port = 587;

// Set the sender and recipient details

$mail->SetFrom('sender@example.com', 'Sender Name');

$mail->AddAddress('recipient@example.com', 'Recipient Name');

// Set the subject and body of the email

$mail->Subject = 'HTML email with CSS using PHPMailer';

$mail->Body = '<h1 style="color: #3498db;">Hello World!</h1><p style="color: #2ecc71;">This is an HTML email with CSS using PHPMailer.</p>';

// Enable HTML formatting

$mail->IsHTML(TRUE);

// Send the email

if($mail->Send()) {

echo "Email sent successfully!";

} else {

echo "Email sending failed: " . $mail->ErrorInfo;

}

```

In the above code, we first set the details for the mail server, including the host, username, password, and port. Next, we set the sender and recipient details and provide a subject and body for the email. Here, we have used HTML tags to format the content of the email. We have also enabled HTML formatting using the `IsHTML()` method.

Once these details are set, we can use the `Send()` method to send the email. If the email is sent successfully, we will get a success message, otherwise, an error message will be displayed.

Apart from basic HTML formatting, PHPMailer also allows us to use CSS to style our HTML emails. We can use inline CSS or include a separate CSS file to style our email. Let's take a look at an example of using a separate CSS file to style our email.

Related Articles

Autosizing Textareas with Prototype

Textareas are a fundamental element in web development, allowing users to input and edit large amounts of text. However, as the size of the ...

btaining the Height of a Table Row

When designing a website, it is important to pay attention to the layout and formatting of your content. One crucial element in creating a w...

IE7 Margin-Bottom Bug in HTML/CSS

The IE7 Margin-Bottom Bug in HTML/CSS has been a long-standing issue for web developers. It is a bug that affects the layout of websites on ...