首页 > 解决方案 > 如何使用phpmailer成功发送邮件

问题描述

我不使用作曲家。

我有一个简单的联系表格,我想通过我网站的电子邮件帐户将文本发送到 gmail。

http://elcomass.com/elcomass-contact.php

我从多个站点复制了代码并尝试了至少五个但没有一个有效。当前代码是我尝试过的最新代码

我尝试或查看了这些链接,但对我没有帮助。这是我最后的手段,因为我也尝试过网站上的教程。

phpmailer无法发送邮件

phpmailer不发送邮件

https://github.com/PHPMailer/PHPMailer#a-simple-example

使用 PHPMailer (SMTP) 发送邮件

未能打开所需的 '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/

https://www.inmotionhosting.com/support/email/send-email-from-a-page/using-phpmailer-to-send-mail-through-php

目前正在研究: https ://www.google.com/search?client=firefox-bd&q=PHP+Fatal+error%3A++require%28%29%3A+Failed+opening+required+%27class.PHPMailer.php% 27

当您提交表单时,您得到的只是一个空白页。在错误日志中,这是最常见的问题:

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 获得了该网站的电子邮件地址规范。

标签: phpemailphpmailer

解决方案


在使用 phpmailer 进行大量工作后,我设法解决了我的问题。正如错误所示,我做错了几件事,但是对于任何有兴趣的人,请小心复制有关您的域电子邮件的详细信息,并且不要在 phpmailer 代码所在的同一页面中调用 SESSION。


推荐阅读