-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_contact_form.php
99 lines (72 loc) · 2.47 KB
/
async_contact_form.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
// before use install asyncAWS: composer require asyncaws
require 'vendor/autoload.php';
use AsyncAws\Ses\SesClient;
use AsyncAws\Ses\Input\SendEmailRequest;
use AsyncAws\Ses\ValueObject\Body;
use AsyncAws\Ses\ValueObject\Content;
use AsyncAws\Ses\ValueObject\Destination;
use AsyncAws\Ses\ValueObject\EmailContent;
use AsyncAws\Ses\ValueObject\Message;
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']??'*',true);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS',true);
header('Access-Control-Allow-Headers: Content-Type, Authorization, Set-Cookie',true);
header('Access-Control-Allow-Credentials: true',true);
header('Vary: Origin',true);
if( $_SERVER['REQUEST_METHOD'] === 'OPTIONS' )
{
header('HTTP/1.1 204 No Content');
exit;
}
// ensure the identities are correctly set in AWS SES
$to_address = '[email protected]';
$from_address = '[email protected]';
// set your region, key and secret
$SES_CREDS = ['region' => 'us-east-2',
'accessKeyId' => '12345678912345678900',
'accessKeySecret' => '1234567891234567890012345678912345678900'];
// define various subjects, commonly from a drop-down
define('SUBJECT_QUESTION',1);
define('SUBJECT_PRODUCT',2);
define('SUBJECT_BILLING',3);
define('SUBJECT_OTHER',4);
$SUBJECTS = [SUBJECT_QUESTION=>"I have a general question",
SUBJECT_JPRODUCT=>"I have a question about a product",
SUBJECT_BILLING=>"I have a billing question",
SUBJECT_OTHER=>"Other"];
$request = json_decode(file_get_contents('php://input'));
if( empty($request) )
exit('not available');
if( !empty($request->checkbox) )
$request->checkbox = 'YES';
else
$request->checkbox = 'NO';
$body = <<< __EOH__
From: {$request->name} - {$request->email}
Checkbox: {$request->checkbox}
Subject: {$SUBJECTS[$request->subject]}
Message:
{$request->message}
__EOH__;
$ses = new SesClient($SES_CREDS);
$result = $ses->sendEmail(new SendEmailRequest([
'FromEmailAddress' => $from_address,
'Content' => new EmailContent([
'Simple' => new Message([
'Subject' => new Content(['Data' => 'NEW CONTACT FORM SUBMIT']),
'Body' => new Body([
'Text' => new Content(['Data' => $body]),
]),
]),
]),
'Destination' => new Destination([
'ToAddresses' => [$to_address]
]),
]));
if( $result )
{
echo 'ok';
error_log('sent contact message: '.$result->getMessageId());
}
else
echo 'error';