I keep on having the 500 internal sever error with my web hosted by GoDaddy, using the PHPmailer; I've tried several solutions available on StackOverflow relating to my challenge, but none of them worked.
Here is my code:
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");
}
//Name
if (!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>";
}
}
Please note that I replaced some of the email values as: email#email.com for my privacy.
I believed that the email setups are correct because I already used those setups from my friend's website hosted by JustHost.
This line is missing a single quote:
$mail->addAddress('email#email.com);
This is quite obvious in SO because you can see how the syntax highlighting breaks at that point.
When you get an error 500 and nothing is displayed, it's a sign that you should be looking in your web server's error log.
There is a "split" function used to write the PHPMailer class. This function has been long deprecated and as such gives error in later PHP versions.
Find them and replace with "explode".
This worked for me.
Related
I've been using PHPMailer for years and have been pretty happy with it, but now I'm trying to get a contact form working with an email address that is domain based but tied to Gmail via Google Apps. I.e. the MX record points to Google. I've added PHPMailer's Gmail example trying to get the setup to work, but at this point, I'm not connecting; I get this message:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
Here's my form processor code:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
//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');
require 'mailer/PHPMailerAutoload.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;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemailaddress.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
//$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('myemailaddress', 'my name');
//Set the subject line
$mail->subject = $subject;
$mail->Body = "Name : $name\n\n"
. "Email : $email\n"
. "Telephone : $tel\n"
. "Message :\n\n $message\n"
. "";
//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(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
To answer some questions in advance: yes, SSL rather than TLS is enabled in Gmail, and the MX record is changed at the host. I also did comment out some stuff, but as far as I can tell nothing that I need to get the form to actually send.
Beyond that, I don't even qualify as a hack with PHP; my primary skills are design, HTML and CSS, so I'm probably missing something obvious....
Thanks in advance.
Once upon a time, I had a nicely-functioning version of a mail script that used an old version of PHPmailer. Without really knowing PHP, I managed to push it reasonably far (in part by using Forms to Go and doing some mods), and got it to redirect to both custom "incomplete" and "thank you" pages, and getting it to refuse to send if the required fields weren't filled out.
I've had to go back to the drawing board a bit as now routing domain-based email through Gmail has become a quite dominant practice. In order to get there, I updated to the latest PHPmailer and finally got it functioning for Gmail setup after a lot of trial and error and research. But in the midst of that, I've lost some of that functionality. The script below works to send mail—but it sends even if the required fields (indeed, everything!) are all empty. So it needs to:
stop sending when the fields aren't filled in properly, and
hopefully redirect to my custom page rather than just display a generic message.
<?php
// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
//set required fields + redirect if incomplete
//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');
require 'mailer/PHPMailerAutoload.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 = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
// Gmail pulls from custom domain due to alteration of MX records
$mail->Host = 'server.mydomain.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "me#myemailaddress.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('me#myemailaddress.com', 'web form');
//Set who the message is to be sent to
$mail->addAddress('me#myemailaddress.com', 'My Name');
//Set an alternative reply-to address
$mail->clearReplyTos();
$mail->addReplyTo($address);
//Set the subject line
$mail->Subject = $subject;
$mail->Body = "Name : $name\n\n"
. "Email : $address\n"
. "Telephone : $tel\n"
. "Message :\n\n $message\n"
. "";
//send the message, check for errors
if (!$mail->send()) {
$output .= "Mailer Error: ". $mail->ErrorInfo;
}
else
{
ob_clean();
header('Location: thankyou.php');
exit();
}
echo $output;
Can anyone help me understand how to edit this in order to get that functionality?
A honey pot would be a bonus too, but perhaps that's asking too much. [Edit: Looks like a honey pot solution in this thread will work: Receiving Spam from my Form Using PHPMailer ... will test tomorrow.]
<?php
/**
*/
if( !isset($_REQUEST['name'],$_REQUEST['address'],$_REQUEST['subject'],$_REQUEST['message'])
||
( !$_REQUEST['name'] || !$_REQUEST['address'] || !$_REQUEST['subject'] || !$_REQUEST['message'])
){
/**
* It means that the required field
* is not filled up all
* if any one filed is not required, then remove that from the above condition the whole $_REQUEST['var_not_required']
*/
/**
* you can display a error message or redirect
* 1. Error message
* 2. Redirect
*/
#1
die("Some of the fields are not filled up properly");
#2
header('location: http://yoururl');
/**
* Use exit to stop execution of the rest, to prevent sending or attempt to send
* with exit
*/
exit();
}
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$tel = $_REQUEST['address'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
I have been trying to make the contact form on my website to work and I've spent weeks trying to figure it out and I couldn't.
Here's the problem - I purchased a web template and it came with the PHPMailer. I'm now done plugging my content into the template, but the contact form has been a pain. I've followed the instructions the best I know on the PHP file, but it's giving me an "Internal Server Error" when I am testing the contact form.
Here's the code that came with my purchase:
$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$site_owners_email = 'name#mydomain.com'; // Replace this with your own email address
$site_owners_name = 'My Name'; // Replace with your name
try {
require_once('/Beta-BRC/php/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "[WEB Form] ".$subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $message;
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com"; // Replace with your SMTP server address
$mail->Port = 465;
$mail->SMTPSecure = "SSL";
$mail->SMTPAuth = true; // Turn on SMTP authentication
$mail->Username = "name#mydomain.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
//echo "true";
if($mail->Send()) {
echo "true";
} else {
echo "Error sending: " . $mail->ErrorInfo;
}
} catch (Exception $e) {
echo $e;
}
Quick note - I've alrealy tried using a GMAIL account on this part but it still does not work.
$mail->Username = "name#mydomain.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
There's no need to log into Gmail with phpmailer. Below is an example of my phpmailer function using the default settings.
public function sendEmail($toaddress,$toname,$subject,$message){
if($template = file_get_contents('/home/username/domains/mydomain.com/public_html/html/email-template.html')){
$template = str_replace("[SUBJECT]",$subject,$template);
$template = str_replace("[CONTENT]",nl2br($message),$template);
$mailer = new PHPMailer;
$mailer->XMailer = "Organization Name 4.0.0";
if($this->is_logged_in()){
$mailer->AddCustomHeader("X-Originating-User-ID",$this->acct['id']);
}
$mailer->AddCustomHeader("X-Originating-IP",$_SERVER['REMOTE_ADDR']);
$mailer->setFrom("outbound#mydomain.com","From Name");
$mailer->AddAddress($toaddress,$toname);
$mailer->Subject = $subject;
$mailer->MsgHTML($template);
$mailer->AltBody = $message;
return $mailer->Send();
}else{
return false;
}
}
The email address listed doesn't actually exist. The email is just being sent from my server and phpmailer just says it's from that email address.
Try modifying my function to suit your needs and let me know how that works.
Note: You'll need to make sure your mail server is turned on for this to work
Although you don't have to use my function at all. Try debugging your code by checking some error logs on your server. Typically in the apache error logs (if you're running apache, however). Checking error logs is a huge part of troubleshooting your code and often can help you become more proactive.
I hope this helps even the slightest!
The specific cause of the Internal Server Error is the incorrect path you've supplied to the require_once statement that loads the PHPMailer class.
The path you've supplied is /Beta-BRC/php/PHPMailer/class.phpmailer.php, where the correct statement should be
require_once('/home/trsta/public_html/Beta-BRC/php/PHPMailer/class.phpmailer.php');
or perhaps more generally:
require_once($_SERVER['DOCUMENT_ROOT'].'/home/trsta/public_html/Beta-BRC/php/PHPMailer/class.phpmailer.php');
You've provided effectively a URL, but PHP requires the path in the server file system, which is not the same.
That should get you past this error. It's possible that there are others.
I have a script that sends emails to users, which are sometimes divided into multiple messages depending on size. Here's the code:
for($i=0; $i<count($the_message); $i++){
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "my.smtp.server";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->IsHTML($is_html);
if($is_html === TRUE){
$mail->AltBody = "To view the message, please use an HTML compatible email client, or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
$mail->Body = $the_message[$i];
$mail->Subject = $the_sub;
$mail->Send();
}
This used to work great. Now, it seems that certain messages will never send. It seems to be a data issue (or perhaps the way PHPMailer sends lines to the server?), but I can't pinpoint why certain message segments are always rejected by the server.
The error message I get from PHPMailer is this:
SMTP Error: Data not accepted.
And from PHP itself:
Notice: fputs(): send of 31 bytes failed with errno=104 Connection
reset by peer in class.smtp.php on line 580
Here is a segment of HTML data that always fails to send:
http://pastebin.com/LGYkTTFA
Note that size isn't an issue, I can successfully send much larger HTML messages than this. However, when I send multiple segments, this one is never accepted by the server.
Here, I rewrote it for you in a somewhat saner way. You don't need to create everything from scratch every time around the loop, just change the body and send again.
<?php
require 'PHPMailerAutoload.php';
$the_message = array('Hello', 'there', 'Justin');
$to = array('tom#example.com', 'dick#example.net', 'harry#example.org');
$the_sub = 'Crazy subject';
$email_port = 25;
$from = 'justin#example.com';
$is_html = true;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "localhost";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->Subject = $the_sub;
$mail->IsHTML($is_html);
$mail->WordWrap = 200;
if($is_html){
$mail->AltBody = "To view the message, please use an HTML compatible email client, '.
'or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
for($i=0; $i<count($the_message); $i++){
$mail->Body = $the_message[$i];
$mail->setWordWrap();
$mail->Send();
}
Update: added word wrapping to deal with content with very long lines.
In class.smtp.php, line 51, I changed the constant MAX_LINE_LENGTH from 998 to 500. This allowed all of my email segments to pass through.
I'm still not sure if the issue lies with my SMTP server or PHPMailer, I'll have to test it elsewhere.
EDIT: Tested using Gmail's SMTP server, and keeping 998 worked fine. This leads me to assume that my SMTP relay server is rejecting data sometimes depending on the length.
EDIT2: Still experienced failures with the above fix. I changed my port number and added SSL, and it is magically working. Wonky server issues...
I have a php page containing PHPMailer. Apperently it was working in both the test server and the production server. It was in a separate php file, but since I need to do send different kinds of email with different alert messages on screen, I placed my code on the same php file as my form.
What I did is:
if(isset($_POST))
{
require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer(true);
if(isset($_POST['feedback_mail']))
{
$mail->Host = "my mail host"; // SMTP server
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = "the account that I will be using"; // SMTP account username
$mail->Password = "my password";
$mail->AddReplyTo($_POST['Email'], $_POST['Name']);
$mail->AddAddress('address to where I will send it', 'Name');
$mail->SetFrom($_POST['Email'], $_POST['Name']);
$mail->Subject = 'Subject'.$_POST['Subject'];
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($_POST['Body']);
$mail->Send();
$marker = 1;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
$marker = 0;
} else {
$marker = 1;
}
}
}
When I try to send an email message, it says that the message is sent, but I have been waiting for almost an hour but there is still no message recieved on my email. I know that my test server allows sending of email because I also have another application that can send mails. And I am using the same account for sending these mails. How do I know if PHPMailer is really working or not?
EDIT: I found out it might probably be my local network connection that's stopping the mail. I just tried running the from on a VPN connection and somehow it recieved an email.
Either my network connection is having problems or the server is deliberately blocking IP's from my country.