Skip to content

Commit 9b731e9

Browse files
committed
From and CC features
1 parent 934ad92 commit 9b731e9

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.0] - 2020-08-06
9+
10+
- Added CC option in admin
11+
- Added From option to admin and mutation
12+
813
## [0.0.1] - 2020-05-04
914

1015
Initial Release

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
One of the simple things about a traditional WordPress sites is sending emails, this plugin makes it easy to do this via a simple mutation when you are using WPGraphQL.
66

7+
### Features
8+
9+
- Set list of allowed origins in admin
10+
- Set CC address in admin
11+
- Set default From address in admin
12+
713
## Composer
814

915
```
@@ -17,6 +23,7 @@ mutation SEND_EMAIL {
1723
sendEmail(
1824
input: {
1925
26+
2027
subject: "test email"
2128
body: "test email"
2229
clientMutationId: "test"

wp-graphql-send-mail.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author URI: https://www.ashleyhitchcock.com
99
* Text Domain: wp-graphql-send-mail
1010
* Domain Path: /languages
11-
* Version: 0.0.2
11+
* Version: 1.0.0
1212
*
1313
* @package WP_Graphql_SEND_MAIL
1414
*/
@@ -40,20 +40,54 @@ function wpgraphql_send_mail_settings_init()
4040
'wsmPlugin',
4141
'wpgraphql_send_mail_wsmPlugin_section'
4242
);
43+
44+
add_settings_field(
45+
'wpgraphql_send_mail_cc',
46+
__('CC address', 'wp-graphql-send-mail'),
47+
'wpgraphql_send_mail_cc_render',
48+
'wsmPlugin',
49+
'wpgraphql_send_mail_wsmPlugin_section'
50+
);
51+
52+
add_settings_field(
53+
'wpgraphql_send_mail_from',
54+
__('Default From address', 'wp-graphql-send-mail'),
55+
'wpgraphql_send_mail_from_render',
56+
'wsmPlugin',
57+
'wpgraphql_send_mail_wsmPlugin_section'
58+
);
4359
}
4460

4561
function wpgraphql_send_mail_origins_textarea_render()
4662
{
4763
$options = get_option('wpgraphql_send_mail_settings');
4864
?>
49-
<textarea rows="6" name='wpgraphql_send_mail_settings[wpgraphql_send_mail_allowed_origins]'><?php echo $options['wpgraphql_send_mail_allowed_origins']; ?></textarea>
65+
<textarea rows="6" name='wpgraphql_send_mail_settings[wpgraphql_send_mail_allowed_origins]'>
66+
<?php echo isset($options['wpgraphql_send_mail_allowed_origins']) ? trim($options['wpgraphql_send_mail_allowed_origins']) : ''; ?>
67+
</textarea>
68+
<?php
69+
}
70+
71+
function wpgraphql_send_mail_cc_render()
72+
{
73+
$options = get_option('wpgraphql_send_mail_settings');
74+
?>
75+
<input type="email" name='wpgraphql_send_mail_settings[wpgraphql_send_mail_cc]' value="<?php echo isset($options['wpgraphql_send_mail_cc']) ? trim($options['wpgraphql_send_mail_cc']) : ''; ?>" />
76+
<?php
77+
}
78+
79+
function wpgraphql_send_mail_from_render()
80+
{
81+
$options = get_option('wpgraphql_send_mail_settings');
82+
?>
83+
<input type="email" name='wpgraphql_send_mail_settings[wpgraphql_send_mail_from]' value="<?php echo isset($options['wpgraphql_send_mail_from']) ? trim($options['wpgraphql_send_mail_from']) : ''; ?>" />
5084
<?php
5185
}
5286

5387

5488
function wpgraphql_send_mail_settings_section_callback()
5589
{
56-
echo __('Enter a comma separated list of domains that can sent emails.', 'wp-graphql-send-mail');
90+
echo __('Enter a comma separated list of domains that can send emails, remembering to include the protocol e.g. https://wordpress.com', 'wp-graphql-send-mail');
5791
}
5892

5993
function wpgraphql_send_mail_options_page()
@@ -89,6 +123,10 @@ function wpgraphql_send_mail_options_page()
89123
'type' => 'String',
90124
'description' => __('Who to send the email to', 'wp-graphql-send-mail'),
91125
],
126+
'from' => [
127+
'type' => 'String',
128+
'description' => __('Who to send the email from', 'wp-graphql-send-mail'),
129+
],
92130
'subject' => [
93131
'type' => 'String',
94132
'description' => __('Subject of email', 'wp-graphql-send-mail'),
@@ -133,10 +171,13 @@ function wpgraphql_send_mail_options_page()
133171
// Do any logic here to sanitize the input, check user capabilities, etc
134172
$options = get_option('wpgraphql_send_mail_settings');
135173
$allowedOrigins = array_map('trim', explode(',', $options['wpgraphql_send_mail_allowed_origins']));
136-
$http_origin = $_SERVER['HTTP_ORIGIN'];
174+
$cc = trim($options['wpgraphql_send_mail_cc']);
175+
$defaultFrom = trim($options['wpgraphql_send_mail_from']);
176+
$http_origin = trim($_SERVER['HTTP_ORIGIN']);
137177
$message = null;
138178
$canSend = false;
139179

180+
140181
if ($allowedOrigins) {
141182
if (in_array($http_origin, $allowedOrigins)) {
142183
$canSend = true;
@@ -150,18 +191,30 @@ function wpgraphql_send_mail_options_page()
150191

151192
if ($canSend && !empty($input['to']) && !empty($input['body'])) {
152193

153-
154-
$to = $input['to'];
155-
$subject = $input['subject'];
194+
$to = trim($input['to']);
195+
$subject = trim($input['subject']);
156196
$body = $input['body'];
197+
$from = trim($input['from']);
157198
$headers = array('Content-Type: text/html; charset=UTF-8');
158199

200+
if (isset($cc)) {
201+
$headers[] = 'Cc: ' . $cc;
202+
}
203+
204+
if (isset($from)) {
205+
$headers[] = 'From: ' . $from;
206+
} else if (isset($defaultFrom)) {
207+
$headers[] = 'From: ' . $defaultFrom;
208+
}
209+
159210
$sent = wp_mail($to, $subject, $body, $headers);
211+
160212
$message = $sent ? __('Email Sent', 'wp-graphql-send-mail') : __('Email Not Sent', 'wp-graphql-send-mail');
161213
} else {
162214
$sent = false;
163215
$message = $message ? $message : __('Email Not Sent', 'wp-graphql-send-mail');
164216
}
217+
165218
return [
166219
'sent' => $sent,
167220
'origin' => $http_origin,

0 commit comments

Comments
 (0)