Guest User

Untitled

a guest
May 1st, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "Mail.php";
  4.  
  5. /* This is a PHP script to handle form-mailing on Webfaction hosted accounts.
  6. *
  7. * Normal PHP mail scripts won't work due to the way Webfaction has their stuff set up,
  8. * so this is a pretty decent workaround. It's a combination of various other efforts
  9. * by different people who use Webfaction; if you feel you're not being credited for any
  10. * of the effort/code below, feel free to email/message me on Github. I just want this to
  11. * be easily search-able, so I've thrown it here.
  12. *
  13. * Email me: ryan [at] venodesigns.net
  14. */
  15.  
  16. function smtp($to, $subject, $message, $additional_headers = "") {
  17. /* STMP Server Settings */
  18. $smtp_server = "smtp.webfaction.com";
  19. $smtp_username = "remaxbravo_contactforms";
  20. $smtp_password = "5255053678979";
  21. $smtp_default_from = "contact@fredva.com";
  22.  
  23. $to = (string) $to;
  24. $subject = (string) $subject;
  25. $message = (string) $message;
  26. $additional_headers = (string) $additional_headers;
  27.  
  28. /* Pear expects an Array */
  29. $raw_headers = str_replace("\r", "", $raw_headers);
  30. $raw_headers = explode("\n", $additional_headers);
  31. $headers = Array("To" => $to, "Subject" => $subject);
  32. $recipients = $to;
  33.  
  34. foreach($raw_headers as $raw_header) {
  35. $header = explode(":", $raw_header, 2);
  36.  
  37. if (count($header) != 2)
  38. continue; /* Silently kill offending headers. */
  39.  
  40. $header_key = ucfirst(trim($header[0]));
  41. $header_value = trim($header[1]);
  42.  
  43. if ($header_key == "To" || $header_key == "Subject")
  44. continue;
  45.  
  46. if($header_key == "Cc" || $header_key == "Bcc")
  47. $recipients .= ", " . $header_value;
  48.  
  49. $headers[$header_key] = $header_value;
  50. }
  51.  
  52. /* Set a default "From" header if none was provided */
  53. if (!array_key_exists("From", $headers))
  54. $headers["From"] = $smtp_default_from;
  55.  
  56. /* Create the SMTP object and send mail. Must return true on success,
  57. * false on failure.
  58. */
  59. $smtp = Mail::factory("smtp",
  60. Array("host" => $smtp_server,
  61. "auth" => true,
  62. "username" => $smtp_username,
  63. "password" => $smtp_password
  64. )
  65. );
  66.  
  67. $result = $smtp->send($recipients, $headers, $message);
  68.  
  69. if (PEAR::IsError($result)) {
  70. /*echo $result->getMessage(); -- Uncomment if you want some debugging */
  71. return false;
  72. } else {
  73. return true;
  74. }
  75. }
  76.  
  77. /* Grab your desired parameters from the $_REQUEST object.
  78. * Anything other than name/email needs to be concatenated into the final message.
  79. */
  80. $clientName = $_REQUEST["name"];
  81. $clientEmail = $_REQUEST["email"];
  82. $clientPhone = $_REQUEST["phone"];
  83. $projectInfo = $_REQUEST["projectinfo"];
  84.  
  85. /* Said concatenation efforts described above. */
  86. $finalProject = "Name: " . $clientName . "\nEmail: " . $clientEmail . "\nPhone Number: " . $clientPhone . "\nProject Info: " . $projectInfo;
  87.  
  88. /* A generic callback message that we'll throw to the user so they know we got their message. */
  89. $callbackMessage = "Thanks! Got your message, will reply shortly.";
  90.  
  91. /* This is totally optional, depending on what you wanna do... basically, everything that we pulled out of the request object
  92. * above is a REQUIRED field, so we check to make sure it didn't come back as something bogus. This (along with the stuff from $_REQUEST above)
  93. * can obviously change, depending on your needs.
  94. */
  95. if(!isset($_GET["name"]) || !isset($_GET["email"]) || !isset($_GET["phone"]) || !isset($_GET["projectinfo"])) {
  96. header('Location:/contact/mcgrath/?error=true');
  97. }
  98.  
  99. /* Fairly self explanatory... send the client the email saying that we got their message, then send ourselves the message. Ideally, you should reverse this,
  100. * but I'm feeling kind of lazy at the moment. ;P
  101. */
  102. smtp($clientEmail, "Thank you!", $callbackMessage, "");
  103. smtp("kmcgrath@remax.net", "Contact: " . $clientName, $finalProject, "");
  104.  
  105. /* Redirect to an arbitrary location after we send that junx. */
  106. header('Location:/thanks/');
  107.  
  108. ?>
Add Comment
Please, Sign In to add comment