Emailing when submit button is pressed

2019-09-17 06:59发布

问题:

<?php
 echo $this->Form->end(__('Submit')) 
 $to = "someone@hotmail.com"; $subject = "Test mail";
 $message = "Hello! This is a simple email message.";
 $from = "someonelse@example.com";
 $headers = "From:" . $from;
 mail($to,$subject,$message,$headers);
 echo "Mail Sent.";
?>

Hey guys need help here! I want when someone pressed the submit button, it saves and sends the details entered to the emails

回答1:

You have to use like this.Make sure your button type is submit

<?php
if($_POST){

    $to = "someone@hotmail.com"; $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "someonelse@example.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";


    }
    ?>


回答2:

If you are still running WAMPServer then you are on a Windows PC.

Window does not come with a mail server, and the mail() function does little else other than pass a mail onto a mail server.

You will either need to install a mail server, they do exist for windows, but this is not a simple task for a beginner.

Alternatively, look at phpMailer which can be used to send mail and basically piggy backs an existing email account like one of your yahoo or gmail accounts



回答3:

Try the below code,

<?php
    if(isset($_POST['submit'])){ // check if submit button is pressed
        $to = "someone@hotmail.com"; $subject = "Test mail";
        $message = "Hello! This is a simple email message.";
        $from = "someonelse@example.com";
        $headers = "From:" . $from;
        if(mail($to,$subject,$message,$headers))
           echo "Mail Sent.";
        else
           echo 'Error while sending mail.';
    }
?>


标签: php email send