无法让Swiftmailer在PHP网站上工作

时间:2015-07-21 16:16:09

标签: php swiftmailer

我是PHP和Swiftmailer的新手。 我想要做的是在网络服务器上建立一个PHP站点,并使用SwiftMailer发送电子邮件。

我得到的代码可以在我的本地XAMPP服务器上运行,但会产生错误消息:

“致命错误:在[我的php文件的地址]中找不到类'Swift_Attachment'”

从在线网络服务器执行时。

这是我的代码:

<?php

    // Get this directory, to include other files from
    $dir = dirname(__FILE__);

    // Get the contents of the pdf into a variable for later
    ob_start();
    require_once($dir.'/pdf_selbst_lir.php');
    $pdf_html = ob_get_contents();
    ob_end_clean();

    // Load the dompdf files
    require_once($dir.'/dompdf/dompdf_config.inc.php');

    $dompdf = new DOMPDF(); // Create new instance of dompdf
    $dompdf->load_html($pdf_html); // Load the html
    $dompdf->render(); // Parse the html, convert to PDF
    $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later    

    // Get the content of the HTML email into a variable for later
    ob_start();
    require_once($dir.'/Templates/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();                                                                 

    // Swiftmailer
    require_once($dir.'/swiftmailer-5.0.1/lib/swift_required.php'); 

    // Create the attachment with your data
    $attachment = Swift_Attachment::newInstance($pdf_content, 'pdfname.pdf', 'application/pdf');     

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mymailserver', 587, 'tls')
        ->setUsername('username')
        ->setPassword('password')
        ;
    // Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);

    // Create the message
    $message = Swift_Message::newInstance()             
                ->setFrom(array('senderaddress' => 'Sender Name'))      
                ->setSubject('subject')
                ->addBcc('somebccaddress')              
                ->setBody($html_message, 'text/html')               
                ->attach($attachment);  

                try {
                 $message->addTo('somerecipient'); 
                    }
                catch (Swift_RfcComplianceException $e)
                    {
                        // Address was invalid
                        echo "Email address not correct.";
                    }   

    // Send the message
      if ($mailer->send($message))
              $success = true;
        else
                $error = true;

?>

请注意,当我注释掉所有与附件相关的内容时,错误消息会切换到

“致命错误:在[我的php文件的地址]中找不到类'Swift_SmtpTransport'”并指向“$ transport”行。

所以看起来整个SwiftMailer不起作用,这与附件无关。

非常感谢帮助!

2 个答案:

答案 0 :(得分:0)

我不看 - &gt; setTo(your_mail_dest)和 - &gt; send()

尝试像这样的简单发送

$message = Swift_Message::newInstance()             
            ->setFrom(array('senderaddress' => 'Sender Name')) 
            ->setTo( **** your_mail_dest ****)     
            ->setSubject('subject')
            ->addBcc('somebccaddress')              
            ->setBody($html_message, 'text/html')               
            ->attach($attachment)
            ->send();  

答案 1 :(得分:0)

案件结案。

事实证明,代码工作正常。我需要做的就是升级Swiftmailer(现在运行在5.4.2-DEV上)。对不起,麻烦和感谢scaisEdge的帮助!