PHP Classes

File: appsmo_newsletter/includes/news-letter-mailer.php

Recommend this page to a friend!
  Classes of Hillary Kollan   My WordPress Plugin List   appsmo_newsletter/includes/news-letter-mailer.php   Download  
File: appsmo_newsletter/includes/news-letter-mailer.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: My WordPress Plugin List
WordPress plugins for several purposes
Author: By
Last change:
Date: 1 year ago
Size: 1,516 bytes
 

Contents

Class file image Download
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
   
$name = strip_tags(trim($_POST["name"]));
   
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
   
$recipient = $_POST['recipient'];
   
$subject = $_POST['subject'];

   
//validate
   
$validate = validateInput($name, $email);
    if(
$validate){
         
//send response
       
http_response_code(400);
        echo
'Please enter '.$validate;
        exit;
    }

   
//send mail
   
$mail = sendMailToSubscriber($name,$email, $recipient, $subject);
    echo
$mail;

}else{
   
http_response_code(403);
    echo
"Wrong request method";
}

/**
 * Validates name and email
 *
 * @param [type] $name
 * @param [type] $email
 * @return string
 */
function validateInput($name, $email){
   
       
//validation
       
if($name == ""){
            return
'name';
        }
   
        if(
$email == ""){
            return
'email';
        }

}

/**
 * sends email to subsciber
 *
 * @param [type] $name
 * @param [type] $email
 * @param [type] $recipient
 * @param [type] $subject
 * @return void
 */
function sendMailToSubscriber($name,$email, $recipient, $subject){
   
$message = "Name: $name\n";
   
$message .= "Email: $email\n\n";

   
//build Headers
   
$headers = "From: $name <$email>";

    if(
mail($recipient, $subject, $message, $headers)){
       
http_response_code(200);
        return
"You are now subscribed";
    }else{
       
http_response_code(500);
        return
"An error occurred";
    }

}