PHP Tutorials

How to Send Email Using PHP

Form to Send Email Using PHP

To send an email using PHP you will need to code the emailing form and add the emailing script. But first of doing all these you will need the php mail() function. So, let us start with the form – we are assuming a contact form:

$admin_email="[email protected]";
echo "done";
<form method="post">
Email:<input name="email" type="text".
subject:<input name="subject" type="text">
<testarea name= "Body" rows="10" cols="10"></textarea>
<input type="submit" value="Submit/>

What this code does is, If “email” variable is filled out,  it will send the email using all the email information and after sendig the email it will confirm with a thanks message. If the email information is not filled and submited, only the form will be displayed. To send email users need to insert the email, if you are using it as in contact form, make sure to insert your email as default then the subject and email body as we are mentioning here as Comment.

$to='[email protected]';
$subject="put it as you like";
$message="say any thing and lines are separated with a slash and small n";
$headers="From:[email protected]\r\n Reply-To:[email protected]";
[email protected]($to,$subject,$headers,$message);
echo "email delivered";

The explanation to above goes this way: At first we define the email receiver, the mail subject and the message. Each line in the message gets separated with \n. At the same time we also define the header and make it different with \r\n. Then we send the message and receive confirmation.

In above ways you can send text emails, you cannot send emails with attachment or HTML ones. So, we shall check them.

How To Send HTML Email Using PHP?

Once you learn sending text emails using PHP, sending the HTML emails is not a difficult job.  The only difference is in the use of MIME in your code. Please note that allowing users to send HTML email can open the use of JavaScripts – which pose a good security threat. HTML emails often get truncated by email clients – so the success rate fluctuates.

<title>Emailing in PHP</title>
$to="[email protected]";
$subject="give a subject";
$message="Message goes here";
$header="From:[email protected]";
echo "emailing successful";
echo "something went wrong";

If you need to convey the message at any cost, and you cannot take the risk of HTML blocking then you should code the same differently. In that case, you need to generate a text only version of the message and send that along with the HTML one – so if the email client deny’s the HTML email, it will still receive the text version of the mail. See, a quick example code here:

$to = '[email protected]';
$subject = 'Test HTML email';
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text;
This is text email.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: html;
<p>This is HTML email.</p>
--PHP-alt-<?php echo $random_hash; ?>--
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";

Leave a Reply

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