2015-04-07 75 views
10

有没有人有一个工作示例,我如何在Laravel 5中使用PHPMailer?在Laravel 4中,使用简单,但在L5中不起作用。这是我在做L4:Laravel 5和PHPMailer

添加在composer.json

"phpmailer/phpmailer": "dev-master", 

而在controller我用这样的:

$mail = new PHPMailer(true); 
try { 
    $mail->SMTPAuth(...); 
    $mail->SMTPSecure(...); 
    $mail->Host(...); 
    $mail->port(...); 

    . 
    . 
    . 

    $mail->MsgHTML($body); 
    $mail->Send(); 
} catch (phpmailerException $e) { 
    . 
    . 
} catch (Exception $e) { 
    . 
    . 
} 

但它不工作, L5。任何想法?谢谢!

+0

定义“不起作用”。任何具体的错误,例外,什么? –

+0

为什么你想首先使用PHPMailer? Laravel具有很好的内置SwiftMailer实现。 –

+0

是的,我会使用它,但它是一个遗留代码,我必须在第一阶段从4.2升级到5。之后我会将其转换为SwiftMailer。 –

回答

9

那么我认为有多个错误... 这是一个在Laravel 5中使用PhpMailer发送邮件的工作示例。只是对它进行了测试。

 $mail = new \PHPMailer(true); // notice the \ you have to use root namespace here 
    try { 
     $mail->isSMTP(); // tell to use smtp 
     $mail->CharSet = "utf-8"; // set charset to utf8 
     $mail->SMTPAuth = true; // use smpt auth 
     $mail->SMTPSecure = "tls"; // or ssl 
     $mail->Host = "yourmailhost"; 
     $mail->Port = 2525; // most likely something different for you. This is the mailtrap.io port i use for testing. 
     $mail->Username = "username"; 
     $mail->Password = "password"; 
     $mail->setFrom("[email protected]", "Firstname Lastname"); 
     $mail->Subject = "Test"; 
     $mail->MsgHTML("This is a test"); 
     $mail->addAddress("[email protected]", "Recipient Name"); 
     $mail->send(); 
    } catch (phpmailerException $e) { 
     dd($e); 
    } catch (Exception $e) { 
     dd($e); 
    } 
    die('success'); 

和当然,你需要添加depency到composer.json

但是之后做了作曲家的更新,我宁愿建于SwiftMailer的laravel。 http://laravel.com/docs/5.0/mail

+0

正如我所看到的,除了* @ +%之外,我的示例完全一样! root namespace backslash char ... :)我的眼睛只是略过了这一点,我不知道为什么... :(所以,谢谢,它现在正在工作。是的,我会将代码更改为Laravel的内置邮件,但它是一个遗留代码,首先我必须从4.2升级到5,然后我会做一些改进。再次感谢! –

+0

当我说多个错误,我认为你使用的东西像$ mail-> Host =“yourmailhost “;不是作为一个任务,而是作为函数$ mail-> Host(”yourmailhost“);但我明白,你只是想指出你通常做了什么,我不应该把它看作正确的语法。很高兴帮助你;) –

+0

任何人都可以回答,我怎样才能在相同的设置中使用NONSSL? 未使用ssl laravel会给出错误,而我没有使用SSL凭证 –

4

第一步:打开命令提示符,运行以下命令

composer require phpmailer/phpmailer 

第二步:在您的控制器添加以下或者要使用邮件功能

use PHPMailerAutoload; 
use PHPMailer; 

第3步:使用以下代码发送邮件

$mail = new PHPMailer; 

// notice the \ you have to use root namespace here 
try { 
$mail->isSMTP(); // tell to use smtp 
$mail->CharSet = “utf-8”; // set charset to utf8 
$mail->Host = $_SERVER[‘MAIL_HOST_NAME’]; 
$mail->SMTPAuth = false; 
$mail->SMTPSecure = false; 
$mail->Port = 25; // most likely something different for you. This is the mailtrap.io port i use for testing. 
$mail->Username = “”; 
$mail->Password = “”; 
$mail->setFrom(“[email protected]”, “examle Team”); 
$mail->Subject = “examle”; 
$mail->MsgHTML(“This is a test new test”); 
$mail->addAddress(“[email protected]”, “admin”); 
$mail->addAddress(“[email protected]”, “test”); 
$mail->addReplyTo(‘[email protected]’, ‘Information’); 
$mail->addBCC(‘[email protected]’); 
$mail->addAttachment(‘/home/kundan/Desktop/abc.doc’, ‘abc.doc’); // Optional name 
$mail->SMTPOptions= array(
‘ssl’ => array(
‘verify_peer’ => false, 
‘verify_peer_name’ => false, 
‘allow_self_signed’ => true 
) 
); 

$mail->send(); 
} catch (phpmailerException $e) { 
dd($e); 
} catch (Exception $e) { 
dd($e); 
} 
dd(‘success’); 

更多信息:点击这里查看:http://programmerlab.com/question/how-to-use-phpmailer-in-laravel/

+0

你甚至没有尝试改变我的PHP评论.... –

0

在Laravel 5.5或以上版本,你需要做以下步骤

您laravel应用程序安装的PHPMailer的。

composer require phpmailer/phpmailer 

然后转到您想要使用phpmailer的控制器。

<?php 
namespace App\Http\Controllers; 

use PHPMailer\PHPMailer; 

class testPHPMailer extends Controller 
{ 
    public function index() 
    { 
     $text    = 'Hello Mail'; 
     $mail    = new PHPMailer\PHPMailer(); // create a n 
     $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
     $mail->SMTPAuth = true; // authentication enabled 
     $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail 
     $mail->Host  = "smtp.gmail.com"; 
     $mail->Port  = 465; // or 587 
     $mail->IsHTML(true); 
     $mail->Username = "[email protected]"; 
     $mail->Password = "testpass"; 
     $mail->SetFrom("[email protected]", 'Sender Name'); 
     $mail->Subject = "Test Subject"; 
     $mail->Body = $text; 
     $mail->AddAddress("[email protected]", "Receiver Name"); 
     if ($mail->Send()) { 
      return 'Email Sended Successfully'; 
     } else { 
      return 'Failed to Send Email'; 
     } 
    } 
}