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.
Related
We have a site on godaddy server. We have a contact form. When user filled form, first we should send mail to our info#oursite, after we should send mail to user's mail from our info#oursite. My tried code below:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->port = 25;
$mail->Host = 'localhost';
$mail->Username = 'username#example.com';
$mail->Password = 'password';
$mail->addAddress($email, $name);
$mail->subject = 'Subject';
$mail->MsgHTML('mail içeriği');
if ($mail->Send()){
this outputs below:
it seems mail delivered, but it never delivered the message.
I tried many things;
$mail->SMTPAuth = true;
port 25,80,3535,465(with ssl),
tls, ssl authentication,
what should I do? what should I try more, any ideas?
First of all, you're using a very old version of PHPMailer; get the latest.
I don't know where you got your code, but you've got the case of some vital properties wrong - I suggest you base your code on the examples provided with PHPMailer. In particular, subject should be Subject and port should be Port. When sending to localhost you don't need a username or password, so you don't need to set them. Similarly, Port defaults to 25, so you don't need to set it.
You're not specifying a From address, and it looks like whatever address you're passing to addAddress is invalid, so you're sending a message from nobody to nobody - and not surprisingly it's not going anywhere!
It looks like your message body is in Turkish (?), which will not work in the default ISO-8859-1 character set, so I suggest you switch to UTF-8 by setting $mail->CharSet = 'UTF-8';.
Check your return values when calling addAddress to make sure the submitted value is valid before trying to send to it.
With these things fixed:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer;
if (!$mail->addAddress($email, $name)) {
die('Invalid email address');
}
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 2;
$mail->Host = 'localhost';
$mail->Subject = 'Subject';
$mail->msgHTML('mail içeriği');
$mail->setFrom('me#example.com', 'My Name');
if ($mail->send()) {
echo 'Sent';
} else {
echo 'Error: '.$mail->Errorinfo;
}
I've been running PHPMailer for a year now on a php server. Everything was fine until 3 days ago when I started getting the following error:
SMTP Error: Could not authenticate.
Allow less secure apps is ON
Here is the code:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = 1;
try {
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!=''){
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "myemailpass"; // SMTP password
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
//$mail->AddReplyTo($webmaster_email, "DiFractal Customer Service");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
} catch (phpmailerException $e) {
$myfile = fopen("debug_email.txt", "w");
fwrite($myfile,$e->errorMessage() . "\n" . $mail->ErrorInfo);
fclose($myfile);//Pretty error messages from PHPMailer
} catch (Exception $e) {
$myfile = fopen("debug_email_stp.txt", "w");
fwrite($myfile,$e->getMessage());
fclose($myfile);//Pretty error messages from PHPMailer
}
}
Note I just updated PHPMailer to the latest version to try to remedy the problem but nothing has changed! The old version 5.2.2 was still having the same problem!
EDIT: I just had one successful email go through to google and sent properly. Which now makes me question if it's lag issue or something of that sort. Does anyone know how phpmailer functions under high loads or if high loads can cause the above error?
Try going to:
myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON".
Alternative:
Try changing SMTP Port to: 465 (gmail also).
I was having similar issues and needed to set the from address
$mail->setFrom('myemail#gmail.com', 'Webmaster');
Make sure you check google's usage limits! PHPMailer will not tell you particulars it will just give you the Could not authenticate error but the reason why can be because of your limits.
# https://support.google.com/a/answer/166852?hl=en
Upgraded to a new account with google business and switched to that account. Issue resolved.
I've had a look around as to why my PHP script was not sending any emails when I had the $mail->isSMTP(); set.
Before I go into the problem, I would like to point out that I am using mailgun.org as my SMTP server. My PHP script is simple, I have an HTML form that forwards data to a PHP file which in turn calls the PHPMailer script and sends an email.
Here is the code to my PHP Script:
date_default_timezone_set('Asia/Manila');
require 'phpmailer/PHPMailerAutoload.php';
// include 'phpmailer/class.phpmailer.php';
// require 'phpmailer/class.smtp.php';
$mail = new PHPMailer;
//$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.mailgun.org";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = ""; //Username removed
$mail->Password = ""; //Password removed
$mail->setFrom('', ''); //Email address and Name removed
$mail->addReplyTo($userMail, $firstName);
$mail->addAddress('emailOne#example.com', 'Name'); //Actual Values Changed
$mail->addAddress('emailTwo#example.com', 'Name'); //Actual Values Changed
$mail->addAddress('emailThree#example.com', 'Name'); //Actual Values Changed
$mail->Subject = 'New Application for website by '. $firstName;
$mail->msgHTML($theMessage);
$mail->AltBody = $theMessage;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else { //Some HTML code here
}
Now then, as you can see, I have commented out the $mail->isSMTP(); line. This is because whenever I try to use it, the code does not load, and after a long wait gives a 500 error. (Sadly, by cPanel setup does not allow me to view apache logs).
I tried importing just the class.phpmailer.php file, but that gave me a fatal error that the class SMTP was not defined, which was expected. I then included the class.smtp.php file which gave me the same 500 error.
I had a look around StackOverflow and came across this answer however it did not help my case.
I have had a look at Mailgun's logs, but they have no record of the script even trying to connect.
Additional Information: If it matters, I have the following files in the same directory:
- sendMail.php //The script above
- class.phpmailer.php
- class.smtp.php
- PHPMailerAutoload.php
- index.html //Not important in this situation.
I hope someone will be able to help me out, I'm relying on the non-smtp method for now. :/
Try this working fine..!
date_default_timezone_set('Asia/Manila');
require 'phpmailer/PHPMailerAutoload.php';
// include 'phpmailer/class.phpmailer.php';
// require 'phpmailer/class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = ""; //Username removed
$mail->Password = ""; //Password removed
$mail->addReplyTo($userMail, $firstName);
$mail->SetFrom('emailOne#example.com', 'my name');
$mail->Subject = 'New Application for website by '. $firstName;$mail->MsgHTML($theMessage);
$mail->AddAddress('aswad#yahoo.com', 'my name');
if($mail->Send()) {
echo "Message sent!";
}else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
I have the following code to send an email with phpMailer. What I don't understand is why the email is sent and everything is OK even if I use a wrong password. In that case, the headers of the email give this answer (adapted from real case): X-PHP-Originating-Script: 532:class.phpmailer.php
How can I force the login error to appear and avoid the email being sent anyway? I guess that this has something to do with the class using other methods after trying to connect to the SMTP server. I don't want the email to be sent in ALL cases, if the password has changed or service not available, I want to know it and the script to stop and throw error. I use the latest available version of the class.
require 'phpmailer/class.phpmailer.php';
$to = "someone-email#example.com";
$subject = "Hello World";
$body = "HELLO WORLD";
$mail = new PHPMailer;
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "example#example.com";
$mail->Password = "**********";
$mail->From = "example#example.com";
$mail->FromName = "TEST";
$mail->AddReplyTo($to);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "KO " . $mail->ErrorInfo;
return false;
}
else
{
echo "OK";
return true;
}
You aren't using SMTP so I guess it's defaulting to mail(), which doesn't accept authentication.
To enable SMTP:
$mail->IsSMTP();
Hello guys I get this error:
The following From address failed: username_from#gmail.com : Called Mail() without being connected
<?php
require("PHPMailer/class.phpmailer.php");
$msg = 'hello world!';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'username#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'username_from#gmail.com';
$mail->FromName = 'My name';
$mail->AddAddress('other_user#gmail.com', '');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = 'hello';
$mail->Body = $msg;
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
?>
How can I fix it? Thanks! I send email from localhost running XAMPP in PHP using Gmail mail server.
Possible reason for error:
you have to edit your gmail account permissions. Trying enabling the 'Access for less secure apps' Security setting.
may be,you're using an old version of PHPMailer - update it.
Don't use ssl/465 for gmail; use tls/587. Take a look at the gmail example code provided with PHPMailer.
You don't need to call singleTo if you're only adding one address anyway.
Try and set Host to smtp.gmail.com
you can add $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only so you can see some debug statements?