我制作了一个自定义类函数来设置 PHPMailer 所需的基本信息(因此我不需要每次都输入它)。这是该函数的确切代码。
<?php
class PHPMailer {
public static function send() {// I will just add here the addAddress
require_once 'mail/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "validusername";
$mail->Password = "validpassword";
$mail->setFrom('validusername', 'Valid Username');
$mail->addAddress('[email protected] /cdn-cgi/l/email-protection', 'Googol');
$mail->Subject = "Subject";
$mail->Body = "<a href=\"www.google.com\">www.google.com</a>";
$mail->IsHTML(true);
if (!$mail->send()) {
return "Error sending message" . $mail->ErrorInfo;
} else {
return "Message sent!";
}
}
}
到目前为止,它正在我的本地主机上运行,但我有疑问:
- 这是一个好的做法吗?
- 代码没问题吗?
- 这有什么缺点吗?
- 如果需要在这里优化性能,我需要做什么才能实现它?
我对 PHP 和 PHPMailer 非常陌生,任何小答案都可能对我有帮助,谢谢。
您的代码看起来不错,但更好的方法可能是调用variables
,因此您不必每次调用该类时都进行配置。
class phpmailer {
public function sendMail($email, $message, $subject)
{
require_once('../phpmailer/class.phpmailer.php');
require_once('../phpmailer/class.smtp.php');
require_once('../phpmailer/class.pop3.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->addAddress($email);
$mail->Username = "[email protected] /cdn-cgi/l/email-protection";
$mail->Password = "email_password";
$mail->setFrom('[email protected] /cdn-cgi/l/email-protection', 'Alias');
$mail->addReplyTo("[email protected] /cdn-cgi/l/email-protection", "Alias");
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
}
}
那么你可以这样称呼它:
$email_send = new phpmailer();
$email_send->sendMail($user_email,$message,$subject);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)