最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send angular 6 form without backend? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create a website with angular 6 and send emails from contact form.

This website is deployed on apache server and there is no backend, just Angular 6 html and javascript files.

I have maillet/mailgun account, firebase account and smtp access on my email address.

Is it possible to send email with angular 6 only ? How can i do that ?

Thanks. :)

I'm trying to create a website with angular 6 and send emails from contact form.

This website is deployed on apache server and there is no backend, just Angular 6 html and javascript files.

I have maillet/mailgun account, firebase account and smtp access on my email address.

Is it possible to send email with angular 6 only ? How can i do that ?

Thanks. :)

Share Improve this question asked Aug 24, 2018 at 17:43 JérémyJérémy 4432 gold badges12 silver badges23 bronze badges 2
  • You can use Mailgun's library or API and make the calls from your angular ponent. – Marc Commented Aug 24, 2018 at 19:43
  • You may want to take a look at EmailJS, which allows sending email from the client side Javascript code using pre-built templates [disclosure - I'm one of the creators] – Sasha Commented Aug 26, 2018 at 6:29
Add a ment  | 

1 Answer 1

Reset to default 3

You can't do that using only Angular, here's an example using Angular with a little of PHP to send emails:

HTML form:

<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 file:

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.plete();
    }

}

and in the php file:

<?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 = "Wele.";

    $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;
?>
发布评论

评论列表(0)

  1. 暂无评论