wuno
wuno

Reputation: 9875

syntax error with php mail script

I have a working contact form. I am trying to convert it to only send a email address from a different form to allow people to subscribe to a mailing list. I don't understand php that well. I am using codev and do not see an errors in my syntax.

This is my code that is working I am using for the contact.php form:

<?php

$params = array(
    'to' => '[email protected]', 
    'from' => '<[email protected]>' 
);

$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' ) );
    }
}
?>

And This is the new code I am trying to make to send me someones email when they subscribe:

  <?php
$params =   ["to" => "[email protected]", "from" => "[email protected]"];
$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"]);
    }
}
?>

Upvotes: 1

Views: 319

Answers (3)

JBES
JBES

Reputation: 1557

Some variables are actually constants so can be defined beforehand basically for legibility, though could be hardcoded.

<?php 
$to='[email protected]';
$from='[email protected]';
$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"]); 
}} 
?>

Upvotes: 1

Sidstar
Sidstar

Reputation: 354

$params =   ["to" => "[email protected]", "from" => "[email protected]"];
$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"]);
    }
}

This is totally based on your code. Nevertheless I am unable to undestand some variable placements like $subscribe is actually undefined while $message get its value from $_POST["subscribe"], should be other way around, isn't?

I lest formatting intact on your $message var as I think there must be some other purpose for it. BUT how come Reply-To address in headers is filled by $message var... I give up!

Upvotes: 0

Sidstar
Sidstar

Reputation: 354

Yes there is...

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

take a good look here: PHP manual for mail()


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

$email_message = whatever message you are willing to send...

Upvotes: 3

Related Questions