菜鸟实习——使用laravel框架无法在阿里虚拟主机上发邮件500的解决办法

使用laravel自带的邮件发送在本地调试成功,但上线阿里虚拟主机后一直无法发送,然后经过网上提示查看了storage/logs/laravel.log文件的错误信息:无法连接到stmp.163.com
在这里插入图片描述

发现不论怎么更改端口依旧无法发送,最后提交工单,发现原来是阿里虚拟主机不支持larvel上的一个命令
在这里插入图片描述

最后结合文档发现:阿里官方给出的文档是使用PHPMailer发送的邮件,所以就想在laravel上不用自带发邮件的转而使用PHPMailer来发邮件

在参考网上各路大神的文件后最后找的一个自己合适的


首先安装PHPMailer

composer require phpmailer/phpmailer

在app下新建目录PMail,新建php文件PMail(此代码来源于csdn上,忘了是哪个博客,如看到请留言我加上链接):

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/11
 * Time: 11:53
 */
namespace App\PMail;

use PHPMailer\PHPMailer\PHPMailer;

class PMail
{
    public static $HOST = 'smtp.163.com'; // QQ 邮箱的服务器地址
    public static $PORT = 465; // smtp 服务器的远程服务器端口号
    public static $SMTP = 'ssl'; // 使用 ssl 加密方式登录
    public static $CHARSET = 'UTF-8'; // 设置发送的邮件的编码

    private static $USERNAME = 'xxx@163.com'; // 授权登录的账号
    private static $PASSWORD = 'xxx'; // 授权登录的密码
    private static $NICKNAME = 'xxxx'; // 发件人的昵称

    /**
     * @param bool $debug [调试模式]
     */
    public function __construct($debug = false)
    {
        $this->mailer = new PHPMailer();
        $this->mailer->SMTPDebug = $debug ? 1 : 0;
        $this->mailer->isSMTP(); // 使用 SMTP 方式发送邮件
    }

    /**
     * @return PHPMailer
     */
    public function getMailer()
    {
        return $this->mailer;
    }

    private function loadConfig()
    {
        /* Server Settings  */
        $this->mailer->SMTPAuth = true; // 开启 SMTP 认证
        $this->mailer->Host = self::$HOST; // SMTP 服务器地址
        $this->mailer->Port = self::$PORT; // 远程服务器端口号
        $this->mailer->SMTPSecure = self::$SMTP; // 登录认证方式
        /* Account Settings */
        $this->mailer->Username = self::$USERNAME; // SMTP 登录账号
        $this->mailer->Password = self::$PASSWORD; // SMTP 登录密码
        $this->mailer->From = self::$USERNAME; // 发件人邮箱地址
        $this->mailer->FromName = self::$NICKNAME; // 发件人昵称(任意内容)
        /* Content Setting  */
        $this->mailer->isHTML(true); // 邮件正文是否为 HTML
        $this->mailer->CharSet = self::$CHARSET; // 发送的邮件的编码
    }

    /**
     * Add attachment
     * @param $path [附件路径]
     */
    public function addFile($path)
    {
        $this->mailer->addAttachment($path);
    }


    /**
     * Send Email
     * @param $email [收件人]
     * @param $title [主题]
     * @param $content [正文]
     * @return bool [发送状态]
     */
    public function send($email, $title, $content)
    {
        $this->loadConfig();
        $this->mailer->addAddress($email); // 收件人邮箱
        $this->mailer->Subject = $title; // 邮件主题
        $this->mailer->Body = $content; // 邮件信息
        return (bool)$this->mailer->send(); // 发送邮件
    }
}

然后在控制器中引用
use App\PMail;
$mail = new PMail\PMail();
$user = '接收邮件hello@qq.com';
$mailhtml = <<<EOF
//这里写发送内容可以是html
	EOF;

$mail->send($user, '邮件主题', $mailhtml);
最后测试可以成功发送。自此纠缠我2天的问题解决
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值