[
'to' => [
'type' => 'String',
'description' => __('Who to send the email to', 'wp-graphql-send-mail'),
],
'from' => [
'type' => 'String',
'description' => __('Who to send the email from', 'wp-graphql-send-mail'),
],
'replyTo' => [
'type' => 'String',
'description' => __('Reply to address', 'wp-graphql-send-mail'),
],
'subject' => [
'type' => 'String',
'description' => __('Subject of email', 'wp-graphql-send-mail'),
],
'body' => [
'type' => 'String',
'description' => __('Body of email', 'wp-graphql-send-mail'),
],
],
# outputFields expects an array of fields that can be asked for in response to the mutation
# the resolve function is optional, but can be useful if the mutateAndPayload doesn't return an array
# with the same key(s) as the outputFields
'outputFields' => [
'sent' => [
'type' => 'Boolean',
'description' => __('Was the email sent', 'wp-graphql-send-mail'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['sent']) ? $payload['sent'] : null;
}
],
'origin' => [
'type' => 'String',
'description' => __('Origin that sent the request', 'wp-graphql-send-mail'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['origin']) ? $payload['origin'] : null;
}
],
'to' => [
'type' => 'String',
'description' => __('Who the email got sent to', 'wp-graphql-send-mail'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['to']) ? $payload['to'] : null;
}
],
'replyTo' => [
'type' => 'String',
'description' => __('reply To address used', 'wp-graphql-send-mail'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['replyTo']) ? $payload['replyTo'] : null;
}
],
'message' => [
'type' => 'String',
'description' => __('Message', 'wp-graphql-send-mail'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['message']) ? $payload['message'] : null;
}
]
],
# mutateAndGetPayload expects a function, and the function gets passed the $input, $context, and $info
# the function should return enough info for the outputFields to resolve with
'mutateAndGetPayload' => function ($input, $context, $info) {
// Do any logic here to sanitize the input, check user capabilities, etc
$options = get_option('wpgraphql_send_mail_settings');
$allowedOrigins = array_map('trim', explode(',', $options['wpgraphql_send_mail_allowed_origins']));
$cc = trim($options['wpgraphql_send_mail_cc']);
$defaultFrom = trim($options['wpgraphql_send_mail_from']);
$defaultTo = trim($options['wpgraphql_send_mail_to']);
$http_origin = trim($_SERVER['HTTP_ORIGIN']);
$message = null;
$canSend = false;
$to = isset($input['to']) ? trim($input['to']) : trim($defaultTo);
$replyTo = trim($input['replyTo']) ;
if ($allowedOrigins) {
if (in_array($http_origin, $allowedOrigins)) {
$canSend = true;
} else {
$message = __('Origin not allowed, set origin in settings', 'wp-graphql-send-mail');
}
} else {
// if they did not enter any then we will allow any
$canSend = true;
}
// Above tests passed and there is a to address and an email body
if ($canSend && !empty($to) && !empty($input['body'])) {
$subject = trim($input['subject']);
$body = $input['body'];
$from = trim($input['from']);
$headers = array('Content-Type: text/html; charset=UTF-8');
if (isset($cc) && !empty($cc)) {
$headers[] = 'Cc: ' . $cc;
}
if (isset($replyTo) && !empty($replyTo)) {
$headers[] = 'Reply-To: ' . $replyTo;
}
if (isset($from) && !empty($from)) {
$headers[] = 'From: ' . $from;
} else if (isset($defaultFrom) && !empty($defaultFrom)) {
$headers[] = 'From: ' . $defaultFrom;
}
$sent = wp_mail($to, $subject, $body, $headers);
$message = $sent ? __('Email Sent', 'wp-graphql-send-mail') : __('Email failed to send', 'wp-graphql-send-mail');
} else {
$sent = false;
$message = $message ? $message : __('Email Not Sent', 'wp-graphql-send-mail');
}
return [
'sent' => $sent,
'origin' => $http_origin,
'to' => $to,
'replyTo' => $replyTo,
'message' => $message,
];
}
]);
});