使用phpmailer发送邮件的简单示例

1.0.0 2024-03-27 13:33 UTC

This package is auto-updated.

Last update: 2024-09-27 14:41:48 UTC


README

需求

  • PHP >= 7.0
  • Composer已安装
  • 服务器邮件

安装

您可以使用行命令安装此类

composer require gabriel-binotti/email

步骤 0

您需要在 vendor/appEmail/config/email.ini 中配置文件 email.ini

host = nameserve
username = user
password = password
auth = 'true' or 'false'
port = 587 or 465
charset = 'UTF-8'
secure = 'false' or 'true'

配置完成后,您可以使用上述示例使用该类。

index.php

use GabrielBinottiEmail\Email;

require_once "vendor/autoload.php";

try {

    Email::email('email')                       // name file .ini
        ->debug()                               // active show debug (server, client or off "default is off")
        ->from("email", "name")                 // sender's email , name (Can your name or you company...)
        ->reply("email", "name ")               // answer email (optional)
        ->destination("email", 'name')          // recipient email , name
        ->subject("subject email")              // subject email
        ->bodyHtml('text body')                 // text body
        ->options([
            "ssl" => [
                'verify_peer'       => false,   
                'verify_peer_name'  => false,   
                'allow_self_signed' => true    
            ]
        ])
        ->send();                               // send email
        
} catch (Exception $e) {
    echo $e->getMessage();
}

options() 的配置基于您的服务器是否有 SSL

您可以在邮件正文中使用 HTML 模板,将 bodyHtml() 更改为 template()

    template('template.html');                    

您在 vendor/appEmail/template/ 中创建的模板,默认情况下第二个参数是空数组,但您可以发送一个数组进行替换。

$arrayReplace = [
    [
        "before" => "value",
        "after" => "value",
    ],
    [
        "before" => "value",
        "after" => "value",
    ]
];

 template('template.html', $arrayReplace);

您可以在邮件中发送文件。您需要将文件添加到 vendor/appEmail/files/

addFile("filename.format", "name file");

您可以在正文中添加图片。将图片添加到 vendor/appEmail/images/

addImage("image.format", "name image");

要添加正文中的图片,您需要创建一个模板,例如上面的示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <img src="cid:nameImage" alt="">
</body>
</html>