Guest User

Untitled

a guest
Apr 7th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. function sendWithPhpMailer($subject, $body, $reply) {
  4. require(ABSPATH . WPINC . '/class-phpmailer.php');
  5. require(ABSPATH . WPINC . '/class-smtp.php');
  6.  
  7. // date_default_timezone_set( 'America/Sao_Paulo' );
  8.  
  9. $blogname = wp_strip_all_tags( trim( get_option( 'blogname' ) ) );
  10. $smtpHost = wp_strip_all_tags( trim( get_option( 'smtp_host' ) ) );
  11. $mailPort = wp_strip_all_tags( trim( get_option( 'smtp_port' ) ) );
  12. $smtpUser = wp_strip_all_tags( trim( get_option( 'smtp_user' ) ) );
  13. $smtpPass = wp_strip_all_tags( trim( get_option( 'smtp_pass' ) ) );
  14. $mailTo = wp_strip_all_tags( trim( get_option( 'mail_to' ) ) );
  15.  
  16. $send = false;
  17. $mail = new PHPMailer;
  18.  
  19. try {
  20. $mail->IsSMTP();
  21. $mail->SMTPDebug = 0;
  22. $mail->Sender = $smtpUser;
  23. $mail->CharSet = 'utf-8';
  24. $mail->SMTPSecure = 'tls';
  25. $mail->SMTPAuth = true;
  26. $mail->Port = $mailPort;
  27. $mail->Host = $smtpHost;
  28. $mail->Username = $smtpUser;
  29. $mail->Password = $smtpPass;
  30. $mail->Subject = $subject;
  31. $mail->From = $smtpUser;
  32. $mail->setFrom($smtpUser, $blogname);
  33. $mail->addReplyTo($reply);
  34. $mail->addAddress($mailTo);
  35.  
  36. // Attachments
  37. // $mail->addAttachment('/var/tmp/file.tar.gz');
  38.  
  39. $mail->isHTML(true);
  40. $mail->Body = $body;
  41.  
  42. $send = $mail->Send();
  43.  
  44. $mail->ClearAllRecipients();
  45. } catch (Exception $e) {
  46. echo "Message could not be sent. Mailer Error: $mail->ErrorInfo \n";
  47. echo "Error: $e";
  48. return false;
  49. }
  50.  
  51. return $send;
  52. }
  53.  
  54. function sendContactMail() {
  55. $response = array(
  56. 'status' => 304,
  57. 'message' => 'There was an error sending the form.'
  58. );
  59.  
  60. $parameters = $request->get_json_params();
  61.  
  62. if ( count($_POST) > 0 ) {
  63. $parameters = $_POST;
  64. }
  65.  
  66. $contactName = wp_strip_all_tags( trim( $parameters['contact_name'] ) );
  67. $contactEmail = wp_strip_all_tags( trim( $parameters['contact_email'] ) );
  68. $contactPhone = wp_strip_all_tags( trim( $parameters['contact_phone'] ) );
  69. $contactMessage = wp_strip_all_tags( trim( $parameters['contact_message'] ) );
  70.  
  71. if ( !empty($contactName) && !empty($contactEmail) && !empty($contactPhone) && !empty($contactMessage) ) {
  72. $subject = "(New message sent from site $blogname) $contactName <$contactEmail>";
  73. $body = "<h3>$subject</h3><br/>";
  74. $body .= "<p><b>Name:</b> $contactName</p>";
  75. $body .= "<p><b>Email:</b> $contactEmail</p>";
  76. $body .= "<p><b>Phone:</b> $contactPhone</p>";
  77. $body .= "<p><b>Message:</b> $contactMessage</p>";
  78.  
  79. if ( sendWithPhpMailer( $subject, $body, $contactEmail ) ) {
  80. $response['status'] = 200;
  81. $response['message'] = 'Form sent successfully.';
  82. }
  83. }
  84.  
  85. return json_decode( json_encode( $response ) );
  86. exit();
  87. }
  88.  
  89. add_action( 'rest_api_init', function () {
  90. register_rest_route( 'contact/v1', '/send', array(
  91. 'methods' => 'POST',
  92. 'callback' => 'sendContactMail'
  93. ));
  94. });
Add Comment
Please, Sign In to add comment