I have done a contact form and obviously I would like to receive a confirmation email when a user fill out this form on my website.
I'm using PHPMailer. When I test the code on my computer as localhost, everything works fine and confirmation message is received properly.
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("myemail#gmail.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
The problem is when I upload this PHP script on a web hosting (i.e 123-reg.co.uk), it doesn´t work.
Error message:
SMTP Error: Data not accepted
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("email#mydomainat123-reg.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.123-reg.co.uk";
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "email#mydomainat123-reg.com";
$mail->Password = "password";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
I have tried uploading and executing this PHP script from another free web hosting but it was to not avail.
I'm kind of new in PHP, especially in dealing with mail functions.
What can be the reason? Any additional help would be appreciated.
Thanks in advance for your help.
Your server doesn't allow different sender and username you should config: $mail->From like $mail->Username
Moreover...
Most times I've seen this message the email gets successfully sent anyway, but not always. To debug, set:
$mail->SMTPDebug = true;
You can either echo the debug messages or log them using error_log():
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';
A likely candidate especially on a heavily loaded server are the SMTP timeouts:
// default is 10
$mail->Timeout = 60;
class.smtp.php also has a Timelimit property used for reads from the server!
Hope this information helps! :)
Related
I am trying to send emails using SMTP Server in Core HP. I am getting a blank page not displaying any errors. Can any one help me out? what i am doing wrong? Unable to check the issue as well!
<?php ob_start();
if(isset($_POST['submit_contact'])) {
require 'PHPMailer/PHPMailerAutoload.php';
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "password1#3";
$mail->SetFrom('$email');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
is your source code located at public_html folder?
In that case you have to include like below
require 'aaa/PHPMailer/PHPMailerAutoload.php';
I designed my website and hosted on my amazon ec2 instance and I bought my domain in godaddy (www.mydomain.com).Now I want a mail configuration in my contact form page in website.. Below its my code , I don't know where am I mistake the code?
<?php
if(isset($_REQUEST['submit']))
{
try
{
$name = $_POST['name'];
echo "<script type='text/javascript'>alert('$name')
</script>";
$email = $_POST['email'];
echo "<script type='text/javascript'>alert('$email')
</script>";
$subject = $_POST['subject'];
echo "<script type='text/javascript'>alert('$subject')
</script>";
$message = $_POST['message'];
echo "<script type='text/javascript'>alert('$message')
</script>";
$response ="";
$body = <<<EOD
<div style='font-size:18px'>
<b> Name </b> : $name <br />
<b> Email address </b> : $email <br />
<b>Message </b> : $message <br />
</div>
EOD;
$to = "XXXXX#gmail.com";
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.phpmailer.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.smtp.php');
$mail = new PHPMailer(true);
//$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
// echo $res;
$mail->IsSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPDebug=true;
$mail->SMTPAuth = true; // Auth Type
$mail->Port = 25;
$mail->IsSendmail();
//$mail->SMTPSecure = "ssl";
$mail->Username = "support#mydomain.com";
$mail->Password = "******";
$mail->Sender = "supportexample#mydomain.com";
$mail->From = "supportexample#mydomain.com";
$mail->AddReplyTo($email);
$mail->FromName = "Example";
$mail->AddAddress($to);
//$mail->AddAddress("desired recipient no.2 optional");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body=$body;
$mail->WordWrap = 50;
$mail->Send();
echo "<script type='text/javascript'>alert('Mail Send Successfully')
</script>";
}
catch (phpmailerException $e) {
echo "<script type='text/javascript'>alert('Failed')
</script>";
echo $e->errorMessage();
}
}
?>
It gives an error
Could not execute: /var/qmail/bin/sendmail
Try This One. This Might Helpful. You Have To Use Different Email For From Mail.
Instead Of Using
$mail->From = "support#mydomain.com"
You Have To Use Another Email Here :
$mail->From = "supportexample#mydomain.com";
require_once('phpmailer/class.phpmailer.php');
require_once('phpmailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com"; // SMTP HOST
$mail->SMTPAuth = true;
$mail->Username = "support#mydomain.com"; // SMTP username
$mail->Password = "********"; // SMTP password
$mail->SMTPSecure = "ssl"; // Enable TLS encryption, `ssl` also accepted
$mail->Port = "465"; // TCP port to connect to 587
$mail->From = "supportexample#mydomain.com";
$mail->FromName = "Domain Example";
$mail->addAddress("XXXX#gmail.com");
$mail->addReplyTo("supportexample#mydomain.com");
$mail->isHTML(true);
$mail->Subject = "Hello Test";
$mail->Body = "Test Message Working With Us";
if($mail->send()){
return true;
}
else{
return $mail->ErrorInfo;
}
I'm using Migadu mail server and PHP Mailer to set up a simple contact form.
This is the PHP file:
<?php
require("includes/class.phpmailer.php");
require("includes/class.smtp.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$smtpHost = "smtp.migadu.com";
$smtpUsername = "info#mywebsite.com";
$smtpPassword = "mypassword";
$to = 'info#mywebsite.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Host = $smtpHost;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->From = $smtpUsername;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->AddReplyTo($email);
$mail->Subject = "Contact Form";
$body = '<h1>Contact!</h1>';
$body .= '<p><b>Name: </b>'.$name.'</p>';
$body .= '<p><b>Email: </b>'.$email.'</p>';
$body .= '<p><b>Message: </b>'.$message.'</p>';
$mail->Body = $body;
$sentStatus = $mail->Send();
if($sentStatus){
echo json_encode(['status' => 'ok']);
}else{
echo json_encode(['status' => 'error', 'errorType' => 'server']);
}
?>
But it's not sending the mail. Also, it's not throwing any error, it just stuck in loading forever.
It's not showing any error because you're not displaying any errors - look in the ErrorInfo property. All of the examples provided with PHPMailer do this, so go look at them to see how to do that.
It's most likely that it's not "stuck forever", it's just that the timeout is long, and that's probably because your ISP blocks outbound SMTP, which is very common. The troubleshooting guide tells you how to diagnose this. Your ISP probably has an alternative method for sending email, for example they may provide their own relay, so you should refer to their docs.
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$mailbody="";
$mailbody.="Hello,<br/><br/>You have a new contact on your website:<br/><br/>";
$mailbody.="Name: ".$name."<br />\r\n";
$mailbody.="Email: ".$email."<br />\r\n";
$mailbody.="Phone: ".$phone."<br />\r\n";
$mailbody.="Message: ".$message."<br />\r\n";
require_once('./bat/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.office365.com"; // SMTP server
$mail->Username = "email#domain.com";
$mail->Password = "****";
$mail->Port = 587; // optional if you don't want to use the default
$mail->IsHTML(true);
$mail->From = "email#domain.com";
$mail->FromName = "Website Name";
$mail->Subject = "Subject";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($mailbody);
// Add as many as you want
$mail->AddAddress("email#domain.com", "Name");
if(!$mail->Send()) {
//header("Location:contact.php?sent=0#sent");
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header("Location:contact.php?sent=1#sent");
}
When I test on localhost the email sends successfully and gets delivered.
When I test online it throws the following error: Mailer Error: SMTP connect() failed.
I tried to remove $mail->IsSMTP(); as mentioned in one of the suggestions. The scripts runs online and returns true but the email never gets delivered.
What am I doing wrong. There is a html form that takes three inputs Name, Notes, and Phone. The action on the form is mail.php and method is POST.
Here is the code in mail.php. On success I want it to return to homepage.
<?php
// multiple recipients
//$to = 'waleed.dogar#gmail.com' . ', '; // note the comma
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('/phpmailer/class.phpmailer.php');
require_once('/phpmailer/class.smtp.php');
$mail = new PHPMailer();
$mail->Subject = 'Form Submission on Website';
$name = $_REQUEST['name'] ;
$notes = $_REQUEST['notes'] ;
$phone = $_REQUEST['phone'] ;
$body = "You have received a new message from $name.\n". "Here is the phone number:\n $phone". "Here are the additional notes: \n $notes";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtpout.secureserver.net"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtpout.secureserver.net"; // sets the SMTP server
$mail->Port = 80; // set the SMTP port for the GMAIL server
$mail->Username = "alex#acemobiledetailing.ca"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->SetFrom('example#example.ca', 'Alex Website');
$mail->AddReplyTo("example#example.ca","Alex ");
$mail->MsgHTML($body);
$address = "example#gmail.com";
$mail->AddAddress($address, $name);
$mail-> Send();
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
GOTO(http://example.com/thankyou.php);
}
?>