我创建了一个自定义类函数来设置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('googol8080@gmail.com', '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的新手,任何小答案都可能对我有所帮助,谢谢。
答案 0 :(得分:2)
您的代码似乎很好,但更好的方法是调用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@gmail.com";
$mail->Password = "email_password";
$mail->setFrom('email_Sent_from@gmail.com', 'Alias');
$mail->addReplyTo("email_to@gmail.com", "Alias");
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
}
}
然后你可以这样称呼它:
$email_send = new phpmailer();
$email_send->sendMail($user_email,$message,$subject);