为什么我用phpmailer会发邮件出错?

我下了一个phpmailer,用来发邮件,用它里面的一个简单测试发邮件的文件发,可是出错,不知为什么,请告知错在什么地方?如何正确发原代码<html><head><tit... 我下了一个phpmailer,用来发邮件,用它里面的一个简单测试发邮件的文件发,可是出错,不知为什么,请告知错在什么地方?如何正确发
原代码
<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('contents.html');
$body = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("newbeijingg@126.com");

$mail->SetFrom('34693233@qq.com');

$mail->AddReplyTo("newbeijingg@126.com");

$address = "newbeijingg@126.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!";
}

?>

</body>
</html>

错误提示:Could not instantiate mail function. Mailer Error: Could not instantiate mail function. 翻译过来是无法实例化邮件功能,不知为什么,谢谢回答!!!
展开
 我来答
xntpii
2011-01-13 · TA获得超过1150个赞
知道小有建树答主
回答量:983
采纳率:50%
帮助的人:781万
展开全部
你看下第一个例子的代码
require("class.phpmailer.php");//这个就是包含你下载的phpmailer类的网页,必须

$mail = new phpmailer();//创建对象

$mail->From = "list@example.com";//给对象的属性赋值,就是发件人的邮箱地址
$mail->FromName = "List manager";//发件人的名称
$mail->Host = "smtp1.example.com;smtp2.example.com";//邮件主机的smtp地址
$mail->Mailer = "smtp";
//下面是邮件内容要用到的代码
@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query = "SELECT full_name, email,爌hoto焖ROM employee燱HERE爄d=$id";
$result =燖MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result))
{
// HTML body
$body = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
$body .= "<i>Your</i> personal photograph to this message.<p>";
$body .= "Sincerely, <br>";
$body .= "phpmailer List manager";

// Plain text body (for mail clients that cannot read HTML)
$text_body = "Hello " . $row["full_name"] . ", \n\n";
$text_body .= "Your personal photograph to this message.\n\n";
$text_body .= "Sincerely, \n";
$text_body .= "phpmailer List manager";
//上面就是你邮件要发送的内容,修改成自己的就可以了
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress($row["email"], $row["full_name"]);//注意这个就是收件人,

$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

if(!$mail->Send())//开始发送
echo "There has been a mail error sending to " . $row["email"] . "<br>";

// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}

以上用到的属性方法可以查看class.phpmailer.php的代码,很容易知道是干什么的。下的包里也应该有简单的例子,可以看下。

用法看下面的例子,不就是一个类嘛。

Examples using phpmailer
1. Advanced Example

This demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.

require("class.phpmailer.php");

$mail = new phpmailer();

$mail->From = "list@example.com";
$mail->FromName = "List manager";
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->Mailer = "smtp";

@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query = "SELECT full_name, email,爌hoto焖ROM employee燱HERE爄d=$id";
$result =燖MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result))
{
// HTML body
$body = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
$body .= "<i>Your</i> personal photograph to this message.<p>";
$body .= "Sincerely, <br>";
$body .= "phpmailer List manager";

// Plain text body (for mail clients that cannot read HTML)
$text_body = "Hello " . $row["full_name"] . ", \n\n";
$text_body .= "Your personal photograph to this message.\n\n";
$text_body .= "Sincerely, \n";
$text_body .= "phpmailer List manager";

$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress($row["email"], $row["full_name"]);
$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

if(!$mail->Send())
echo "There has been a mail error sending to " . $row["email"] . "<br>";

// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}

2. Extending phpmailer

Extending classes with inheritance is one of the most powerful features of object-oriented programming. It allows you to make changes to the original class for your own personal use without hacking the original classes. Plus, it is very easy to do. I've provided an example:

Here's a class that extends the phpmailer class and sets the defaults for the particular site:
PHP include file: mail.inc.php

require("class.phpmailer.php");

class my_phpmailer extends phpmailer {
// Set default variables for all new objects
var $From = "from@example.com";
var $FromName = "Mailer";
var $Host = "smtp1.example.com;smtp2.example.com";
var $Mailer = "smtp"; // Alternative to IsSMTP()
var $WordWrap = 75;

// Replace the default error_handler
function error_handler($msg) {
print("My Site Error");
print("Description:");
printf("%s", $msg);
exit;
}

// Create an additional function
function do_something($something) {
// Place your new code here
}
}

Now here's a normal PHP page in the site, which will have all the defaults set above:
Normal PHP file: mail_test.php

require("mail.inc.php");

// Instantiate your new class
$mail = new my_phpmailer;

// Now you only need to add the necessary stuff
$mail->AddAddress("josh@example.com", "Josh Adams");
$mail->Subject = "Here is the subject";
$mail->Body = "This is the message body";
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional name

if(!$mail->Send())
{
echo "There was an error sending the message";
exit;
}

echo "Message was sent successfully";

上面2个就是简单的例子啊!你照着改改就是了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Zoho Mail
2024-11-18 广告
作为卓迈(北京)技术有限公司的工作人员,我们推荐使用Gmail、Outlook等国际知名的电子邮箱服务来在国外收发邮件。这些邮箱服务拥有广泛的国际覆盖和稳定的性能,能确保您与全球各地的合作伙伴和客户保持顺畅的沟通。同时,它们也具备强大的垃圾... 点击进入详情页
本回答由Zoho Mail提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式