PHP - Simple postcard example


SUBMITTED BY: efbee

DATE: Oct. 5, 2016, 7 p.m.

FORMAT: PHP

SIZE: 4.6 kB

HITS: 850

  1. Simple Postcards
  2. <form action="thispage.php4" method="post">
  3. <input type ="hidden" name="BeenSubmitted" value="true">
  4. <?php
  5. class mime_mail
  6. {
  7. var$parts;
  8. var$to;
  9. var$from;
  10. var$headers;
  11. var$subject;
  12. var$body;
  13. function mime_mail()
  14. {
  15. $this->parts=array();
  16. $this->to= "";
  17. $this->from= "";
  18. $this->subject= "";
  19. $this->body= "";
  20. $this->headers= "";
  21. }
  22. function add_attachment($message,$name= "",$ctype= "application/octet-stream")
  23. {
  24. $this->parts[]=array(
  25. "ctype"=>$ctype,
  26. "message"=>$message,
  27. "encode"=>$encode,
  28. "name"=>$name
  29. );
  30. }
  31. function build_message($part)
  32. {
  33. $message=$part[ "message"];
  34. $message=chunk_split(base64_encode($message));
  35. $encoding= "base64";
  36. return "Content-Type:".$part[ "ctype"].
  37. ($part[ "name"]? ";name=\"".$part[ "name"]. "\"": "").
  38. "\nContent-Transfer-Encoding:$encoding\n\n$message\n";
  39. }
  40. function build_multipart()
  41. {
  42. $boundary= "b".md5(uniqid(time()));
  43. $multipart= "Content-Type:multipart/mixed;boundary=$boundary\n\nThisisaMIMEencodedmessage.\n\n--$boundary";
  44. for($i=sizeof($this->parts)-1;$i>=0;$i--)
  45. {
  46. $multipart.= "\n".$this->build_message($this->parts[$i]). "--$boundary";
  47. }
  48. return$multipart.= "--\n";
  49. }
  50. function send()
  51. {
  52. $mime= "";
  53. if(!empty($this->from))
  54. $mime.= "From:".$this->from. "\n";
  55. if(!empty($this->headers))
  56. $mime.=$this->headers. "\n";
  57. if(!empty($this->body))
  58. $this->add_attachment($this->body, "", "text/plain");
  59. $mime.= "MIME-Version:1.0\n".$this->build_multipart();
  60. return(mail($this->to,$this->subject, "",$mime));
  61. }
  62. };
  63. ?>
  64. <?php
  65. if (isset($BeenSubmitted)) {
  66. $num_errors = 0;
  67. if (! strlen($to) > 0) {
  68. echo "You must enter an e-mail address for the recipient.<br>";
  69. $errors++;
  70. }
  71. if (! strlen($from) > 0) {
  72. echo "You must enter your e-mail address.<br>";
  73. $errors++;
  74. }
  75. if (! strlen($subject) > 0) {
  76. echo "You must enter a subject for the e-mail.<br>";
  77. $errors++;
  78. }
  79. if (! strlen($message) > 0) {
  80. echo "You must enter some kind of message for the recipient.<br>";
  81. $errors++;
  82. }
  83. if (! $fp = @fopen($postcard,"r")) {
  84. echo "You must select a postcard from the left.<br>";
  85. $errors++;
  86. }
  87. if ($errors == 0) {
  88. $attachment=fread($fp,filesize("$postcard"));
  89. $mail=new mime_mail();
  90. $mail->from="$from";
  91. $mail->to="$to";
  92. $mail->subject=stripslashes($subject);
  93. $mail->body=stripslashes($message);
  94. $mail->add_attachment("$attachment","$postcard","image/jpeg");
  95. if ($mail->send()) {
  96. //if successful then writes info to a log file called log.txt.
  97. $today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
  98. $filePointer = fopen("log.txt", "a");
  99. fputs($filePointer, "$today\t\t$from has sent this email to $to.\n");
  100. print ("Your email has been successfully sent!\n");
  101. } else {
  102. print ("Your email has been not been successfully sent because of a system error!\n");
  103. }
  104. }
  105. }
  106. ?>
  107. <p>Pick a postcard, fill out the information in the form boxes and click send! Make someone happy.
  108. <p><img src="postcards/thumbnails/tn_butterfly.jpg" alt="" height="75" width="100" border="0">
  109. <!-- Make sure that you have the correct path to the image in the value for the radio button(s) -->
  110. <br><input type="radio" name="postcard" value="/home/username/web_directory/images/butterfly.jpg"> Butterfly
  111. <p>Please fill out all of the information below.</p>
  112. <p>Recipient's Email:
  113. <br><input type="text" name="to" size="24" border="0">
  114. <p>Your Email:
  115. <br><input type="text" name="from" size="24" border="0">
  116. <p>Subject of Email:
  117. <br><input type="text" name="subject" size="24" border="0">
  118. <p>Text of Message:
  119. <br><textarea name="message" rows="6" cols="30"></textarea>
  120. <p><input type="submit" value="Send Postcard"> <input type="reset">
  121. </form>

comments powered by Disqus