- Send Text and HTML Email using PHP
- Send Text Email with PHP
- Send HTML email in PHP
- Thanks you for joining with us!
- Send Email with HTML Content from File
- Conclusion
- Sending Nice HTML Email with PHP
- Send HTML Email in PHP via SMTP
- Using SMTP for Sending Email
- How to Send HTML Email in PHP Using Gmail SMTP
- php.ini
- sendmail.ini
- PHP Code for Sending HTML Email
- Hi! We are glad to announce the Christmas contest winners.
Send Text and HTML Email using PHP
Sending email from the script is a very useful and most used feature for the web application. The email functionality is used for many purposes – sending a welcome email on account registration, sending newsletter, contact or feedback form, etc. By sending an email from the script, the mail is sent dynamically to the recipient without manual interaction.
If your web application is built with PHP, the email can be sent easily from the script using a predefined function. The PHP mail() function is the easiest way to send an email from the code level. You can easily send text and HTML emails using the built-in mail() function in PHP. Generally, the text message is sent via email to the recipient. If you want to send a nice formatted email, HTML content needs to be added to the message body of the email. In this tutorial, we will show you how to send text and HTML emails using PHP mail() function.
Send Text Email with PHP
The following code sends an email from the script with text message content using PHP.
Use the PHP mail() function and pass the required parameters.
- to – Recipient email address.
- subject – Subject of the email.
- message – Message to be sent.
- headers – From, Cc, Bcc, Content-type headers.
$to = 'user@example.com';
$from = 'sender@email.com';
$fromName = 'Sender_Name';
$subject = "Send Text Email with PHP by CodexWorld";
$message = "First line of text\nSecond line of text";
// Additional headers
$headers = 'From: '.$fromName.'.$from.'>';
// Send email
if(mail($to, $subject, $message, $headers)) <
echo 'Email has sent successfully.';
>else <
echo 'Email sending failed.';
>
Send HTML email in PHP
The following code sends an email from the script with HTML message content using PHP.
- Use the PHP mail() function and provide the required parameters.
- to – Recipient email address.
- subject – Subject of the email.
- message – Message to be sent.
- headers – From, Cc, Bcc, Content-type headers.
$to = 'user@example.com';
$from = 'sender@example.com';
$fromName = 'SenderName';
$subject = "Send HTML Email in PHP by CodexWorld";
$htmlContent = '
Thanks you for joining with us!
Name: CodexWorld
Email: contact@codexworld.com
Website: www.codexworld.com
';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: '.$fromName.'.$from.'>' . "\r\n";
$headers .= 'Cc: welcome@example.com' . "\r\n";
$headers .= 'Bcc: welcome2@example.com' . "\r\n";
// Send email
if(mail($to, $subject, $htmlContent, $headers)) <
echo 'Email has sent successfully.';
>else <
echo 'Email sending failed.';
>Send Email with HTML Content from File
If you want to send email with large HTML content, it’s always a good idea to separate message content in HTML file. This functionality is very useful when you want to use the dynamic email template for sending mail.
- Put the HTML content of the message in an HTML file ( email_template.html ).
- Use file_get_contents() function to retrieve HTML content from the file.
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.html");Conclusion
This example script helps you to send HTML email with Cc or Bcc address in PHP. You can use this code to send multiple emails dynamically from the script using PHP. Specify the additional headers to send email with attachment in PHP.
Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request
If you have any questions about this script, submit it to our QA community — Ask Question
Sending Nice HTML Email with PHP
This is going to be a continuation of the Website Change Request Form demo we’ve been using around here for a while. If you need to catch up, first I talked about it, then I built it, then I screencasted it, then I secured it. Throughout all of this, the end result has been a boring text-only email that gets sent to a single email address. We’re going to improve that output, and make the email into a nicer looking HTML-formatted email.
It’s Not Much Different Than Text Email
mail($to, $subject, $message, $headers);
The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML. In fact, the headers area gives us the opportunity to do lots of important email functions. This is where we can set the From: and Reply To: settings if need be, as well as CC and BCC other recipients (Hey, a checkbox for CC’ing yourself would be a cool feature to add!). Here is the code used for the new and improved HTML-Sendin’ Website Change Request Form:
$to = '[email protected]'; $subject = 'Website Change Request'; $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; $headers . /cdn-cgi/l/email-protection" data-cfemail="44373137252a04213c25293428216a272b29">[email protected]\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
The message parameter (a big string we pass to the mail function with the body of our email), can now have HTML tags in it. For example:
$message = ''; $message .= '
Hello, World!
'; $message .= '';$message = ''; $message .= '
'; $message .= '
'; $message .= "
"; $message .= ""; "; $message .= "Name: " . strip_tags($_POST['req-name']) . " "; $message .= "Email: " . strip_tags($_POST['req-email']) . " "; $message .= "Type of Change: " . strip_tags($_POST['typeOfChange']) . " "; $message .= "Urgency: " . strip_tags($_POST['urgency']) . " "; $addURLS = $_POST['addURLS']; if (($addURLS) != '') < $message .= "URL To Change (main): " . $_POST['URL-main'] . " "; > $curText = htmlentities($_POST['curText']); if (($curText) != '') < $message .= "URL To Change (additional): " . strip_tags($addURLS) . " "; > $message .= "CURRENT Content: " . $curText . " "; $message .= "NEW Content: " . htmlentities($_POST['newText']) . " I think that looks A LOT nicer. And since this email is (theoretically) coming directly to you, isn’t it nice to know that it will be formatted to be easy on the eyes?
Some people just absolutely hate HTML email. For one, it can be a security risk as it’s possible to run JavaScript in them in some email clients which can be problematic. HTML emails also have a habit of being more easily caught in Spam filters. I think it’s less of a concern here as this email is essentially being created BY you FOR you.
I updated the demo and download to use the new HTML email format. View Demo Download Files There is likely to be another one or two demos on this form yet to come. I would at least like to do one on writing the data to a database before sending the email. Email can be such a fragile thing, that saving the data to a DB first is surely a smart move.
Send HTML Email in PHP via SMTP
Hi! Welcome to Koding Made Simple. Today we’ll see how to send html email in php. PHP’s mail() function is simple and effective and let you send emails with text/html contents and with attachments. Though it has some shortcomings compared to other mailer libraries like PHPMailer, it does the job and comes in-built with PHP package. Plain text emails are good enough, but the ability to send html emails is more powerful in email marketing and other promotions.
HTML emails include html tags and let you add images, format texts and other eye-catching call-to-action buttons etc. But you have to add additional headers for mailing them.
Using SMTP for Sending Email
You can send mails via third party Mail Servers but you need authentication first. That is if you want to send email via Gmail in php, then you must have a working gmail account and provide the accounts email-id and password for authentication. We’ll see how to set up the php configuration for sending email via gmail smtp server.
How to Send HTML Email in PHP Using Gmail SMTP
You need to change the settings in two places i.e., php.ini and sendmail.ini files. You must remove the ; at the starting of the line to enable the settings in these files. Please note the configuration is given for the xampp package. Open ‘php.ini’ file located at C:\xampp\php\php.ini and edit the settings like below.
php.ini
[mail function] SMTP = smtp.gmail.com smtp_port = 587 sendmail_from = christmascontest@gmail.com sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Next open the ‘sendmail.ini’ file located at C:\xampp\sendmail\sendmail.ini and make the below changes.
sendmail.ini
smtp_server = smtp.gmail.com smtp_port = 587 auth_username = christmascontest@gmail.com auth_password = christmascontest force_sender = christmascontest@gmail.com
Save the files and restart apache server for the changes to reflect. Now you are ready to send send html email using php mail() function.
Create a php file and write down the below php mailer code to it and save.
PHP Code for Sending HTML Email
Hi! We are glad to announce the Christmas contest winners.
# Ticket No. Name #1 P646MLDO808K Sally #2 DFJ859LV9D5U Parker #3 AU30HI8IHL96 Justin