PHP邮件发送代码不起作用

时间:2010-04-11 00:54:00

标签: php

我正在尝试使用这种编码,但它没有处理,也没有输出任何错误。

function send_email($subject='Activate Your Account', $html_content, $text_content, $headers) { 

    $en['email'] = 'test@email.com';
    $to = $en['email'];
    $en['memb_id'] = '39';
    $en['confcode'] = '69696969';
    $en['user'] = 'couple';

    $text_content = "Confirm Your domain.com Account\r\n";
    $text_content.= "UserName: " . $en['user'] . "\r\n";
    $text_content.= "Activate Your Account by visiting this link below:\r\n";
    $text_content.= "http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . "\r\n";
    $text_content.= "\r\n";
    $text_content.= "______________________\r\n";
    $text_content.= "Thanks,\r\n";
    $text_content.= "Staff";

    $html_content = "<html><body><h1>Confirm Your domain.com Account</h1>";
    $html_content.= "<p>UserName: " . $en['user'] . "<br>";
    $html_content.= "Activate Your Account by visiting this link below:<br>";
    $html_content.= "<a href=http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . ">http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . "</a>";
    $html_content.= "</p>";
    $html_content.= "______________________<br>";   
    $html_content.= "Thanks,<br>";
    $html_content.= " Staff";
    $html_content.= "</body></html>";

    $mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

    $headers = "MIME-Version: 1.0\r\n";
    $headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
    $headers.= "Content-Transfer-Encoding: 7bit\r\n";

    $body = "This is a multi-part message in mime format.\n\n";
    $body.= "--$mime_boundary\n";
    $body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
    $body.= "Content-Transfer-Encoding: 7bit\n\n";
    $body.= $text_content;
    $body.= "\n\n";

    $body.= "--$mime_boundary\n";
    $body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $body.= "Content-Transfer-Encoding: 7bit\n\n";
    $body.= $html_content;
    $body.= "\n\n";
    $body.= "--$mime_boundary--\n";

    $headers.= 'From: <support@domain.com>' . "\r\n";
    $headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
    $headers.= 'Date: '.date('n/d/Y g:i A')."\r\n";
    $headers.= 'Reply-To: <support@domain.com>' . "\r\n";

    return mail($to, $subject, $body, $headers);
    echo $to;
    echo $subject;
    echo $body;
    echo $headers;
    }

3 个答案:

答案 0 :(得分:2)

最后一点让我困惑:

return mail($to, $subject, $body, $headers);
echo $to;
echo $subject;
echo $body;
echo $headers;

您知道这些echo语句都不会被执行,因为它们位于return之后吗?

如果您没有尝试过,请尝试使用error_log验证脚本中的步骤或查看错误日志本身。

答案 1 :(得分:1)

使用PHPMailer/Lite。保存自己的MIME头痛。

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

答案 2 :(得分:1)

在这里,我为你修好了。我检查了它,它运作良好。还要确保您的主机支持mail()函数。如果未启用SMTP,则您无法确定发送邮件。

修订后的代码:

<?php 
function send_email($subject='Activate Your Account') { 

    $en['email'] = 'test@email.com';
    $to = $en['email'];
    $en['memb_id'] = '39';
    $en['confcode'] = '69696969';
    $en['user'] = 'couple';

    $text_content = "Confirm Your domain.com Account\r\n";
    $text_content.= "UserName: " . $en['user'] . "\r\n";
    $text_content.= "Activate Your Account by visiting this link below:\r\n";
    $text_content.= "http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . "\r\n";
    $text_content.= "\r\n";
    $text_content.= "______________________\r\n";
    $text_content.= "Thanks,\r\n";
    $text_content.= "Staff";

    $html_content = "<html><body><h1>Confirm Your domain.com Account</h1>";
    $html_content.= "<p>UserName: " . $en['user'] . "<br>";
    $html_content.= "Activate Your Account by visiting this link below:<br>";
    $html_content.= "<a href=http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . ">http://www.domain.com/confirm/" . $en['memb_id'] . "/" . $en['confcode'] . "</a>";
    $html_content.= "</p>";
    $html_content.= "______________________<br>";   
    $html_content.= "Thanks,<br>";
    $html_content.= " Staff";
    $html_content.= "</body></html>";

    $mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

    $headers = "MIME-Version: 1.0\r\n";
    $headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
    $headers.= "Content-Transfer-Encoding: 7bit\r\n";

    $body = "This is a multi-part message in mime format.\n\n";
    $body.= "--$mime_boundary\n";
    $body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
    $body.= "Content-Transfer-Encoding: 7bit\n\n";
    $body.= $text_content;
    $body.= "\n\n";

    $body.= "--$mime_boundary\n";
    $body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $body.= "Content-Transfer-Encoding: 7bit\n\n";
    $body.= $html_content;
    $body.= "\n\n";
    $body.= "--$mime_boundary--\n";

    $headers.= 'From: <support@domain.com>' . "\r\n";
    $headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
    $headers.= 'Date: '.date('n/d/Y g:i A')."\r\n";
    $headers.= 'Reply-To: <support@domain.com>' . "\r\n";

    return mail($to, $subject, $body, $headers);

    }

?>

希望有所帮助。请告诉我们。