以下电子邮件功能突然停止工作

时间:2018-12-07 12:06:58

标签: php html css

我一直在使用以下内容发送电子邮件,效果很好!但是,在过去的几天中,它停止在我的实时服务器和本地主机上工作。我以为应该起作用?我应该尝试改用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服务器->客户:220 ME2PR01CA0106.outlook.office365.com Microsoft ESMTP MAIL服务已在2018年12月8日星期六准备04:21:25 +0000 2018-12-08 04:21:25客户端->服务器:EHLO本地主机 2018-12-08 04:21:25服务器->客户:250-ME2PR01CA0106.outlook.office365.com你好[203.192.94.108] 250尺寸157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250 SMTPUTF8 2018-12-08 04:21:25客户端->服务器:STARTTLS 2018-12-08 04:21:25服务器->客户:220 2.0.0 SMTP服务器就绪 SMTP错误:无法连接到SMTP主机。 2018-12-08 04:21:25客户端->服务器:退出 2018-12-08 04:21:25 2018-12-08 04:21:25 SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 消息已发送

我有以下错误:

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

2 个答案:

答案 0 :(得分:0)

不是答案,而是找到答案的路径

在不抑制错误的情况下尝试使用mail函数,即删除@

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

$mail = mail($to, $subject, $message, $headers, $returnpath);

这样,您将在发送电子邮件时出错。

答案 1 :(得分:0)

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

您应该使用phpMailer库或其他类似的库,并使用有效的用户名和密码登录from地址域的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服务器有关的其他答案,例如:Sending mail in PHP using Hotmail smtp