php - mail()预计至少有3个参数。但我已经正确地声明了变量

时间:2016-06-23 03:24:38

标签: php email

<?php
$targetEmail = 'myemail@gmail.com';
$subject = 'Sending e-mails from PHP is fun!';
$message = 'Do you agree?';

mail($targetEmail, $subject, $message);
echo mail(); //I use this to see the error
?>

我已经设置了该变量,但当我回应它时,它说Warning: mail() expects at least 3 parameters, 0 given我还没有收到电子邮件

3 个答案:

答案 0 :(得分:0)

你应该做

<?php
$targetEmail = 'myemail@gmail.com';
$subject = 'Sending e-mails from PHP is fun!';
$message = 'Do you agree?';

$mail = mail($targetEmail, $subject, $message);
echo $mail;

答案 1 :(得分:0)

执行echo mail();时,它再次调用函数mail(),但您没有向其传递任何变量。尝试:

$mail = mail($targetEmail, $subject, $message);
echo $mail;

然而,根据mail()documentation;该功能接受:

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

但您必须在$ additional_headers变量中设置发件人电子邮件才能发送电子邮件。试试这个:

$targetEmail = 'myemail@gmail.com';
$subject = 'Sending e-mails from PHP is fun!';
$message = 'Do you agree?';
$headers = "From: your-email@example.com" . "\r\n" .
    "CC: somebodyelse@example.com";

$mail = mail($targetEmail, $subject, $message, $headers);
echo $mail;

答案 2 :(得分:0)

尝试这样的事情

 <?php
    $to = 'myemail@gmail.com';
    $subject = 'Sending e-mails from PHP is fun!';
    $message = 'Do you agree?';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    if(mail($to, $subject, $message ,$headers)){
        echo "successfully  mailed";
    }
    else{
        echo "Failure";
    }

    ?>

工作正常..!