首页 > 解决方案 > 以下电子邮件功能突然停止工作

问题描述

我一直在使用以下方法发送电子邮件,效果很好!但是,在过去的几天里,它停止在我的实时服务器和本地主机上工作。我认为它应该工作?我应该尝试改用 phpmailer 吗?我之前确实在设置 phpmailer 时遇到了问题,这就是我没有使用它的原因....

$to = $email;

//sender
$from = 'pianocourse101@hotmail.com';
$fromName = 'PianoCourse101';

//email subject
$subject = 'Activate your Level 1 Monthly Membership Plan!'; 

//attachment file path
$file = "codexworld.pdf";

//email body content
$htmlContent = "<h1>Activate your Level 1 Monthly Membership Plan!</h1>
    <p>Thank you for registering your Level 1 Monthly Membership Plan with PianoCourse101! You are receiving this e-mail because you or someone else claiming to be you has bought a Level 1 Monthly Membership Plan \n\nIf you believe that this is a mistake, please send us a ticket with the subject \"How to cancel my Level 1 Monthly Membership Plan?\" and allow at least 48 hours before receiving a reply.\n\nHowever, if this is correct, then you must activate your Level 1 Monthly Membership Plan by clicking on the link below: \n\n <a href=http://localhost/loginsystem/includes/activatepremium.php?email=".htmlspecialchars($to)."&activatetoken=".htmlspecialchars($token).">Click here to activate your Level 1 Monthly Membership Plan</a>;
</p>";

//header for sender info
$headers = "From: $fromName"." <".$from.">";

//boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

//headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

//multipart boundary 
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 

//preparing attachment
if(!empty($file) > 0){
    if(is_file($file)){
        $message .= "--{$mime_boundary}\n";
        $fp =    @fopen($file,"rb");
        $data =  @fread($fp,filesize($file));

        @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" . 
        "Content-Description: ".basename($file)."\n" .
        "Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    }
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;

//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath); 

//email sending status
echo $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";

我试图从被动编码收入中学习教程,现在我更新的代码看起来像这样,但我收到以下错误。在教程中,他展示了如何同时使用 smtp 和电子邮件,但我无法让它与后者一起使用:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
include_once 'PHPMailer/PHPMailer.php';

//Load Composer's autoloader


$mail = new PHPMailer();                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('piano0011@hotmail.com', 'Recipients');
    $mail->addAddress('piano0011@pianocourse101.com', 'Joe U');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我收到此错误:

无法访问文件:/var/tmp/file.tar.gz 无法访问文件:/tmp/image.jpg

致命错误:未捕获错误:在 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php:1736 中找不到类 'PHPMailer\PHPMailer\SMTP' 堆栈跟踪:#0 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer .php(1861): PHPMailer\PHPMailer\PHPMailer->getSMTPInstance() #1 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1774): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #2 C :\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sat, 8 De...', 'This is a multi...') #3 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #4 C:\xampp\htdocs\loginsystem\phpmailer.php(41): PHPMailer\PHPMailer\ PHPMailer->send() #5 {main} 在第 1736 行的 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php 中抛出

我用 smtp 试过了,现在出现以下错误:

无法访问文件:/var/tmp/file.tar.gz 无法访问文件:/tmp/image.jpg 2018-12-08 04:21:25 SERVER -> CLIENT: 220 ME2PR01CA0106.outlook.office365.com Microsoft ESMTP MAIL 服务于 2018 年 12 月 8 日星期六 04:21:25 +0000 2018-12-08 04:21:25 客户端 -> 服务器:EHLO localhost 2018-12-08 04:21:25 服务器 -> 客户端:250 -ME2PR01CA0106.outlook.office365.com 你好 [203.192.94.108]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250 SMTPUTF8 2018-12-08 04:21:25 客户端-> SERVER 04:21:25 SERVER -> CLIENT: 220 2.0.0 SMTP server ready SMTP 错误:无法连接到 SMTP 主机。2018-12-08 04:21:25 客户端-> 服务器:退出 2018-12-08 04:21:25 2018-12-08 04:21:25 SMTP 连接()失败。https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 信息已发送

我有以下错误:

无法访问文件:/var/tmp/file.tar.gz 无法访问文件:/tmp/image.jpg 无法实例化邮件功能。信息已发送

标签: phphtmlcss

解决方案


我认为您的问题应该是因为您将个人计算机用作 SMTP 服务器,并且 hotmail.com 域上的 DKIM 或 SPF 工具正在做它的工作,阻止您的电子邮件。

您应该使用 phpMailer 库或其他类似的库,并使用有效的用户名和密码登录来自地址域的 SMTP 服务器。

require("class.phpmailer.php");
$mail = new PHPMailer();

//Luego tenemos que iniciar la validación por SMTP:
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "*** your smtp server ***";
$mail->Username = "*** your username on the smtp server ***";
$mail->Password = "*** the password for the user on the smtp server ***";

您可以在此站点上搜索有关使用 hotmail.com 作为 SMTP 服务器的其他答案,例如:使用 Hotmail smtp 在 PHP 中发送邮件


推荐阅读