How to Send HTML-Formatted Email

How to send HTML-formatted email?

Setting isBodyHtml to true allows you to use HTML tags in the message body:

msg = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");

msg.IsBodyHtml = true;

How to send HTML-formatted message with standard mail function in PHP

Php Html email

<?php
$to = "abc@gmail.com";
$subject = "PUT_SUBJECT_HERE";
$mail_body = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example: ' . $subject . '
<h1>this is a heading</h1>
</body>
</html>';
//$headers = "From: abc@gmail.com";
//$headers .= "Content-type: text/html";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <abc@gmail.com>' . "\r\n";
mail($to, $subject, $mail_body, $headers);
?>

Enjoy this code

Send HTML in email via PHP

It is pretty simple. Leave the images on the server and send the PHP + CSS to them...

$to = 'bob@example.com';

$subject = 'Website Change Request';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';

mail($to, $subject, $message, $headers);

It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

Here is the link I got the information from... (link)

You will need security though...

Python : Sending html format email through SMTP

I am not sure how to use it doing smtp.ehlo() but alternatively it can be done as under:-


import os
import smtplib
from email.message import EmailMessage #new

EMAIL = f'{login_email}
PASSWORD = f'{login_email_pass}'

message = EmailMessage()
message['Subject'] = f'{program_name} : Launch verification'
message['From'] = EMAIL
message['To'] = EMAIL
message.set_content('This email is sent using python.')
message.add_alternative("""\
<!DOCTYPE html>
<html>
<body>

<h1 style="color:black;text-align:center;font-family:verdana">SERVER 51SV15J LAUNCH</h1>
<p style="color:black;text-align:center;font-family:courier;font-size:120%">Code for confirmation is - <b>51256fd.</b></p>

</body>
</html>
""", subtype = 'html')

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL, PASSWORD)
smtp.send_message(message)

I hope this well help. I haven't tried it but used the code from hereand edited it to meet your case.

How do I send HTML Formatted emails, through the gmail-api for python

After doing a lot of digging around, I started looking in to the python side of the message handling, and noticed that a python object is actually constructing the message to be sent for base64 encoding into the gmail-api message object constructor.

See line 63 from above: message = MIMEText(message_text)

The one trick that finally worked for me, after all the attempts to modify the header values and payload dict (which is a member of the message object), was to set (line 63):

  • message = MIMEText(message_text, 'html') <-- add the 'html' as the second parameter of the MIMEText object constructor

The default code supplied by Google for their gmail API only tells you how to send plain text emails, but they hide how they're doing that.
ala...
message = MIMEText(message_text)

I had to look up the python class email.mime.text.MIMEText object.
That's where you'll see this definition of the constructor for the MIMEText object:

  • class email.mime.text.MIMEText(_text[, _subtype[, _charset]])
    We want to explicitly pass it a value to the _subtype. In this case, we want to pass: 'html' as the _subtype.

Now, you won't have anymore unexpected word wrapping applied to your messages by Google, or the Python mime.text.MIMEText object


The Fixed Code

def create_message(sender, to, cc, subject, message_text):
"""Create a message for an email.

Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.

Returns:
An object containing a base64url encoded email object.
"""
print(sender + ', ' + to + ', ' + subject + ', ' + message_text)
message = MIMEText(message_text,'html')
message['to'] = to
message['from'] = sender
message['subject'] = subject
message['cc'] = cc
pprint(message)
return {'raw': base64.urlsafe_b64encode(message.as_string())}

Shell Script : Send html formatted email while inside an array

You need an empty line between the email headers and the body; and you need to add HTML formatting to the table in order for it to render correctly. Something like this, perhaps:

# -F ',' -- sample is comma-separated, not tab-separated
awk -F ',' '{ a[$4] = a[$4] ORS "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4 "</td></tr" }
# ^^ Notice the addition of HTML
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v " $4
print "From: static.name@domain.com" | cmd
# Send to user, not to $4
print "To: " user | cmd
print "Cc: static.name@domain.com" | cmd
# What should $2 expand to here?
# Maybe collect that in a separate array
print "Subject: Some text here " $2 " Some more text"| cmd
print "MIME-Version: 1.0" | cmd
print "Content-Type: text/html" | cmd
# Not really useful, but why not
print "Content-Disposition: inline" | cmd
# Add an empty line
print "" | cmd
print "<font face=Calibri><font size=2>Some text here<br><br>"| cmd
print "<table><tbody>" | cmd
print substr(a[user], 2) | cmd
print "</tbody></table>" | cmd
close(cmd) } }' myfile

I kept this simple to highlight the general structure. You can add more embellished HTML formatting to your heart's content once you understand how this works.



Related Topics



Leave a reply



Submit