2017-06-20 66 views
0

I'a试图从PHP发送功能发送电子邮件,它是发送电子邮件,但不与任何数据填充此,它是空角4和PHP电子邮件发送

我的代码 这是发送服务角。在这里我有消息的所有数据,从我的形式

export interface IMessage { 
    name?: string, 
    telephon?: string, 
    message?: string 
} 

@Injectable() 
export class AppService { 
    private emailUrl = '../assets/contact.php'; 

    constructor(private http: Http) { 

    } 

    sendEmail(message: IMessage): Observable<IMessage> | any { 
    JSON.stringify(message); // also tried without it 
    return this.http.post(this.emailUrl, message) 
     .map(response => { 
     console.log('Sending email was successfull', response); 
     return response; 
     }) 
     .catch(error => { 
     console.log('Sending email got error', error); 
     return Observable.throw(error) 
     }) 
    } 
} 

PHP代码

<?php 
    header('Content-type: application/json'); 
    $errors = ''; 
    if(empty($errors)) 
    { 
     $postdata = file_get_contents("php://input"); // here I have null 
     $request = json_decode($postdata); // here I have null checked in console 
     $from_email = $request->email; 
     $message = $request->message; 
     $from_name = $request->name; 
     $to_email = '[email protected]'; 

     $contact = "<p><strong>Name:</strong> $from_name</p> 
           <p><strong>Email:</strong> $from_email</p>"; 
     $content = "<p>$message</p>"; 
     $website = 'My Wicked Awesome Website'; 
     $email_subject = "$website: Received a message from $from_name "; 

     $email_body = '<html><body>'; 
     $email_body .= "$contact $content"; 
     $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; 
     $response_array['MESSAGE'] = $message; 
     $response_array['POSTDATA'] = $postdata; 
     $response_array['REQUEEST'] = $request; 
     e 

cho json_encode($response_array); 
    echo json_encode($from_email); 
    header($response_array); 
    return $from_email; 
} else { 
    $response_array['status'] = 'error'; 
    echo json_encode($response_array); 
    header('Location: /error.html'); 
} 
?> 

那么什么想法? 感谢

+0

我发现一个问题,一切都很好 一个问题是sendEmail服务得到空消息 –

回答

0

我会建议定义变量:

// JS - remove JSON.stringify 
sendEmail(message: IMessage): Observable<IMessage> | any { 
    return this.http.post(this.emailUrl, message) 
    ... 

// PHP 
$message = filter_input('message', '', FILTER_SANITIZE_STRING); 
$from_name = filter_input('name', '', FILTER_SANITIZE_STRING); 
0

你只是打算在PHP获取POST数据

变化

$postdata = file_get_contents("php://input"); 

$message = $_POST['message']; 
+0

但http.post方法需要得到2个参数,在这里你发送1个对象,它不起作用 –

+0

什么不工作 –

+0

这个:this.http.post (数据),代码不会编译 我知道数据是包含电子邮件和消息的对象,但是post()只能看到1个参数 –