【问题标题】:mail error with PHP MailerPHP Mailer 的邮件错误
【发布时间】:2012-02-04 19:30:28
【问题描述】:

据建议,我曾尝试使用 PHP Mailer 发送带有表单的电子邮件附件,但是我不断收到错误消息“Mailer 错误:无法实例化邮件功能。

我已经重新检查了电子邮件地址,但没有发现任何错误

这里是 PHP 的代码以及表单的代码。非常感谢任何输入。谢谢,JB

<html>
<head>
<title>PHPMailer - Mail() basic test</title>
</head>
<body>

<?php

require_once('class.phpmailer.php');

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

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

$mail->AddReplyTo("jbgraphics@rogers.com","First Last");

$mail->SetFrom('jbgraphics@rogers.com', 'First Last');

$mail->AddReplyTo("jbgraphics@rogers.com","First Last");

$address = "jbgraphics@rogers.com";
$mail->AddAddress($address, "John Beadle");

$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.pdf");      // attachment
$mail->AddAttachment("images/phpmailer_mini.jpeg"); // attachment

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

?>

</body>
</html>

及表格代码:

<form action="test_mail_basic.php" method="post"
enctype="multipart/form-data">
<label for="file" class="bodyCopy"><span class="bodyCopy">Attach resume:</span></label><br     />
<input type="file" name="attach1" id="file" /><br /> 
<br />
<label for="file" class="bodyCopy"><span class="bodyCopy">Attach photo:</span></label><br />
<input type="file" name="attach2" id="file" /><br /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

【问题讨论】:

    标签: php


    【解决方案1】:

    该错误是 php mail() 函数返回 false 的结果。

    通常,如果在 php.ini 中未正确配置 sendmail 或服务器上不存在 sendmail,则返回 false。

    您是在 Linux 服务器还是 Windows 上运行它?查看邮件是否正常工作的一个非常简单的测试是运行以下代码:

    <?php
    $to = 'you@yoursite.com';
    
    $res = mail($to, 'Testing mail', "This is a test\n\nEnd.", "From: $to");
    
    if ($res) {
        echo "Message appears to have been accepted"; // does not mean it will be delivered
    } else {
        echo "PHP mail() failed.";
    }
    

    如果您使用的是 Windows,您可能需要使用 SMTP 服务器而不是 php mail(),因此您需要使用 SMTP,如 phpmailer SMTP example 所示。

    如果您在共享主机上,则可能是由于某些额外参数被发送到邮件功能而导致邮件被拒绝。

    【讨论】:

    • 您创建一个包含此代码的文件,然后在本地主机上运行它。你需要有 apache、php 和 postfix/exim 才能让它工作
    • 我将代码保存为 .php 文件还是 .html 文件?另外,我该如何运行它——我仍然不明白那部分。运行是指上传到服务器,然后在浏览器中打开?
    • 只需使用带有.php 扩展名的任何名称保存即可。您只需在浏览器中访问 URL 即可运行它。
    • 得到它并对其进行测试 消息返回为“消息似乎已被接受”,因此可以安全地假设邮件正在工作。
    • 是的,如果您收到消息,那就更好了。现在尝试将 mail() 调用更改为如下所示:mail($to, 'Testing mail', "This is a test\n\nEnd.", "From: $to", "-oi -f $to"); 并查看它是否失败。
    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2018-05-17
    • 2016-08-18
    相关资源
    最近更新 更多