php - 如何使用phpmailer成功发送邮件
问题描述
我不使用作曲家。
我有一个简单的联系表格,我想通过我网站的电子邮件帐户将文本发送到 gmail。
http://elcomass.com/elcomass-contact.php
我从多个站点复制了代码并尝试了至少五个但没有一个有效。当前代码是我尝试过的最新代码
我尝试或查看了这些链接,但对我没有帮助。这是我最后的手段,因为我也尝试过网站上的教程。
https://github.com/PHPMailer/PHPMailer#a-simple-example
未能打开所需的 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php')
https://codeforgeek.com/phpmailer-ultimate-tutorial/
https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/
https://alexwebdevelop.com/phpmailer-tutorial/
当您提交表单时,您得到的只是一个空白页。在错误日志中,这是最常见的问题:
PHP 致命错误:在第 6 行的 /home/elcomass/public_html/elcomass-sendmail.php 中找不到类 'PHPMailer'
PHP 警告:需要(class.PHPMailer.php):无法打开流:第 2 行的 /home/elcomass/public_html/elcomass-sendmail.php 中没有这样的文件或目录
[2019 年 7 月 13 日 12:27:14 UTC] PHP 致命错误:require():无法打开所需的 'class.PHPMailer.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/第 2 行 /home/elcomass/public_html/elcomass-sendmail.php 中的 share/pear')
我试过制作文件夹,在那里手动放置 phpmailer 项目。就像现在一样,它直接放在我的 public_html 中,没有文件夹,它仍然不起作用。Exception.php、STMP.php 也一样
<?php
// This is one way I tried
use PHPMailer;
require 'PHPMailer.php';
// #2
require 'class.PHPMailer.php';
// #3
require 'PHPMailer.php';
// #4
require 'test/class.PHPMailer.php';
// #5
require 'test\class.PHPMailer.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = $_POST['email'];
$name = $_POST['name'];
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxx@xxxxxx.com'; // SMTP username
$mail->Password = 'xxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = xxx; // TCP port to connect to
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('xxx@gmail.com', 'elcomass'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
我希望联系表格将电子邮件发送到 gmail 地址。我通过 cPanel 获得了该网站的电子邮件地址规范。
解决方案
在使用 phpmailer 进行大量工作后,我设法解决了我的问题。正如错误所示,我做错了几件事,但是对于任何有兴趣的人,请小心复制有关您的域电子邮件的详细信息,并且不要在 phpmailer 代码所在的同一页面中调用 SESSION。
推荐阅读
- python - 如何在程序中使用 pytest-html 生成 html 报告和 IDE PyCharm?
- java - Guava 缓存在第一次写入后过期
- botframework - 使用表单流处理 Bot Framework 中的用户中断
- c# - ASP.Net Core 2.2 升级 - 默认路由重定向问题
- python - 使用 Python 刷新 Excel 图表外部数据链接
- c++ - 访问另一个结构中的结构字段
- vuejs2 - 如何确保在 Vue 中加载参考数据?
- python - 将文件中的数据读入字典
- c++ - 是否可以进行只读可复制(通过引用)shared_ptr?
- sharepoint-online - 如何在 Jmeter 中为 sharepoint 应用程序验证 0auth?