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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料