【问题标题】:PHP error when trying to send email using Swift Mailer尝试使用 Swift Mailer 发送电子邮件时出现 PHP 错误
【发布时间】:2013-09-29 22:37:57
【问题描述】:

我已经尝试了一切,我不知道如何解决这个问题,所以,我正在使用这个 Swift Mailer 库来发送确认电子邮件。所以这是我的 index.php 中的代码

if($confirm){

            //include the swift class
            include_once 'inc/php/swift/swift_required.php';

            //put info into an array to send to the function
            $info = array(
                'username' => $username,
                'email' => $email,
                'key' => $key);

            //send the email
            if(send_email($info)){

                //email sent
                $action['result'] = 'success';
                array_push($text,'Thanks for signing up. Please check your email for confirmation!');

            }else{

                $action['result'] = 'error';
                array_push($text,'Could not send confirm email');

            }

        }

而我的 send_email 函数在另一个 php 文件 functions.php 中

//send the welcome letter
function send_email($info){

    //format each email
    $body = format_email($info,'html');
    $body_plain_txt = format_email($info,'txt');
        //setup the mailer
    $transport = Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl)
       ->setUsername('my email here')
          ->setPassword('my password');

    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance();
    $message ->setSubject('Welcome to Site Name');
    $message ->setFrom(array('somedomain.com' => 'somethinghere'));
    $message ->setTo(array($info['email'] => $info['username']));

    $message ->setBody($body_plain_txt);
    $message ->addPart($body, 'text/html');

    $result = $mailer->send($message);

    return $result;

}

我得到的错误是

致命错误:在第 31 行的 /srv/disk7/something/www/something/signup/inc/php/functions.php 中调用未定义的方法 Swift_MailTransport::setUsername()

我该如何解决这个问题?我是php初学者。

【问题讨论】:

    标签: php swiftmailer


    【解决方案1】:

    我猜Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl) 未能创建预期的实例。

    顺便说一句,不应该是Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl')即 smtp.gmail.com 和引号中的 ssl)吗?

    【讨论】:

    • 不,它必须在“”中
    • 同样的事情。我的问题仍然存在。
    • 请注意您的语言!不是每个人的“心情”都和你一样!
    【解决方案2】:

    Swift_MailTransport,来自文档...

    ...通过委托给 PHP 的内部 mail() 函数发送消息。

    根据我和其他人的经验,mail() 函数不是特别可预测或有用的。

    它没有的另一件事是setUsernamesetPassword 方法。

    我会说你想使用Swift_SmtpTransport

    还有一件事;正如其他人指出的那样,您的字符串参数应该被引用,即

    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
    

    【讨论】:

      【解决方案3】:

      看起来你还没有包含 Swift Mail 类。

      【讨论】:

        猜你喜欢
        • 2012-08-23
        • 2014-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        相关资源
        最近更新 更多