尝试使用Swift Mailer发送电子邮件时出现PHP错误

时间:2013-09-29 22:37:58

标签: php swiftmailer

我尝试了一切,我不知道如何解决这个问题,所以,我正在使用这个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的初学者。

3 个答案:

答案 0 :(得分:0)

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

顺便说一句,不应该是Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl')即smtp.gmail.com和ssl in quotes )?

答案 1 :(得分:0)

Swift_MailTransport,来自文档......

  

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

     

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

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

我想你要使用Swift_SmtpTransport

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

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

答案 2 :(得分:-1)

看起来你还没有收到Swift Mail类。

相关问题