How to send mail using php and html

 

How to send mail using php and html

How to send mail using php and html

Now a day’s sending mail through web application or website is common thing or we can say it is being compulsory to have a contact form, enquiry form, registration form or any form that take the value from user and send the mail. You can use the PHP built-in mail() function for creating and sending email messages to one or more recipients dynamically from your PHP application either in a plain-text form or formatted HTML.

Syntax : mail(to, subject, message, headers, parameters)

Some basics you have to know: above parameters of mail() function.

to: The recipient’s email address.(mail to whom you want to receive.)

subject : Subject of the email to be sent. This parameter cannot contain any newline characters.

message :  message that you want to send.

header :  this parameter generally used to add extra headers. i.e. “From”, “Cc”, “Bcc”. The

additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n).

Note : Other than above we can use other parameters to customize our mail. This is an optional.

Sending email using HTML Formatting:

Now we are going to formatting the email to improve the output using HTML.

<?php

$to = 'contact@lotsofthing.com'; // here recipient email id comes

$subject = 'here your subject comes';

$from = 'peterparker@email.com'; // here sender email id comes

// To send HTML mail, the Content-type header must be set

$headers = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859- 1' . "\r\n";

// Create email headers

$headers .= 'From: '.$from."\r\n".

'Reply-To: '.$from."\r\n" .

'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message

$message = '<html><body>';

$message .= '<h1 style="color:#f40;">Hi Tony!</h1>';

$message .= '<p style="color:#080;font-size:18px;">How are you?</p>';

$message .= '</body></html>';

// Sending email

if(mail($to, $subject, $message, $headers)){

echo 'Your mail has been sent successfully.';

} else{

echo 'Unable to send email. Please try again.';

}

?>

 

This is basics way to ” How to send mail using php and html “. In the next page we will learn

“how to create simple contact form for our website using php and HTML click here.”

 

Leave a Reply

Your email address will not be published. Required fields are marked *