问题
I need to send emails and the mail()
function just does not work for me.
The problem is that PHP is not installed on my device. Can I still use it in, index.php
for example?
I use 000webhost
回答1:
Try this code (it works for me):
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer.php';
require 'SMTP.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'xxx'; // Change here
$mail->Password = 'xxx'; // Change here
$mail->setFrom('xxx', 'Mailer'); // Change here
$mail->addAddress('xxx'); // Change here
$mail->isHTML();
$mail->CharSet = 'utf-8';
$mail->Subject = 'Subject';
$mail->Body = 'Hello world';
echo $mail->Send() ? 'OK!' : 'Something went wrong';
Be sure that:
- the files
PHPMailer.php
andSMTP.php
are in the same folder of the PHP script (I suggest you to put them all in the root of your website, for now); - the PHP script (containing the above code) is UTF-8 encoded.
来源:https://stackoverflow.com/questions/65220766/how-can-i-use-phpmailer-without-composer