sdf*_*for 33 php email phpmailer
我正在使用mail()稍微修改我的用户ID 的基本示例,我收到错误"Mailer Error:无法实例化邮件功能"
如果我使用邮件功能 -
mail($to, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)
它工作正常,虽然我在发送html时遇到问题,这就是为什么我在尝试使用PHPMailer.
这是代码:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
print ($body ); // to verify that I got the html
$mail->AddReplyTo("reply@domain.com","my name");
$mail->SetFrom('from@domain.com', 'my name');
$address = "to@domain.com";
$mail->AddAddress($address, "her name");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$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!";
}
?>
Run Code Online (Sandbox Code Playgroud)
Muk*_*ain 26
尝试使用SMTP发送电子邮件: -
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Run Code Online (Sandbox Code Playgroud)
Avi*_*ash 13
$mail->AddAddress($address, "her name");
Run Code Online (Sandbox Code Playgroud)
应改为
$mail->AddAddress($address);
Run Code Online (Sandbox Code Playgroud)
这适用于我的情况..
Mat*_*epa 10
这对我有用
$mail->SetFrom("from@domain.co","my name", 0); //notice the third parameter
Run Code Online (Sandbox Code Playgroud)
如果您要发送文件附件,并且您的代码适用于小附件但大型附件失败:
如果您在尝试发送大型电子邮件时收到错误"无法实例化邮件功能"错误,并且您的PHP错误日志包含消息"无法发送邮件:太大",那么您的邮件传输代理(sendmail,postfix,exim等)拒绝提供这些电子邮件.
解决方案是配置MTA以允许更大的附件.但这并不总是可行的.备用解决方案是使用SMTP.您需要访问SMTP服务器(如果您的SMTP服务器需要身份验证,则需要登录凭据):
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // set the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "yourname@yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
Run Code Online (Sandbox Code Playgroud)
PHPMailer默认使用PHP mail()函数,该函数使用php.ini通常默认使用sendmail(或类似的东西)的设置.在上面的示例中,我们覆盖了默认行为.
您的代码看起来不错,您是否忘记在服务器上安装PostFix?
sudo apt-get install postfix
Run Code Online (Sandbox Code Playgroud)
它对我有用;)
干杯
| 归档时间: |
|
| 查看次数: |
152559 次 |
| 最近记录: |