$.post to phpMailer failed - php

I'm using $.post in my js to check my form and if all is okay, the data will go to my sendinquiry.php which will send the contact form email. All is working just that I can't catch the echo "submitted" to sdata so that I can display the thank you message in jQuery. Anyone?
jQuery
.
.
.
if(errflag > 0){
$('.disclaimer').addClass('errortext');
}
else {
$.post(
"sendinquiry.php",
$('#contact_form').serialize(),
function(sdata)
{
if(sdata == 'submitted') {
var ntext = '<div class="row topbot_m shortwidth"><div class="columns medium-12 text-center"><h3>THANK YOU FOR SUBMITTING YOUR ENQUIRY.</h3><p>Our Sales Representative will contact you shortly.</p></div></div>';
$('#contact_form').hide().html(ntext).fadeIn('slow');
}
}
);
}
PHP
<?php
$subject = "Line Enquiry Form";
$gname = "High Line";
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$contactno = $_POST['contactno'];
$emailadd = $_POST['emailadd'];
$fmessage = $_POST['message'];
require 'PHPMailermaster/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($emailadd, $fname);
$mail->addAddress('sender#gmail.com', 'Reza San'); // Add a recipient
$mail->addReplyTo($emailadd, $fname +' '+$lname);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body ="<html>
<head>
<title>".$gname."</title>
</head>
<body>
<h2>".$subject."</h2>
<table>
<tr><td width='200'>First Name: </td><td>".$fname."</td></tr>
<tr><td width='200'>Last Name: </td><td>".$lname."</td></tr>
<tr><td width='200'>Contact No: </td><td>".$contactno."</td></tr>
<tr><td width='200'>Email Address: </td><td>".$emailadd."</td></tr>
<tr><td width='200'>Message: </td><td>".$fmessage."</td></tr>
</table>
</body>
</html>";
if($mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'submitted';
}
?>

Related

Mail configuration error in amazon hosting using 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;
}

How to use phpMailer isSMTP on Bluehost?

It's taken me days to get the right settings so I thought I would post a php script that works on Bluehost. In initial tests using isSMTP is faster than isMAIL.
<?php
require_once '../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "box1311.bluehost.com"; // specify bluehost as outgoing server
$mail->SMTPSecure = "tls"; // sets the prefix to the server do not use ssl
$mail->SMTPDebug = 3; // comment out if you don't need debug info
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "USER#EXAMPLE.COM"; // SMTP username (your email account)
$mail->Password = "PASSWORD"; // SMTP password
$mail->Port = 25;
$mail->From = 'USER#EXAMPLE.COM';
$mail->FromName = "USER#EXAMPLE.COM";
$mail->AddAddress('CLIENT#gmail.com');
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'test message';
$body = '<!DOCTYPE html>
<html><header>
</header>
<body lang=EN-US>
<div style="text-align:center">
<h2>this is a test</h2>
</div>
</body>
</html>';
$mail->Body = $body;
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo '<h1>message sent</h1>';
}
?>
This code work for me.
include "phpmailer/class.phpmailer.php";
include "phpmailer/class.smtp.php";
$email_user = "email#host.com";
$email_password = "pass123";
$the_subject = "Title";
$from_name = "Sender";
$phpmailer = new PHPMailer();
// ---------- datos de la cuenta de correo -----------------------------
$phpmailer->Username = $email_user;
$phpmailer->Password = $email_password;
//---------------------------------------------------------------------
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Host = "box6171.bluehost.com";
$phpmailer->Port = 26;
//$phpmailer->SMTPDebug = 2;
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;
$phpmailer->setFrom($phpmailer->Username,$from_name);
$phpmailer->AddAddress("to#host.com");
$phpmailer->Subject = $the_subject;
$phpmailer->Body .="<h1 style='color:#3498db;'>Attachment:</h1>";
$phpmailer->Body .= "<h3>".$attach1."</h3>";
$phpmailer->AddAttachment($attach, "attach1");
$phpmailer->AddBCC("hidecopy#host.com", "bcc1");
$phpmailer->IsHTML(true);
$enviado = $phpmailer->Send();
if($enviado) {
echo 'email send successful';
}
2022 update
$phpmailer->Host = [your fully qualified domain]
$phpmailer->Port = 465
$phpmailer->SMTPSecure = 'ssl'
Important that $phpmailer->From and $phpmailer->Username must be the same.

PHPmailer how to show message

How I can show success message after to click send in my form? my phpmailer is working but I want to also after to click send to redirect to my homepage (index.html) and show my message div. This is my php code.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xxxxxxx.pl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx#xxxxxxxx.pl'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($email, $name);
$mail->addAddress('xxxx#gmail.com', 'xxxxxxxx'); // Add a recipient // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Body test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header('Location: index.html');
}
And this is my Jquery code to show/hidden div succes:
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
You could send extra parameter with your request :
header('Location: index.php?r=1');
Then in your index page get the parameter and use it to show the box :
<script>
var result="<?php echo $_GET['r']; ?>";
if(parseInt(result)){
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
}
</script>
Hope this helps.

PHPMailer doesn't show HTML

I am building a bulk email tool to address a list of our clients regarding some upcoming migrations. Our current mass mail tool doesn't quite cut it in this particular case, where I have opted to just build one instead.
I am using TinyMCE to provide an editor for the email message body and passing this along to PHPMailer to send out. Everything is working great except the html is not displayed properly when viewed in a client such as Outlook. I have made absolutely sure $mail->isHTML(true) is set so I am at a loss now.
I echo out the value of $message in the bulk_mail_sender() function and its correct. If I paste this string as $mail->Body it works. If I have $message set as $mail->Body however, it turns into all sorts of strange characters.
Message Source:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><p>Hi there,</p>
<p>Â </p>
<p>What is up foo</p>
Code:
function bulk_mail_sender($vars, $emails, $subject, $message)
{
foreach ($emails as $email)
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Host = $vars['opt_host']; // Specify main SMTP Server
$mail->Port = $vars['opt_port']; // TCP port
$mail->Username = $vars['opt_user']; // SMTP Username
$mail->Password = $vars['opt_pass']; // SMTP Password
$mail->SMTPSecure = $vars['opt_type']; // Enable TLS / SSL encryption
$mail->setFrom($vars['opt_sender_email'], $vars['opt_sender_name']);
$mail->addAddress($email);
$mail->addReplyTo($vars['opt_sender_email'], $vars['opt_sender_name']);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send())
{
echo 'Message failed to send to ' . $email;
echo 'Mailer Error: ' . $mail->ErrorInfo . '</br></br>';
}
else
{
echo 'Message has been sent to ' . $email . '</br>';
}
}
}
function bulk_mail_output($vars)
{
if (!empty($_POST))
{
$subject = $_POST['subject'];
$message = $_POST['message'];
$emails = $_POST['emails'];
$emails = explode(PHP_EOL, $emails);
bulk_mail_sender($vars, $emails, $subject, $message);
}
else
{
echo '<form method="POST" action="">';
echo 'Subject: <input type="text" name="subject" id="subject"></br></br>';
echo '<textarea rows="10" cols="100" name="message" id="message"></textarea></br></br>';
echo '<h3>Email Addresses</h3>';
echo '<textarea rows="10" cols="100" name="emails" id="emails"></textarea></br></br>';
echo '<input type="submit" value="Submit">';
echo '</form>';
echo '<script language="javascript" type="text/javascript" src="../includes/jscript/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
mode: "exact",
elements: "message",
theme: "advanced",
entity_encoding: "raw",
convert_urls: false,
relative_urls: false,
plugins: "style,table,advlink,inlinepopups,media,searchreplace,contextmenu,paste,directionality,visualchars,xhtmlxtras",
theme_advanced_buttons1: "cut,copy,paste,pastetext,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,|,search,replace",
theme_advanced_buttons2: "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,|,forecolor,backcolor,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,|,ltr,rtl,cleanup,code,help",
theme_advanced_buttons3: "", // tablecontrols
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "bottom",
theme_advanced_resizing: true
});
function toggleEditor(id)
{
if (!tinyMCE.get(id))
tinyMCE.execCommand(\'mceAddControl\', false, id);
else
tinyMCE.execCommand(\'mceRemoveControl\', false, id);
}
</script>';
}
}
While I couldn't ever find a way to fix this within TinyMCE, the workaround used was to just wrap my $message variable in the html_entity_decode function when setting it to the mail body. I would have preferred to pass the data from TinyMCE properly the first time, however the entity encoding cannot be fully disabled for some reason.
$mail = new PHPMailer;
$mail->CharSet = "UTF-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Host = $vars['opt_host']; // Specify main SMTP Server
$mail->Port = $vars['opt_port']; // TCP port
$mail->Username = $vars['opt_user']; // SMTP Username
$mail->Password = $vars['opt_pass']; // SMTP Password
$mail->SMTPSecure = $vars['opt_type']; // Enable TLS / SSL encryption
$mail->setFrom($vars['opt_sender_email'], $vars['opt_sender_name']);
$mail->addReplyTo($vars['opt_sender_email'], $vars['opt_sender_name']);
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = html_entity_decode($message);
if(!$mail->send())
{
echo 'Message failed to send to ' . $email;
echo 'Mailer Error: ' . $mail->ErrorInfo . '</br></br>';
}
else
{
echo 'Message has been sent to ' . $email . '</br>';
}

My form will not submit on the website. It just shows blank page at mail.php

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);
}
?>

Categories