
To set the HTML body type in an SMTP Server Client Body, you need to ensure that the email content is sent with the proper Content Type
header which is header is used for specify that the body of the email is HTML rather than plain text (hope you get the point)
SMTP Server Client Body in HTML
So this is how you can do it when sending emails via SMTP in different environments (like, Python, PHP, Java):
1. Python (using smtplib
and email
libraries)
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Create the MIME message
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Test Email with HTML Body'
# HTML body content
html = """
<html>
<body>
<h1>This is a Heading</h1>
<p>This is a <b>paragraph</b> of text.</p>
</body>
</html>
"""
# Attach the HTML body to the email message
msg.attach(MIMEText(html, 'html'))
# Connect to SMTP server and send email
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login('your_email@example.com', 'your_password')
server.sendmail('your_email@example.com', 'recipient@example.com', msg.as_string())
server.quit()
2. PHP (using PHPMailer
)
To send HTML emails with SMTP using PHPMailer, make sure you set the isHTML
method to true
:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('your_email@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Test HTML Email';
$mail->Body = '<h1>This is a Heading</h1><p>This is a <b>paragraph</b> of text.</p>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
3. Java (using JavaMail
API)
In Java, you can use the MimeMultipart
and MimeBodyPart
classes to set the content type to HTML.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) {
String host = "smtp.example.com";
String port = "587";
String user = "your_email@example.com";
String password = "your_password";
String to = "recipient@example.com";
String subject = "Test HTML Email";
String body = "<h1>This is a Heading</h1><p>This is a <b>paragraph</b> of text.</p>";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create the HTML part
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html");
// Create a multipart message
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(htmlPart);
// Set the content of the message
message.setContent(multipart);
// Send the message
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Youtube Video Tutorial:
If It Doesn’t Help Make Sure You:
- Set the Content-Type to HTML: This tells the email client that the body of the email is formatted in HTML.
- MIME Type: Use
MIMEText
for Python,isHTML(true)
for PHPMailer in PHP, orMimeBodyPart
in Java withtext/html
to specify the HTML content. - SMTP Settings: Ensure that your SMTP settings (host, port, username, password) are correctly configured to send the email.
Hope it satisifed your query related but if it doesn’t, feel free to comment us below and our experts will get back to you ASAP!