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:


<?php
if(isset($_REQUEST['email'])){
$admin_email="putyouremailaddresshere@dot.com";
$email=$_REQUEST['email'];
$subject=$_REQUEST['yoursubjectgoeshere'];
$comment=$_REQUEST['yourmessagegoeshere'];
mail($admin-email,"$subject","$comment".$email);
echo "done";
}
else{
<form method="post">
Email:<input name="email" type="text".
subject:<input name="subject" type="text">
Message:
<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.


<?php
$to='theemail@address.com';
$subject="put it as you like";
$message="say any thing and lines are separated with a slash and small n";
$headers="From:theemail@address.com\r\n Reply-To:anotheremail@address.com";
$mail_sent=@mail($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.


<html>
<head>
<title>Emailing in PHP</title>
</head>
<body>
<?php
$to="putyouremailhere@dot.com";
$subject="give a subject";
$message="Message goes here";
$header="From:him@there.com";
$header=MIME-Version1.0";
$retval=mail($to,$subject,$message,$header);
if($retval==true)
{
echo "emailing successful";
}
else
{
echo "something went wrong";
}
?>
</body>
</html>

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:


<?php
$to = 'youraddress@example.com';
$subject = 'Test HTML email';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start();
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text;
Hi!
This is text email.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: html;
<h2>Hi!</h2>
<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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.