doupao2277 2017-04-13 13:24
浏览 80
已采纳

无法用php mail()发送邮件

After many search, my problem is still here. I'm new with angular and i try to send a mail in my personnal adresse after validate a contact form.

I have no mail after submit but if i copy and past the file mail.php in my server and i ping in i have a mail with default values.

When i use postman aplication a have the return of echo in my mail.php

I need your help

Here my contact.component.ts

  import { Component, OnInit } from '@angular/core';
import { FormGroup , FormControl, Validators } from '@angular/forms';
import { Http }    from '@angular/http';

@Component({
  selector: 'contact',
  templateUrl: './contact.component.html',
  styles:['input.ng-invalid {border-left: 5px solid red;}',
    'input.ng-valid {border-left:5px solid green;}',
    'input.ng-pristine {border-left: 1px solid grey;}',
    'textarea.ng-invalid {border-left: 5px solid red;}',
      'textarea.ng-valid {border-left:5px solid green;}',
      'textarea.ng-pristine {border-left: 1px solid grey;}']
  // styleUrls: ['../../app.component.css']
})
export class ContactComponent implements OnInit {

    name: string;
    email: string;
    message: string;
    endpoint : string;

    http : Http;

    constructor(http : Http) {
        this.http = http;
    }

    userForm = new FormGroup({
        nom: new FormControl(null),
        prenom: new FormControl(null),
        email: new FormControl(null),
        telephone: new FormControl(null),
        message: new FormControl(null)
    });

    onSubmit(){
        console.log(this.userForm.value);
        let postVars = {
          email : this.email,
          name : this.name,
          message : this.message
        };
            this.endpoint = "http://localhost:8000/src/app/page/contact/mail.php";
        this.http.post(this.endpoint, postVars)
        .subscribe(
            response => console.log(response),
            response => console.log(response)
        )
    }

    ngOnInit() {
    //This data could really come from some inputs on the interface - but let's keep it simple.
    this.email = "mypersonaladress@gmail.com";
    this.name = "Hayden Pierce";
    this.message = "Hello, this is Hayden.";

        //Start php via the built in server: $ php -S localhost:8000
            this.endpoint = "http://localhost:8000/src/app/page/contact/mail.php";
    }


}

here my contact.component.html

<main>
    <div class="container">
        <div class="row">
            <p class="col-xs-12 text-center">Vous desirez discuter un moment?</p>
            <p class="col-xs-12 text-center">Vous etes sur au bon endroit ?</p>
        </div>
        <form [formGroup]="userForm" method="POST" (ngSubmit)="onSubmit()">
            <div class="form-group">
                <label>Nom</label>
                <input type="text" name="nom" #refNom class="form-control" formControlName="nom">
                <b>{{refNom.className}}</b>
                <div class="alert alert-danger" *ngIf="userForm.controls['nom'].hasError('required') && ( userForm.controls['nom'].touched)">
                    Merci de rentrer un nom
                </div>
                <div class="alert alert-danger" *ngIf="userForm.controls['nom'].hasError('minlength')">
                    Il faut écrire un nom avec plus de deux characteres
                </div>
                <div class="alert alert-danger" *ngIf="userForm.controls['nom'].hasError('maxlength')">
                    Vous n'avez pas un diminutif?
                </div>
            </div>
            <div class="form-group">
                <label>Prenom</label>
                <input type="text" #refPrenom class="form-control" formControlName="prenom">
                <b>{{refPrenom.className}}</b>
                <div class="alert alert-danger" *ngIf="userForm.controls['prenom'].hasError('required') && ( userForm.controls['prenom'].touched)">
                    Merci de rentrer un prénom
                </div>
                <div class="alert alert-danger" *ngIf="userForm.controls['prenom'].hasError('minlength')">
                    Il faut écrire un prénom avec plus de deux characteres
                </div>
                <div class="alert alert-danger" *ngIf="userForm.controls['prenom'].hasError('maxlength')">
                    Vous n'avez pas un diminutif?
                </div>
            </div>
            <div class="form-group">
                <label>email</label>
                <input type="email" name="email" class="form-control" formControlName="email">
            </div>
            <div class="form-group">
                <label for="telephone">Telephone</label>
                <input type="text" class="form-control" formControlName="telephone">
            </div>
            <div class="form-group">
                <label for="message">Votre message</label>
                <textarea class="form-control" name="message" id="exampleTextarea" rows="3" formControlName="message"></textarea>
            </div>
            <button type="submit" [disabled]="!userForm.valid" class="btn btn-primary">Submit</button>
        </form>
    </div>
</main>

here my mail.php file

    <?php
$recipient = 'mypersonaladress@gmail.com
';
$subject = 'new message
';
$headers = "From: 
";
$message = '$params->message
';
mail('mypersonaladress@gmail.com', 'mon sujet', 'coucou');

switch($_SERVER['REQUEST_METHOD']){
    case("OPTIONS"): //Allow preflighting to take place.
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST");
        header("Access-Control-Allow-Headers: content-type");
        exit;
    case("POST"): //Send the email;
        header("Access-Control-Allow-Origin: *");

        $json = file_get_contents('php://input');
        $params = json_decode($json);
        $email = $params->email;
        $name = $params->name;
        $message = '$params->message
';

        $recipient = 'mypersonaladress@gmail.com
';
        $subject = 'new message
';
        $headers = "From: toto@toto.com
";
        if(mail($recipient, $subject, $message, $headers)){
            echo'totooo';
        }
    //    mail($recipient, $subject, $message, $headers);
        break;
    default: //Reject any non POST or OPTIONS requests.
        header("Allow: POST", true, 405);
        exit;
}
?>

i have no error on the bash for ng serve and on the bash php -S localhost:8000 i can see [Thu Apr 13 14:41:37 2017] 127.0.0.1:34118 [200]: /src/app/page/contact/mail.php

i dont know how to fix this.

  • 写回答

1条回答 默认 最新

  • douzhannao5357 2017-04-13 20:52
    关注

    Try and set the url parameter with URLSearchParams and use RequestOptions and set headers with x-www-form-urlencoded. I also noticed the weird use of Http, remove declaring http in component: http : Http;

    and change your constructor to:

    constructor(private http : Http) { }
    

    But back to the POST request. Import the following:

    import { RequestOptions, URLSearchParams, Headers } from '@angular/http';
    

    Your onSubmit:

    onSubmit(){
      let body = new URLSearchParams();
      body.set('email', this.email);
      body.set('name', this.name);
      body.set('message', this.message);
    
      let headers = new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      let options = new RequestOptions({ headers: headers });
    
      this.endpoint = "http://localhost:8000/src/app/page/contact/mail.php";
    
        this.http.post(this.endpoint, body, options)
          .subscribe(
            response => console.log(response)
        )
    }
    

    Hopefully this helps! Furthermore, I suggest you move all your http request to an actual service, that is usually the way to do it ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况