我试图在GoDaddy上使用PHPMailer,这是我第一次使用它--但是,它给我的错误。Parse error: syntax error, unexpected 'install' (T_STRING)
; 在行 composer install
,到底是什么问题?我看了一下其他的帖子,也玩了一下代码,但是我还是不知道哪里出了问题,任何帮助都是非常感激的!我想在GoDaddy上使用PHPMailer,这是我第一次使用,但是它给我的错误是:解析错误:语法错误,意外的'安装'(T_STR)。
<?php
composer install
composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'stmp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'MY EMAIL';
$mail->Password = 'MY PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('MY EMAIL', 'MY NAME');
$mail->addAddress('xxx');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the main message';
$mail->AltBody = 'Some body text';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
笔记: 如果你使用的是go daddy的共享主机,而不是专用服务器,我想你将无法运行composer,你将只能使用SMTP配置。
不然就试试这个
通过命令行运行这些命令。
composer require phpmailer/phpmailer
在您的目录中会出现一个名为vendor的文件夹。
你的php文件应该是这样的
索引.php
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'stmp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'MY EMAIL';
$mail->Password = 'MY PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('MY EMAIL', 'MY NAME');
$mail->addAddress('xxx');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the main message';
$mail->AltBody = 'Some body text';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}