-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
executable file
·70 lines (56 loc) · 2.21 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/* CUSTOM REST API ENDPOINTS START */
function sendWithPhpMailer($subject, $body, $reply) {
/* Import PHPMailter */
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->SMTPDebug = 2; /* 0=Debug OFF | 1=Debug Client | 2=debug Server */
$mail->isSMTP();
$mail->Host = "Your host name";
$mail->SMTPAuth = true;
$mail->Username = "user name or email";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = "from email address";
$mail->FromName = "from name";
$mail->addAddress("to address", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = $subject;
/* Add email template directly */
$mail->Body = $body;
/* If you have custom varibles to pass to your email template, You can do like this. Comment above line and below 3 lines. */
$message = $body;
$message = str_replace('%testusername%', 'Chamith K.', $message);
$mail->msgHTML($message);
/* You can display this varialble inside your html template like this: Hi %testusername%, */
$mail->AltBody = "This is the plain text version of the email content";
$send = false;
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
$send = false;
}else{
$send = true;
}
return $send;
}
function sendCustomEmail(WP_REST_Request $request) {
$subject = "Your Email Subject";
$themeURL = get_template_directory_uri() . '/emails/demo.html'; // Using the email template. You can comment these two lines if you aren't using a html template.
$body = file_get_contents($themeURL);
if ( sendWithPhpMailer( $subject, $body, $contactEmail ) ) {
$response['status'] = 200;
$response['message'] = 'Form sent successfully.';
}
return json_decode( json_encode( $response ) );
exit();
}
add_action( 'rest_api_init', function () {
register_rest_route( 'contact/v1', '/send', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'sendCustomEmail'
));
});
/* CUSTOM REST API ENDPOINTS ENDS */
?>