我刚刚从github下载了phpmailer并上传到共享主机。 我创建了一个名为mailer.php的文件,并添加了这段代码(例如git-page):
当我开始此操作(打开文件)时,没有任何内容被回显,也没有收到任何邮件。如果我尝试在此代码之后回复任何类似文本的内容,它就不会这样做但是如果我把它放在它之前就会回显它。
我查了Here,但答案中的任何内容都没有...
<?php
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; //shared hosting ssl port
//Recipients
$mail->setFrom('email@mydomain.com', 'Site Title');
$mail->addAddress('myGmail@gmail.com', 'Soma name'); // Name is optional
$mail->addReplyTo('email@mydomain.com', 'Info');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>