Как отправить угловую 6 форму без бэкэнда? - PullRequest
0 голосов
/ 24 августа 2018

Я пытаюсь создать веб-сайт с angular 6 и отправлять электронные письма из контактной формы.

Этот веб-сайт развернут на сервере Apache, и нет никакого бэкэнда, только Angular 6 html и javascript файлы.

У меня есть учетная запись maillet / mailgun, учетная запись firebase и smtp на мой адрес электронной почты.

Можно ли отправлять электронную почту только с угловым 6?Как я могу это сделать?

Спасибо.:)

1 Ответ

0 голосов
/ 24 августа 2018

Вы не можете сделать это, используя только Angular, вот пример использования Angular с небольшим количеством PHP для отправки электронных писем:

HTML-форма:

<form [formGroup]="subscribeForm" novalidate (ngSubmit)="sendMail($event)">
    <input type="text" placeholder="Enter your email" formControlName="email"/>
    <button class="button margin-top-15" type="submit">Subscribe</button>
</form>

Ts файл:

export class MyComponent implements OnInit, OnDestroy {

    public subscribeForm: FormGroup;
    public email: FormControl;
    private unsubscribe = new Subject<void>();

    constructor(private databaseService: DatabaseService, private mailerService: MailerService, private http: HttpClient) { }

    ngOnInit() {
        this.createFormControls();
        this.createForm();
    }

    createFormControls() {
        this.email = new FormControl('', [
            Validators.required
        ]);
    }

    createForm() {
        this.subscribeForm = new FormGroup({
            email: this.email
        });
    }

    sendMail() {
        if (this.subscribeForm.valid) {
            this.http.post("link to the php file.", email).subscribe();
        }
    }

    ngOnDestroy(): void {
        this.unsubscribe.next();
        this.unsubscribe.complete();
    }

}

и в php-файле:

<?php
    header('Content-type: application/json');
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    $request = json_decode(file_get_contents("php://input"));
    $from_email = "your email goes here";

    $message = "Welcome.";

    $from_name = "your name goes here";

    $to_email = $request->email;

    $contact = "<p><strong>Name:</strong>$from_name</p><p><strong>Email:</strong> $from_email</p>";

    $email_subject = "Angular Php Email Example: Neue Nachricht von $from_name erhalten";

    $email_body = '<html><body>';
    $email_body .= "$<p><strong>Name:</strong>$from_name</p><p><strong>Email:</strong> $from_email</p>
                    <p>$message</p>";
    $email_body .= '</body></html>';

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: $from_email\n";
    $headers .= "Reply-To: $from_email";

    mail($to_email,$email_subject,$email_body,$headers);

    $response_array['status'] = 'success';
    $response_array['from'] = $from_email;

    echo json_encode($response_array);
    echo json_encode($from_email);
    header($response_array);
    return $from_email;
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...