Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mail()函数,标题问题,无法发送电子邮件_Php_Email - Fatal编程技术网

php mail()函数,标题问题,无法发送电子邮件

php mail()函数,标题问题,无法发送电子邮件,php,email,Php,Email,我知道这是一个很常见的问题,但我对标题有问题。此功能仅在我使用$header2发送邮件时发送电子邮件。在其他情况下,我得到失败的回声。不使用$headers 我不知道为什么会发生这种情况,而且它不适用于下面的php.net默认标题: $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 完整

我知道这是一个很常见的问题,但我对标题有问题。此功能仅在我使用
$header2
发送邮件时发送电子邮件。在其他情况下,我得到失败的回声。不使用
$headers

我不知道为什么会发生这种情况,而且它不适用于下面的php.net默认标题:

$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
完整功能:

function sendMail() {

$to = "mymail@gmail.com";
$subject = "This is subject";

$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

$header2 = "From:abc@somedomain.com \r\n";
$header2 = "Cc:afgh@somedomain.com \r\n";
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";

$retval = mail ($to, $subject, $message, $headers);

if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
}
函数sendMail(){ $to=”mymail@gmail.com"; $subject=“这是主题”; $message=“这是HTML消息。”; $message.=“这是标题。”; $headers='MIME版本:1.0'。“\r\n”; $headers.=“内容类型:text/html;字符集=iso-8859-1”。“\r\n”; //附加标题 $headers.=“收件人:Mary,Kelly”。“\r\n”; $headers.=“发件人:生日提醒”。“\r\n”; $headers.='Cc:birthdayarchive@example.com“。”\r\n”; $headers.='密件抄送:birthdaycheck@example.com“。”\r\n”; $header2=“发件人:abc@somedomain.com\r\n“; $header2=“抄送:afgh@somedomain.com\r\n“; $header2.=“MIME版本:1.0\r\n”; $header2.=“内容类型:text/html\r\n”; $retval=mail($to、$subject、$message、$headers); 如果($retval==true){ 回显“消息已成功发送…”; }否则{ 回显“无法发送消息…”; } }
首先,去掉整个代码块,因为这里没有引用您使用的
$headers2
变量一定要把我的答案通读一遍。

$header2 = "From:abc@somedomain.com \r\n";
$header2 = "Cc:afgh@somedomain.com \r\n";
//       ^ BROKEN concatenate
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";
不管怎样,PHP都会检查您的整个代码。它包含一个断开的连接

然而,这里不清楚您想做什么,显然您已经从
mail()
上的手册中选取了一些示例,然后盲目地在其中添加内容

侧注:收件人:已经预先确定了发送到哪里,因此不需要在标题中使用
收件人:

function sendMail() {

$to = "mymail@gmail.com";
$subject = "This is subject";

$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= "From:abc@somedomain.com \r\n";
$headers .= "Cc:afgh@somedomain.com \r\n";

$retval = mail ($to, $subject, $message, $headers);

if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
}

旁注:显示错误只能在暂存阶段进行,而不能在生产阶段进行。

请停止使用php mail()函数!我的建议是使用SMTP发送电子邮件

下面是一个使用PHPmailer的小代码片段:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtpserver.com';  // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@youremail.com';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@youremail.com', 'Mailer');
$mail->addAddress('joe@youremail.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@youremail.com');               // Name is optional
$mail->addReplyTo('info@youremail.com', 'Information');
$mail->addCC('cc@youremail.com');
$mail->addBCC('bcc@youremail.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

请仔细查看此信息
$header2=“From:abc@somedomain.com\r\n“$header2=“Cc:afgh@somedomain.com\r\n“$header2.=“MIME版本:1.0\r\n”-缺少什么?提示:这曾经发生在我身上。。。我花了1/2个小时“一次”才发现是什么破坏了我的代码。我说“一次”;-)另外,我看不到您在邮件标题中使用了
$headers2
。。。使用
$header2
时,由于
=
$header2=“From:abc@somedomain.com\r\n“$header2=“Cc:afgh@somedomain.com\r\n“-抄送将覆盖From$retval=mail($to,$subject,$message,$headers);在这一部分。当我放置$headers时,放置$headers 2将起作用。我知道($header2)缺少一个点,但当我把那个点(.=)放进去时,它就不再工作了;/仅在$header2函数正确的情况下。我是前端,不想学习下一个新的phpmailer和其他,这一个的预算真的很小,我也不想使用symfony2。@CD001这就是我告诉OP的内容,用这么多的话;-)可能值得注意的是,因为
From
头是强制的,所以它本身并不是中断的连接(没有语法错误),而是它正在删除强制头…@CD001 True。然而OP的问题有点不清楚。完全重写将是一种方式,让他们阅读函数。我提供了一个链接,指向我给出的答案(关于和如果),他们想分别发送两封邮件,可能是针对发送请求的人。谢谢大家的评论和建议!我决定改用PHPMailer。@UlandNimblehoof不客气。我很高兴听到这个消息,我想避免这种情况,但事实上,这种方式要好得多。我已经使用PHPmailer完成了这项工作,并使用自定义php代码在表单中包含附件。
<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtpserver.com';  // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@youremail.com';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@youremail.com', 'Mailer');
$mail->addAddress('joe@youremail.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@youremail.com');               // Name is optional
$mail->addReplyTo('info@youremail.com', 'Information');
$mail->addCC('cc@youremail.com');
$mail->addBCC('bcc@youremail.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}