I’m trying to send an email using the following code and I’m getting Internal Server Error. I am not sure why I am having this trouble.
PHP code:
<?php
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'myemail@gmail.com';
$mail->Password = "mypasswordhere";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress('myemail@gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
?>
I just placed this file as test.php inside the PhpMail folder after extracting it. Like the below

I’ve been trying to get my PHPMailer working for over a day now reading a lot of forum posts but i can’t figure out why it keeps returning an HTTP Error 500.
MacBook-Air-van-Benjamin:~ benjaminkuijs$ composer require phpmailer/phpmailer
Using version ^6.0 for phpmailer/phpmailer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Below you can find the index.php file that i’ve created to send the email — it is based on the Gmail example which is stated within the examples page.
<?php //Import PHPMailer classes into the global namespace use PHPMailerPHPMailerPHPMailer; require '../vendor/autoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer; //Tell PHPMailer to use SMTP //$mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; // use // $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = "test.format.website@gmail.com"; //Password to use for SMTP authentication $mail->Password = "*******"; //Set who the message is to be sent from $mail->setFrom('info@qubecargo.com', 'QUBE Cargo'); //Set an alternative reply-to address $mail->addReplyTo('info@qubecargo.com', 'QUBE Cargo'); //Set who the message is to be sent to $mail->addAddress('benjaminkuijs@hotmail.com', 'Benjamin Kuijs'); //Set the subject line $mail->Subject = 'First email send using PHPMailer'; //Set email body $mail->Body = 'This is the first test Body for the PHPMailer mail'; //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }
Я продолжаю иметь внутреннюю ошибку 500 на моем веб-сервере, размещенном на GoDaddy с использованием PHPmailer; Я попробовал несколько решений, доступных на StackOverflow, касающихся моей задачи, но ни одно из них не сработало.
Вот мой код:
require 'phpmailer/PHPMailerAutoload.php';
$feedback='';
$flag = array();
if(isset($_POST["submitlogin"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$name = seo_friendly_url($_POST['name']);
$email = seo_friendly_url($_POST['email']);
$subject = seo_friendly_url($_POST['subject']);
$message = seo_friendly_url($_POST['message']);
//Email
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
$feedback="Invalid email format";
array_push($flag,"false");
}else {
array_push($flag,"true");
}
//Email
//Name
if (preg_match('/^[-a-zA-Z0-9._]+$/', $name)){
array_push($flag,"true");
}else {
$feedback="Invalid name format";
array_push($flag,"false");
}
//Nameif (!in_array("false", $flag)) {
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtpout.asia.secureserver.net";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
//$mail->Username = 'email@email.com';
//$mail->Password = 'password';
$mail->SMTPAuth = false;
//Set who the message is to be sent from
$mail->setFrom('email@email.com');
//Set an alternative reply-to address
$mail->addReplyTo($email);
//Set who the message is to be sent to
$mail->addAddress('email@email.com'); // Add a recipient
$mail->addAddress('email@email.com');
$mail->addAddress('email@email.com);
//Set the subject line
$mail->Subject = 'HLS Inquiry';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($message);$email_ar = $email;
$subject_ar = $name.", Thank you for your Inquiry";
$acknowledgement_receipt = '
<div style="padding:10px 20px;font-size:16px;">
<p>Dear '.$name.',</p>
<p>We've received your message and would like to thank you for contacting us. We will reply by email shortly.</p>
<p>Talk to you soon,</p>
<p>
<div>Customer Service</div>
</p>
</div>
';if ($mail->send()) {
$feedback = "<span style='color:green;'>Thank you for your inquiry. We will respond to you within 24 hours.</span>";
}
}
Обратите внимание, что я заменил некоторые из значений электронной почты как: email@email.com для моей конфиденциальности.
Я считал, что настройки электронной почты верны, потому что я уже использовал эти настройки с сайта моего друга, размещенного на JustHost.
0
Решение
В этой строке отсутствует ни одна кавычка:
$mail->addAddress('email@email.com);
Это довольно очевидно в SO, потому что вы можете видеть, как подсветка синтаксиса ломается в этой точке.
Когда вы получаете ошибку 500 и ничего не отображается, это признак того, что вы должны искать в журнале ошибок вашего веб-сервера.
0
Другие решения
Других решений пока нет …
I am using PHPMailer on a project I am working on, and on my localhost it works like a charm! But I am using a friend’s server to test the site for different devices. As I’m waiting for more content to plug in, I’m doing some work on the functionality of the site.
I wrote a script to go along with PHPMailer that does everything I need it to do, but on the top of the page, before my doctype, I have set a couple of session variables. After hours of tinkering with it, I have come to realize that is what’s causing my 500 error. I’ve tried moving them into the head section, but the only thing that (kind of) works on the server is commenting them out completely. But then, when I try to submit the mail, it goes to the PHP file and never redirects to the home page like it’s supposed to.
Any ideas on how to fix this? If it helps, here’s the part of my code that’s messing things up:
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
UPDATE: Here is the image for the phpinfo…
I am using PHPMailer on a project I am working on, and on my localhost it works like a charm! But I am using a friend’s server to test the site for different devices. As I’m waiting for more content to plug in, I’m doing some work on the functionality of the site.
I wrote a script to go along with PHPMailer that does everything I need it to do, but on the top of the page, before my doctype, I have set a couple of session variables. After hours of tinkering with it, I have come to realize that is what’s causing my 500 error. I’ve tried moving them into the head section, but the only thing that (kind of) works on the server is commenting them out completely. But then, when I try to submit the mail, it goes to the PHP file and never redirects to the home page like it’s supposed to.
Any ideas on how to fix this? If it helps, here’s the part of my code that’s messing things up:
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
UPDATE: Here is the image for the phpinfo…