/*******************************************************************************
* Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
namespace Amazon.PowerShell.Cmdlets.SES
{
///
/// Composes an email message using an email template and immediately queues it for sending.
///
///
///
/// In order to send email using the SendTemplatedEmail
operation, your call
/// to the API must meet the following requirements:
/// -
/// The call must refer to an existing email template. You can create email templates
/// using the CreateTemplate operation.
///
-
/// The message must be sent from a verified email address or domain.
///
-
/// If your account is still in the Amazon SES sandbox, you may only send to verified
/// addresses or domains, or to email addresses associated with the Amazon SES Mailbox
/// Simulator. For more information, see Verifying
/// Email Addresses and Domains in the Amazon SES Developer Guide.
-
/// The maximum message size is 10 MB.
///
-
/// Calls to the
SendTemplatedEmail
operation may only include one Destination
/// parameter. A destination is a set of recipients who will receive the same version
/// of the email. The Destination
parameter can include up to 50 recipients,
/// across the To:, CC: and BCC: fields.
/// -
/// The
Destination
parameter must include at least one recipient email address.
/// The recipient address can be a To: address, a CC: address, or a BCC: address. If a
/// recipient email address is invalid (that is, it is not in the format UserName@[SubDomain.]Domain.TopLevelDomain),
/// the entire message will be rejected, even if the message contains other recipients
/// that are valid.
///
/// If your call to the SendTemplatedEmail
operation includes all of the
/// required parameters, Amazon SES accepts it and returns a Message ID. However, if Amazon
/// SES can't render the email because the template contains errors, it doesn't send the
/// email. Additionally, because it already accepted the message, Amazon SES doesn't return
/// a message stating that it was unable to send the email.
///
/// For these reasons, we highly recommend that you set up Amazon SES to send you notifications
/// when Rendering Failure events occur. For more information, see Sending
/// Personalized Email Using the Amazon SES API in the Amazon Simple Email Service
/// Developer Guide.
///
///
[Cmdlet("Send", "SESTemplatedEmail", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("System.String")]
[AWSCmdlet("Calls the Amazon Simple Email Service (SES) SendTemplatedEmail API operation.", Operation = new[] {"SendTemplatedEmail"}, SelectReturnType = typeof(Amazon.SimpleEmail.Model.SendTemplatedEmailResponse))]
[AWSCmdletOutput("System.String or Amazon.SimpleEmail.Model.SendTemplatedEmailResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.SimpleEmail.Model.SendTemplatedEmailResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class SendSESTemplatedEmailCmdlet : AmazonSimpleEmailServiceClientCmdlet, IExecutor
{
#region Parameter Destination_BccAddress
///
///
/// The recipients to place on the BCC: line of the message.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Destination_BccAddresses")]
public System.String[] Destination_BccAddress { get; set; }
#endregion
#region Parameter Destination_CcAddress
///
///
/// The recipients to place on the CC: line of the message.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Destination_CcAddresses")]
public System.String[] Destination_CcAddress { get; set; }
#endregion
#region Parameter ConfigurationSetName
///
///
/// The name of the configuration set to use when you send an email using SendTemplatedEmail
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ConfigurationSetName { get; set; }
#endregion
#region Parameter ReplyToAddress
///
///
/// The reply-to email address(es) for the message. If the recipient replies to the message,
/// each reply-to address will receive the reply.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("ReplyToAddresses")]
public System.String[] ReplyToAddress { get; set; }
#endregion
#region Parameter ReturnPath
///
///
/// The email address that bounces and complaints will be forwarded to when feedback forwarding
/// is enabled. If the message cannot be delivered to the recipient, then an error message
/// will be returned from the recipient's ISP; this message will then be forwarded to
/// the email address specified by the ReturnPath
parameter. The ReturnPath
/// parameter is never overwritten. This email address must be either individually verified
/// with Amazon SES, or from a domain that has been verified with Amazon SES.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ReturnPath { get; set; }
#endregion
#region Parameter ReturnPathArn
///
///
/// This parameter is used only for sending authorization. It is the ARN of the identity
/// that is associated with the sending authorization policy that permits you to use the
/// email address specified in the ReturnPath
parameter.For example, if the owner of example.com
(which has ARN arn:aws:ses:us-east-1:123456789012:identity/example.com
)
/// attaches a policy to it that authorizes you to use feedback@example.com
,
/// then you would specify the ReturnPathArn
to be arn:aws:ses:us-east-1:123456789012:identity/example.com
,
/// and the ReturnPath
to be feedback@example.com
.For more information about sending authorization, see the Amazon
/// SES Developer Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ReturnPathArn { get; set; }
#endregion
#region Parameter Source
///
///
/// The email address that is sending the email. This email address must be either individually
/// verified with Amazon SES, or from a domain that has been verified with Amazon SES.
/// For information about verifying identities, see the Amazon
/// SES Developer Guide.If you are sending on behalf of another user and have been permitted to do so by a
/// sending authorization policy, then you must also specify the SourceArn
/// parameter. For more information about sending authorization, see the Amazon
/// SES Developer Guide.Amazon SES does not support the SMTPUTF8 extension, as described in RFC6531.
/// For this reason, the local part of a source email address (the part of the
/// email address that precedes the @ sign) may only contain 7-bit
/// ASCII characters. If the domain part of an address (the part after the
/// @ sign) contains non-ASCII characters, they must be encoded using Punycode, as described
/// in RFC3492. The sender name
/// (also known as the friendly name) may contain non-ASCII characters. These characters
/// must be encoded using MIME encoded-word syntax, as described inRFC
/// 2047. MIME encoded-word syntax uses the following form: =?charset?encoding?encoded-text?=
.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String Source { get; set; }
#endregion
#region Parameter SourceArn
///
///
/// This parameter is used only for sending authorization. It is the ARN of the identity
/// that is associated with the sending authorization policy that permits you to send
/// for the email address specified in the Source
parameter.For example, if the owner of example.com
(which has ARN arn:aws:ses:us-east-1:123456789012:identity/example.com
)
/// attaches a policy to it that authorizes you to send from user@example.com
,
/// then you would specify the SourceArn
to be arn:aws:ses:us-east-1:123456789012:identity/example.com
,
/// and the Source
to be user@example.com
.For more information about sending authorization, see the Amazon
/// SES Developer Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String SourceArn { get; set; }
#endregion
#region Parameter Tag
///
///
/// A list of tags, in the form of name/value pairs, to apply to an email that you send
/// using SendTemplatedEmail
. Tags correspond to characteristics of the email
/// that you define, so that you can publish email sending events.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Tags")]
public Amazon.SimpleEmail.Model.MessageTag[] Tag { get; set; }
#endregion
#region Parameter Template
///
///
/// The template to use when sending this email.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String Template { get; set; }
#endregion
#region Parameter TemplateArn
///
///
/// The ARN of the template to use when sending this email.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String TemplateArn { get; set; }
#endregion
#region Parameter TemplateData
///
///
/// A list of replacement values to apply to the template. This parameter is a JSON object,
/// typically consisting of key-value pairs in which the keys correspond to replacement
/// tags in the email template.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
#else
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String TemplateData { get; set; }
#endregion
#region Parameter Destination_ToAddress
///
///
/// The recipients to place on the To: line of the message.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Destination_ToAddresses")]
public System.String[] Destination_ToAddress { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'MessageId'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.SimpleEmail.Model.SendTemplatedEmailResponse).
/// Specifying the name of a property of type Amazon.SimpleEmail.Model.SendTemplatedEmailResponse will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "MessageId";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the TemplateData parameter.
/// The -PassThru parameter is deprecated, use -Select '^TemplateData' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^TemplateData' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter Force
///
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.Source), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Send-SESTemplatedEmail (SendTemplatedEmail)"))
{
return;
}
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
if (this.PassThru.IsPresent)
{
throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
}
}
else if (this.PassThru.IsPresent)
{
context.Select = (response, cmdlet) => this.TemplateData;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ConfigurationSetName = this.ConfigurationSetName;
if (this.Destination_BccAddress != null)
{
context.Destination_BccAddress = new List(this.Destination_BccAddress);
}
if (this.Destination_CcAddress != null)
{
context.Destination_CcAddress = new List(this.Destination_CcAddress);
}
if (this.Destination_ToAddress != null)
{
context.Destination_ToAddress = new List(this.Destination_ToAddress);
}
if (this.ReplyToAddress != null)
{
context.ReplyToAddress = new List(this.ReplyToAddress);
}
context.ReturnPath = this.ReturnPath;
context.ReturnPathArn = this.ReturnPathArn;
context.Source = this.Source;
#if MODULAR
if (this.Source == null && ParameterWasBound(nameof(this.Source)))
{
WriteWarning("You are passing $null as a value for parameter Source which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.SourceArn = this.SourceArn;
if (this.Tag != null)
{
context.Tag = new List(this.Tag);
}
context.Template = this.Template;
#if MODULAR
if (this.Template == null && ParameterWasBound(nameof(this.Template)))
{
WriteWarning("You are passing $null as a value for parameter Template which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.TemplateArn = this.TemplateArn;
context.TemplateData = this.TemplateData;
#if MODULAR
if (this.TemplateData == null && ParameterWasBound(nameof(this.TemplateData)))
{
WriteWarning("You are passing $null as a value for parameter TemplateData which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.SimpleEmail.Model.SendTemplatedEmailRequest();
if (cmdletContext.ConfigurationSetName != null)
{
request.ConfigurationSetName = cmdletContext.ConfigurationSetName;
}
// populate Destination
var requestDestinationIsNull = true;
request.Destination = new Amazon.SimpleEmail.Model.Destination();
List requestDestination_destination_BccAddress = null;
if (cmdletContext.Destination_BccAddress != null)
{
requestDestination_destination_BccAddress = cmdletContext.Destination_BccAddress;
}
if (requestDestination_destination_BccAddress != null)
{
request.Destination.BccAddresses = requestDestination_destination_BccAddress;
requestDestinationIsNull = false;
}
List requestDestination_destination_CcAddress = null;
if (cmdletContext.Destination_CcAddress != null)
{
requestDestination_destination_CcAddress = cmdletContext.Destination_CcAddress;
}
if (requestDestination_destination_CcAddress != null)
{
request.Destination.CcAddresses = requestDestination_destination_CcAddress;
requestDestinationIsNull = false;
}
List requestDestination_destination_ToAddress = null;
if (cmdletContext.Destination_ToAddress != null)
{
requestDestination_destination_ToAddress = cmdletContext.Destination_ToAddress;
}
if (requestDestination_destination_ToAddress != null)
{
request.Destination.ToAddresses = requestDestination_destination_ToAddress;
requestDestinationIsNull = false;
}
// determine if request.Destination should be set to null
if (requestDestinationIsNull)
{
request.Destination = null;
}
if (cmdletContext.ReplyToAddress != null)
{
request.ReplyToAddresses = cmdletContext.ReplyToAddress;
}
if (cmdletContext.ReturnPath != null)
{
request.ReturnPath = cmdletContext.ReturnPath;
}
if (cmdletContext.ReturnPathArn != null)
{
request.ReturnPathArn = cmdletContext.ReturnPathArn;
}
if (cmdletContext.Source != null)
{
request.Source = cmdletContext.Source;
}
if (cmdletContext.SourceArn != null)
{
request.SourceArn = cmdletContext.SourceArn;
}
if (cmdletContext.Tag != null)
{
request.Tags = cmdletContext.Tag;
}
if (cmdletContext.Template != null)
{
request.Template = cmdletContext.Template;
}
if (cmdletContext.TemplateArn != null)
{
request.TemplateArn = cmdletContext.TemplateArn;
}
if (cmdletContext.TemplateData != null)
{
request.TemplateData = cmdletContext.TemplateData;
}
CmdletOutput output;
// issue call
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
return output;
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.SimpleEmail.Model.SendTemplatedEmailResponse CallAWSServiceOperation(IAmazonSimpleEmailService client, Amazon.SimpleEmail.Model.SendTemplatedEmailRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Simple Email Service (SES)", "SendTemplatedEmail");
try
{
#if DESKTOP
return client.SendTemplatedEmail(request);
#elif CORECLR
return client.SendTemplatedEmailAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public System.String ConfigurationSetName { get; set; }
public List Destination_BccAddress { get; set; }
public List Destination_CcAddress { get; set; }
public List Destination_ToAddress { get; set; }
public List ReplyToAddress { get; set; }
public System.String ReturnPath { get; set; }
public System.String ReturnPathArn { get; set; }
public System.String Source { get; set; }
public System.String SourceArn { get; set; }
public List Tag { get; set; }
public System.String Template { get; set; }
public System.String TemplateArn { get; set; }
public System.String TemplateData { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.MessageId;
}
}
}