dousi5501 2016-06-14 09:56
浏览 95

使用SwiftMailer通过Gmail发送电子邮件

I'm using Slim PHP framework and trying to send e-mail via Gmail. The problem is when I am sending the e-mail, it will cause "Slim Application Error."

$mailer->send($message);

Here is my function to send the e-mail

function send_email($email)
{
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls')
        ->setUsername('pandala61@gmail.com')
        ->setPassword('yourpassword');

    $mailer = Swift_Mailer::newInstance($transport);


    // Create a message
    $message = Swift_Message::newInstance('Test mail')
        ->setFrom(array('from_email@gmail.com' => 'SENDER'))
        ->setTo(array('To_email@gmail.com'))
        ->setBody("Hello, test send mail");

    // Send the message
    $mailer->send($message);

} 
  • 写回答

1条回答

  • doumie6223 2016-08-15 15:09
    关注

    Before you use it, you must prepare your gmail account to do it: https://support.google.com/accounts/answer/2461835?hl=en Initializing: // Create Transport. You can use it in directly in routes.php (not good practice), in your config.php file is best. $transport = Swift_SmtpTransport::newInstance() ->setHost('smtp.gmail.com') ->setPort(465) ->setEncryption('ssl') ->setUsername('emailat@gmail.com') ->setPassword('passw') ;

        // Create Mailer instance with our Transport. To be ready to call it.
    
    $mailer = Swift_Mailer::newInstance($transport);
    </code>
    Using: (Where you need to use it. eg: in UserController.php , you can use it.
    <code>
    
        $message = Swift_Message::newInstance()
                        ->setSubject('Email from contact form')
                        ->setFrom(array('email@email.com'=> 'Me'))
                        ->setTo(array('youremail@email.com' => 'You'))
                        ->setBody("I am sending you an email from contact form");
    
    $result = $mailer->send($message);
    
    
        // Print the results, 1 = message sent!
        print($result);
    </code>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容