0

My question seems to be very similar to AWS - Using email template from S3 bucket except instead of Python, I am using java/springboot to send the email from AWS SES and using javascript/typescript for the front end. I'm able to send an email with a hardcoded subject and body. I want to be able to send an email with a template from my S3 bucket. My application has a list of the templates in the bucket. When selected, a preview is displayed. How can I send a selected template within my application?

# "send-email-templates.ts"
constructor(
    private templateService: TemplateService,
    private schoolService: SchoolService,
    private hireStageService: HireStageService,
    private emailSearchService: EmailSearchService,
    private router: Router,
    private _ngZone: NgZone,
    private candidateService: CandidateService,
    public dialog: MatDialog,
) { }

ngOnInit() {
    this.getSchools();
    this.getHireStages();
    this.isPreviewOpen = false;
    this.selectedTemplateName = null;
    this.getAllTemplates();
}

triggerResize() {
    this._ngZone.onStable.pipe(take(1)).subscribe(() => this.autosize.resizeToFitContent(true));
}

createNewTemp() {
    this.router.navigate([`/create_template`])
}

// Send email template to multiple emails after search filter
sendEmail() {
            this.dialog.open(DialogOverviewExampleDialog, {
        width: '250px'
    });
    let candidateEmails = (<HTMLInputElement>document.getElementById("emailList")).value
    let subject = this.sendForm.get('subjectForm').value
    let body = "HARDCODED BODY"
    this.candidateService.sendEmailWithoutPositions(candidateEmails, subject, body).subscribe(() => {
        this.router.navigate(['/send_email']);
    });
}

searchCandidates() {
    this.emailSearchService.searchCandidates(this.searchForm.get('namesForm').value,
        this.searchForm.get('majorsForm').value, this.schools.toString(),
        this.hireStages.toString(), this.searchForm.get('startDateForm').value,
        this.searchForm.get('endDateForm').value)
        .subscribe(listOfEmails => {
            console.log(listOfEmails);
           (<HTMLInputElement>document.getElementById('emailList')).value = <string> listOfEmails;
        })
}

//Send email to new candidate
sendEmail(candidateEmail: string, positionId: number[], subject: string, body: string) {
    let details:emailDetails = new emailDetails();
    details.to = candidateEmail;
    details.positionId = positionId;
    details.subject = subject;
    details.body = body;
    return this.http.post<emailDetails>(`${this.context.getOutgoingEmailUrl()}`, details)
}

// Send email to string of search/filtered candidate emails
sendEmailWithoutPositions(candidateEmails: string, subject: string, body: string) {
    let details:emailDetails2 = new emailDetails2();
    details.to = candidateEmails;
    details.subject = subject;
    details.body = body;
    return this.http.post<emailDetails2>(`${this.context.getOutgoingEmailUrl()}`, details)
}

HTML

    <mat-grid-tile colspan="6" rowspan="4">
        <div class='search'>
            <form [formGroup]='searchForm' autocomplete='off' novalidate>
                <mat-form-field class="nameSearch">
                    <span matPrefix>Name:&nbsp;</span>
                    <input matInput id='namesForm' placeholder="&nbsp;Enter candidate name" formControlName='namesForm'>
                </mat-form-field>
                <mat-form-field class="majorSearch">
                    <span matPrefix>Major:&nbsp;</span>
                    <input matInput id='majorsForm' placeholder="&nbsp;Enter candidate major" formControlName="majorsForm">
                </mat-form-field>

                <tr>
                    <td>
                        <mat-form-field *ngIf="schoolSource" class="schools">
                            <mat-label>Schools</mat-label>
                            <mat-chip-list #chipList>
                                <mat-chip *ngFor="let school of schools" [selectable]="true" [removable]="true"
                                    (removed)="removeSchool(school)">
                                    {{ school }}
                                    <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
                                </mat-chip>
                                <input input='schoolsForm' placeholder="Choose school(s)" #schoolInput [formControl]="schoolCtrl"
                                    [matAutocomplete]="auto" [matChipInputFor]="chipList"
                                    [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                                    [matChipInputAddOnBlur]="addOnBlur" formControlName='schoolsForm'>
                            </mat-chip-list>

                            <mat-autocomplete #auto (optionSelected)="selectedSchool($event)">
                                <mat-option *ngFor="let school of schoolSource" [value]='schoolId'>
                                    {{ school.schoolName }}
                                </mat-option>
                            </mat-autocomplete>
                        </mat-form-field>
                    </td>
                    <td>
                        <mat-form-field *ngIf="hireStageSource" class="hireStages">
                            <mat-label>Hire Stage</mat-label>
                            <mat-chip-list #chipList1>
                                <mat-chip *ngFor="let hireStage of hireStages" [selectable]="true" [removable]="true"
                                    (removed)="removeStage(hireStage)">
                                    {{ hireStage }}
                                    <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
                                </mat-chip>
                                <input id='stagesForm' placeholder="Choose Hire Stage(s)" #hireStageInput [formControl]="hireStageCtrl"
                                    [matAutocomplete]="auto1" [matChipInputFor]="chipList1"
                                    [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                                    [matChipInputAddOnBlur]="addOnBlur" formControlName='stagesForm'>
                            </mat-chip-list>

                            <mat-autocomplete #auto1 (optionSelected)="selectedStage($event)">
                                <mat-option *ngFor="let hireStage of hireStageSource" [value]='stageId'>
                                    {{ hireStage.stageName }}
                                </mat-option>
                            </mat-autocomplete>
                        </mat-form-field>
                <tr>
                    <div class="dates">
                        <mat-form-field class="startDate">
                            <input matInput id='startDateForm' [matDatepicker]="picker" placeholder="Start date"
                                name="startDate" formControlName='startDateForm'>
                            <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                            <mat-datepicker #picker></mat-datepicker>
                        </mat-form-field>

                        <mat-form-field class="endDate">
                            <input matInput id='endDateForm'[matDatepicker]="picker2" placeholder="End date"
                                name="endDate" formControlName='endDateForm'>
                            <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
                            <mat-datepicker #picker2></mat-datepicker>
                        </mat-form-field>
                            <button class="addEmail" mat-raised-button color = "primary" (click) = "searchCandidates()">Add</button>
                    </div>
                </tr>
                </td>
            </form>
        </div>
    </mat-grid-tile>

    <mat-grid-tile colspan="4" rowspan="4">
        <div class='send'>
            <form [formGroup]='sendForm' autocomplete='off' novalidate name="form">
                <tr>
                    <button class="newTemplateButt" mat-raised-button color = "primary" (click) = "createNewTemp()">Create New Template</button>
                </tr>
                <tr>
                    <mat-form-field class="sendToList">
                        <mat-label>Candidate Emails will be listed here</mat-label>
                        <textarea   id = 'emailList'
                                    matInput
                                    cdkTextareaAutosize
                                    #autosize = "cdkTextareaAutosize"
                                    cdkAutosizeMinRows = "1"
                                    cdkAutosizeMaxRows = "8"></textarea>
                    </mat-form-field>
                </tr>
                <tr>
                    <mat-form-field class = "subjectLine">
                        <span matPrefix>Subject:&nbsp;</span>
                        <input matInput id='subjectLine' formControlName='subjectForm'>
                    </mat-form-field>
                </tr>
                <button mat-raised-button color = "primary" class = "sendEmail" (click) = "sendEmail()">Send Email</button>
            </form>
        </div>
    </mat-grid-tile>
</mat-grid-list>

<div class="email-templates" [style.display]="isPreviewOpen ? 'grid' : 'flex'">
    <app-email-templates-list [templates]="templateList" [selectedTemplate]="selectedTemplateName" (display)="displayPreviewHandler($event)"></app-email-templates-list>```
    <app-email-templates-preview [previewStatus]="isPreviewOpen" [selectedTemplate]="selectedTemplateName"></app-email-templates-preview>
</div>
Tim Kelly
  • 1
  • 6

1 Answers1

0
sendEmail() {
            this.dialog.open(DialogOverviewExampleDialog, {
        width: '250px'
    });
    let candidateEmails = (<HTMLInputElement>document.getElementById("emailList")).value
    let subject = this.sendForm.get('subjectForm').value
    console.log(this.selectedTemplateName)
    this.templateService.getTemplate(this.selectedTemplateName).subscribe((templateData : any) => {
        console.log(templateData.Body)
        console.log(templateData.Body.data)
        let body = importTemplate(templateData.Body.data).toString();
        console.log(body)
        this.candidateService.sendEmailWithoutPositions(candidateEmails, subject, body).subscribe(() => {
            this.router.navigate(['/send_email']);
        });
    })
}

This is now sending the templates, but images are failing to display. Still looking into that issue. Edit: The images weren't sending because they were base64 encoded. When viewing the source of all images in google images search results, they all showed base64 encoding. I noticed that when I went to the source of these images, it would give me a more specific src such as https://......jpg. When I dragged and dropped this source image into my template and saved and sent, it forwent the base64 encoding and therefore, images show up in my Gmail inbox when sent from my application now.

Tim Kelly
  • 1
  • 6