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.
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.';
}
The following code sends an email from the script with HTML message content using PHP.
to
– Recipient email address. subject
– Subject of the email.message
– Message to be sent.headers
– From, Cc, Bcc, Content-type headers.$htmlContent
variable holds the HTML contents of the email.$to = 'user@example.com';
$from = 'sender@example.com';
$fromName = 'SenderName';
$subject = "Send HTML Email in PHP by CodexWorld";
$htmlContent = '
<html>
<head>
<title>Welcome to CodexWorld</title>
</head>
<body>
<h1>Thanks you for joining with us!</h1>
<table cellspacing="0" style="border: 2px dashed #FB4314; width: 100%;">
<tr>
<th>Name:</th><td>CodexWorld</td>
</tr>
<tr style="background-color: #e0e0e0;">
<th>Email:</th><td>contact@codexworld.com</td>
</tr>
<tr>
<th>Website:</th><td><a href="http://www.codexworld.com">www.codexworld.com</a></td>
</tr>
</table>
</body>
</html>';
// 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.';
}
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.
email_template.html
).// Get HTML contents from file
$htmlContent = file_get_contents("email_template.html");
Creating a Simple Contact Form with PHP
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.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
It’s helpful
hello ii would like to know if i can send an email with template with css style
nice
Thanks CodexWorld, It works well with PHPMailer too, to put Mail Html into a separate file and then use this function ( file_get_contents) .
Hi I have developed one website its in core PHP can you please add calendar logic in it means it will be like doctor will add appointment slot from there panel which is already I implemented in my code I just want based on given slot it will show there slots in there profile.
Explained elegantly and simply, right to the point and it works! Easy to understand too. Thank you very much!
thank you so much for this useful code.
Hello!
This is great job – works perfectly on my webpage.
I have one question. How to display in htmlcontent a varible from php?
in this example, in email I have message “$username” instead “John”.
I would like to insert username in message – i don’t know how to do this.
Is it true we must remove the ‘\r’ apart from the $headers (so only ‘\n’) in linux machine?
No, you don’t need to remove the ‘\r’ apart from the $headers.
hello, thanks for this tutorial. I will like to add Phpmailer to this code, how do i go about it? Thanks
See this tutorial to send email via SMTP server using PHPMailer – https://www.codexworld.com/send-html-email-php-gmail-smtp-phpmailer/
Any examples using this method with variables in the HTML code? Best way to break in and out of code?
This code worked so well. But,How can i add html code using swiftmailer?
Hey i am sending an html formatted email with button. But i am having issue i caanot see button in my mail please help me
Wow it worked , was trying hard since 2 days finally i got my solution here. thank you very much. keep it up. thanks once again
can you plz help me
i want to make a form which will get emails addresses and post email to them just like your demo page
Please send your requirements to support@codexworld.com
Thanks a lot
Great tutorial, very straight forward and clear
hi tnkzz, i need you r help , y my mails directly go to spam folder can u help me please .
i am using host gator hosting
@Anas Make sure that the sender email address already exists.
Hello Team CodexWorld
Awesome Tutorial, but how can I make html email format dynamic, means I want to send email to users with product image and product description and that is coming from database
So how can I get this type of functionality
Please help me…
Thanks Codex World for this great recipe. It exactly matched my requirement. Code worked as is to fulfill my needs. Its very easily extendable to fit in other formats.
Thanks for this tutorial, it is so useful for me.
Thank you very much!
Thanks alot friend! The cleanest and the most understandable source for a simple cool email. people get stuck with their stupid secure ways and makes codes unreadable. Loved it
Hello codex-world team,
Many many thanks for the tutorials it is very useful for me.
hii ,Thanks codexworld team for this useful source code.
thanks dear
Hi, thanks for this source code. I’m creating an email registration with an activation code, I already have a php code that creates a random activation code, Where and how can I put this feature to this script?