【问题标题】:How to make "PHPmailer" work in a function如何使“PHPmailer”在函数中工作
【发布时间】:2020-09-25 12:18:16
【问题描述】:

我不知道如何在函数中使用 PHPmailer 发送邮件。

我想做类似的事情——当我想发送邮件时,我只需调用一个函数(如 send::mail())。这是我的代码:

<?php
require '.\vendor\autoload.php';

//PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsSendmail();
//From email address and name
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->setFrom('my.bot.mail@gmail.com', 'Bot name');
$mail->Password = "########";                       // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;         
//To address and name
$mail->addAddress("mymail@gmail.com"); //Recipient name is optional

//Address to which recipient will reply

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
$mail->Mailer = "sendmail";
if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

有人可以帮我把它放到一个函数中吗?当我尝试时,“使用”命令不起作用。

【问题讨论】:

  • 您想要自定义文本邮件或者是简单的所有用户的相同电子邮件并且您需要更改邮件用户?
  • 相同的邮件。那是我网站上的“忘记密码”选项

标签: php function class email phpmailer


【解决方案1】:

首先,您必须确定数据。

    $mailHost = '';
    $mailSMTPAuth = '';
    $mailUsername = '';
    $mailPassword = '';
    $mailSMTPSecure = '';
    $mailPort = '';
    $mailSenderAddress = '';
    $mailSenderName = '';

然后你可以生成一个只包含收件人、主题和消息的函数,你可以通过将设置拉入这个函数来轻松发送邮件:

function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {
    global $mailHost;
    global $mailSMTPAuth;
    global $mailUsername;
    global $mailPassword;
    global $mailSMTPSecure;
    global $mailPort;
    global $mailSenderAddress;
    global $mailSenderName;

    require_once('class/phpmailer/PHPMailerAutoload.php');
    $mail = new PHPMailer;
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = $mailHost; // Specify main and backup SMTP servers
    $mail->SMTPAuth = $mailSMTPAuth; // Enable SMTP authentication
    $mail->Username = $mailUsername; // SMTP username or mail
    $mail->Password = $mailPassword; // SMTP password
    $mail->SMTPSecure = $mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = $mailPort; // TCP port to connect to
    $mail->CharSet = 'utf-8';
    $mail->setFrom($mailSenderAddress, $mailSenderName);
    $mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = $sendMailSubject;
    $mail->Body = $sendMailMessage;
    if(!$mail->send()) {
        return false;
    } else {
        return true;
    }
}

函数会这样工作:

sendMail('hello@test.com', 'Test User', 'Subject of test mail', 'And your mail message);

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2012-05-21
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多