php邮件脚本的语法错误

时间:2014-03-16 04:12:17

标签: php email

我有一份工作联系表格。我试图将其转换为仅发送来自不同表单的电子邮件地址,以允许人们订阅邮件列表。我不太了解php。我正在使用codev,并没有在我的语法中看到错误。

这是我用于contact.php表格的代码:

<?php

$params = array(
    'to' => 'mail@mail.com', 
    'from' => '<mail@mail.com>' 
);

$name = trim( $_POST['name'] );
$email = trim( $_POST['email'] );
$subject = trim( $_POST['subject'] );
$message = trim( $_POST['message'] );

if ( empty( $name ) or empty( $message ) ) {
    echo json_encode( array( 'status' => 'error' ) );
} else if ( empty( $email ) or ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
    echo json_encode( array( 'status' => 'email' ) );
} else {
    $headers = 'From: ' . $name . ' ' . $params['from'] . "\r\n" .
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion( );
    $message = str_replace( array( "\r", "\n" ), array( '', "\r\n" ), $message );

    if ( mail( $params['to'], ( empty( $subject ) ? 'New message without subject' : 'New message: ' . $subject ), $message, $headers ) ) {
        echo json_encode( array( 'status' => 'ok' ) );
    } else {
        echo json_encode( array( 'status' => 'error' ) );
    }
}
?>

这是我试图在订阅时向我发送电子邮件的新代码:

  <?php
$params =   ["to" => "recieve@wuno.com", "from" => "noreply@wuno.com"];
$subject    =   "New Subscription";
$message    =   trim($_POST["subscribe"]);


if (empty($message)   || !filter_var($message, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(["status" => "email"]);
} else {
    $headers    =   "From: {$params[from]}\r\n";
    $headers    .=  "Reply-To: {$message}\r\n";
    $headers    .=  "X-Mailer: PHP".phpversion()."\r\n";
    $message    =   str_replace(["\r","\n"], ["", "\r\n"], $message);
    if(mail($params["to"], $subject, $message, $headers)) {
        echo json_encode(["status" => "ok"]);
    } else {
        echo json_encode(["status" => "error"]);
    }
}
?>

3 个答案:

答案 0 :(得分:3)

是的,有......

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

在这里好好看看:PHP manual for mail()


mail( $params['to'], "New Subscription", $email_message, $headers);

$ email_message =您愿意发送的任何消息......

答案 1 :(得分:1)

有些变量实际上是常量,因此可以事先定义,基本上是为了易读性,但可以硬编码。

<?php 
$to='tomail@mail.com';
$from='noreply@mail.com';
$subject='New Subscription'; 
$message=trim($_POST['subscribe']); 

if(!filter_var($message,FILTER_VALIDATE_EMAIL)){
echo json_encode(["status" => "email"]);
}else{
$headers="From: $from\r\nReply-To: $message\r\nX-Mailer: PHP".phpversion()."\r\n"; 
$message=str_replace(["\r","\n"], ["", "\r\n"], $message); 
if(mail($to,$subject,$message,$headers)){ 
echo json_encode(["status" => "ok"]); 
}else{ 
echo json_encode(["status" => "error"]); 
}} 
?>

答案 2 :(得分:0)

$params =   ["to" => "nichodiaz@gmail.com", "from" => "noreply@wuno.com"];
$subject    =   "New Suscription";
$message    =   trim($_POST["subscribe"]);
$subscribe  =   "???";

if (empty($subscribe)   || !filter_var($subscribe, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(["status" => "email"]);
} else {
    $headers    =   "From: {$params[from]}\r\n";
    $headers    .=  "Reply-To: {$message}\r\n";
    $headers    .=  "X-Mailer: PHP".phpversion()."\r\n";
    $message    =   str_replace(["\r","\n"], ["", "\r\n"], $message);
    if(mail($params["to"], $subject, $message, $headers)) {
        echo json_encode(["status" => "ok"]);
    } else {
        echo json_encode(["status" => "error"]);
    }
}

这完全基于您的代码。然而,我无法找到一些变量位置,例如$ subscribe实际上是未定义的,而$ message从$ _POST [&#34; subscribe&#34;]获得它的价值,应该是其他方式,不是吗?

我认为$ message var上的格式完好无损,因为我认为必须有其他目的。但是如何通过$ message var填充标题中的回复地址...我放弃了!