Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send HTML email in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 221
    Comment on it

    In today's world of internet, Email become one of the most popular and demanded service. Email become a necessary part of web development, email can be generate and send on different events such as registration of new users, activation of accounts on websites etc.

    PHP provides a built-in mail() function which allow us to create and send email to one or more recipients. Using mail() function we can send an email as plain text or in the HTML format.


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


    Description :
    recipient_email : Recipient's email address. Multiple email addresses can be added using comma(,). (Required)
    subject : Subject of email. This parameter cannot contain any newline characters.(Required)
    message : Message to be sent.Each line should be separated with a LF (\n). Lines should not exceed 70 characters.(Required)
    headers : Additional headers such as From, Cc, and Bcc etc. The additional headers should be separated with a CRLF (\r\n).(Optional)
    parameters : Additional parameters.(Optional)


    1. Send Plain Text Email

    The easiest way to send an email with PHP is to send a plain text email. The below example defines how to send a simple text email from PHP application. First we declare the variables in order recipient's email address, subject of email and message to be send and headers, then we pass these variables to the mail() function to send the email.


    <?php
    $recipient = 'test@test.com';
    $subject = 'Testing email'; 
    $message = "Hello User!\n\nThis email is for testing."; 
    $headers = "From: admin@test.com\r\nReply-To: info@test.com";
    $mail = @mail( $recipient, $subject, $message, $headers );
    
    //Check if the mail is sent successfully or not
    if($mail){
      echo "Email send successfully.";
    }else{
      echo "Email can not send.";
    }
    ?>
    

    2. Send HTML Email using PHP

    Usually when we send a simple email using PHP then all the content will be treated as simple text. We can also send an HTML formatted email using PHP mail() function which improves the output.

    <?php
    $recipient = 'youraddress@example.com';
    $subject = 'Test for HTML email'; 

    //Content-type always be set $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    $headers = "From: admin@test.com\r\nReply-To: info@test.com";

    $message = '<html><body>'; $message .= '<h2>Hello World!</h2>'; $message .= '<p style="color:#f00;font-size:12px;">This is HTML email for testing.</p>'; $message .= '</body></html>';

    if(mail($recipient, $subject, $message, $headers)){ echo "Email send successfully."; } else{ echo "Email can not send."; } ?>


 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: