Sending plain text email
Using mail() function you can send email in PHP. This function takes three mandatory arguments that specify the recipient’s email address, email subject and the message additionally there are other two optional parameters also.mail( to, subject, message, headers, parameters );
Here is the description for each parameters.No. | Function Arguments & Description |
---|---|
1 | to Required. Specifies the receiver / receivers of the e-mail |
2 | subject Required. Specifies the subject/title of the e-mail. Single lined title. |
3 | message Required. Defines the message to be sent. Every line ought to be separated with a LF (\n). |
4 | headers Optional. It’s used to add Cc,Bcc in email. It is also used to send HTML formatted emails. |
Sending HTML email
By default, PHP sends simple texted email but if you want to send formatted email you have to add HTML tags in a text message. When you are using HTML tags in Email message then you have to specify header for that.While sending an email using PHP, you can specify a Mime version, content type and character set to send an HTML e-mail.
Example
Following example will send an HTML email message to tutorial@technokida.com copying it to test@technokida.com. You can code this program in such a way that it should receive all content from the user and then it should send an email.<html> <head> <title>Sending HTML email using PHP</title> </head> <body> <?php $to = "test@technokida.com"; $subject = "This is test subject"; $message = "<b>This is HTML formatted message.</b>"; $message .= "<h1>This is headline.</h1>"; $header = "From:noreply@technokida.com \r\n"; $header .= "Cc:test2@technokida.com \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); //it will return true if message will sent successfully. if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?> </body> </html>
Sending attachments with email
The main important thing is to send email with Attachment. You have to specify Content-type header to multipart/mixed. Then text and attachment part can be specified within boundaries.A boundary is begins with two hyphens followed by a unique number which can not appear in the message part of the email. A PHP function md5() is used to generate a 32 digit hexadecimal number to create unique number. A final boundary denoting the email’s final section must also end with 2 hyphens.
<?php // request variables // important $from = $_REQUEST["from"]; $emaila = $_REQUEST["emaila"]; $attached_file = $_REQUEST["attached_file"]; if ($attached_file) { function mail_attachment ($from , $to, $subject, $message, $attachment){ $fileatt = $attachment; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1; $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment $email_from = $from; // Who the email is from $subject = "New Attachment Message"; $email_subject = $subject; // The Subject of the email $email_txt = $message; // Message that the email has in it $email_to = $to; // Who the email is to $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $msg_txt="\n\n You have recieved a new attachment message from $from"; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_txt .= $msg_txt; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_txt . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name = \"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename = \"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "File Sent Successfully."; unlink($attachment); // delete a file after attachment sent. }else { die("Sorry but the email could not be sent. Please go back and try again!"); } } move_uploaded_file($_FILES["attached_file"]["tmp_name"], 'temp/'.basename($_FILES['attached_file']['name'])); mail_attachment("$from", "noreply@technokida.com", "subject", "message", ("temp/".$_FILES["attached_file"]["name"])); } ?> <html> <head> <script language = "javascript" type = "text/javascript"> function CheckMyData() { with(document.filepost) { if(filea.value ! = "") { document.getElementById('one').innerText = "Attaching File... Please Wait"; } } } </script> </head> <body> <table width = "100%" height = "100%" border = "0" cellpadding = "0" cellspacing = "0"> <tr> <td align = "center"> <form name = "filepost" method = "post" action = "send-email-with-file.php" enctype = "multipart/form-data" id = "file"> <table width = "300" border = "0" cellspacing = "0" cellpadding = "0"> <tr valign = "bottom"> <td height = "20">Your Name:</td> </tr> <tr> <td><input name = "from" type = "text" id = "from" size = "30"></td> </tr> <tr valign = "bottom"> <td height = "20">Your Email Address:</td> </tr> <tr> <td class = "frmtxt2"><input name = "emaila" type = "text" id = "emaila" size = "30"></td> </tr> <tr> <td height = "20" valign = "bottom">Attach File:</td> </tr> <tr valign = "bottom"> <td valign = "bottom"><input name = "attached_file" type = "file" id = "attached_file" size = "16"></td> </tr> <tr> <td height = "40" valign = "middle"><input name = "Reset2" type = "reset" id = "Reset2" value = "Reset"> <input name = "Submit2" type = "submit" value = "Submit" onClick = "return CheckMyData()"></td> </tr> </table> </form> <center> <table width = "400"> <tr> <td id = "one"> </td> </tr> </table> </center> </td> </tr> </table> </body> </html>
Note: You can comment below, if you have any query
If you are buying a desktop computer online, be sure to play bazaar satta king see it in person if you can. For example, if you are going to buy a Dell computer online, attempt to see it at a store near you before you buy it. You may find that the keys are too close together for comfort, or that the screen is not as big as you thought. If you're able to see a physical model, it will help make your decision easier.play bazaar satta king
ReplyDelete