I am trying to send mail and it just brought me to blank page. The echo had not been hit and i couldn't see the error.
try {
$mail->SetFrom($email, $name);
$mail->SMTPDebug = 2;
$address = "xx#gmail.com";
$mail->AddAddress($address, "xx");
$mail->Subject = "Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
$mail->SMTPSecure = "tls";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'xx#gmail.com';
$mail->Password = 'password1234';
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else
echo 'sent';
//header("Location: contact.php?status=thanks");
} catch (phpmailerException $e) {
echo 'error1';
} catch (Exception $e) {
echo 'error2';
}
As mentioned, you should turn error reporting on with the following two lines on the top of your script:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Also, have you included the SMTP class of PHPMailer, since you're sending mail through SMTP? The file "class.smtp.php" should be placed in the same directory as "class.phpmailer.php" - if that is missing, then PHPMailer will error out when it's trying to include it
Related
I am trying to send emails using phpmailer and getting the error as mentioned in the title. I am using XAMPP for apache server.
I have searched almost every link on google and everywhere but nothing is working for me.
<?php
ini_set('display_errors', 1);
require 'autoload.php';
require 'class.phpmailer.php';
//require 'class.smtp.php';
$mail = new PHPMailer;
$mail->IsSMTP();
//$mail->SetLanguage("en", 'language');
$mail->Mailer = "smtp";
//$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->SMTPDebug = 2;
$mail->SMTPAuth=False;
$mail->Host="smtp.gmail.com";
$mail->Username = "email#gmail.com";
$mail->Password = "password";
//$mail->setFrom("email#gmail.com");
$mail->From = "email#gmail.com";
$mail->addAddress('email#gmail.com','Rishabh Goel');
$mail->isHTML(false);
$mail->Subject = 'First Subject';
$mail->Body = 'Mail body in HTML';
$mail->AltBody = 'This is the plain text version of the email content';
/*try{
$mail->Send();
echo "Thanks. Bug report successfully sent.
We will get in touch if we have any more questions!";
} catch(Exception $e){
//Something went bad
echo "Mailer Error: - " . $mail->ErrorInfo;
}*/
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
?>
How can I redirect when the email is being sent with PHPMailer?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
// $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//
$mail->SetFrom('test#gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject
//message
$body = emailZReports($total_sales, $inventory);
// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress("test#gmail.com", "Z-Reports");
//Success
if ($mail->Send()) {
echo "Message Sent!";
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
How can I redirect to a specific page instead of displaying Message Sent!?
if you are using SMTP, you can't redirect to another page if $mail->SMTPDebug is not set to 0. This is because it sends out headers which you can't modify with header("Location: path/to/redirect"). Set $mail->SMTPDebug = 0; so that you can redirect to another page after $mail->send(). but you have to do that only when you have finished debugging and your mail is sending successfully.
You can use php header function
//Success
if ($mail->Send()) {
header("Location:Yourfile.php");//echo "Message Sent!";
}
Make sure there is not echo or any output before this function
You can use like this,
See Header for more info.
It can be like this,
//Success
if ($mail->Send()) {
header('Location: nextpage.php');
}
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.nextpage.com/"
This works
if(!$mail->Send()) {
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} //echo getcwd();
header("Location:index.php") //redirects to a page named index.php;
// make another script for redirecting
<?php
require_once("mail_function.php");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>
//mail_function.php
<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer = "smtp";
$mail->SetFrom("xxxx#gmail.com", "web");
$mail->Port = '587'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication.
$mail->Username = 'xxxx#gmail.com'; //Sets SMTP username
$mail->Password = 'xxxx'; //Sets SMTP password
$mail->SMTPSecure = 'tls'; //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email); //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
require_once 'PHPMailer-master/class.phpmaileroauthgoogle.php';
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
//$mail->SMTPDebug =2;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'zhaider113#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$from = 'shahghafoor439#gmail.com';
$mail->setFrom($from, 'Ghafoor Shah');
$mail->addReplyTo($from, 'Ghafoor Shah');
$mail->addAddress('zhaider113#gmail.com', 'zeeshan');
$mail->Subject = 'This is subject';
$mail->Body = 'This is the body of email';
$mail->AltBody = 'This is the body of email';
$mail->send();
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
?>
I try to send email but it not send and give error message which is:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting */
try with $mail->SMTPSecure = 'tls';
See full example at PHPMailer's gmail example below: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Make sure you include correct PHPMailer Library.
And make this changes
Some Servers not response for SSL(Secure). So that change this $mail->SMTPSecure = 'tls';
And In your code you have two Mail sending option $mail->send();
//$mail->send();//Comment this
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
Now either mail sent Or either It will print Error log
I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
I'm trying to run PHPMailer for an internal contact form and I am getting the error ERROR: AUTH not accepted from server. Here is my current code..
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = True;
$mail->Username = 'xxxxx';
$mail->Password = '***';
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
This error basically means that your attempt to authenticate was rejected by the remote server. Different PHPMailer settings (well SMTP settings) are required by different remote mail servers.
This could be caused by
Using the wrong port
Using the wrong host
Incorrect user/pass
Incorrect SMTPSecure
Example SMTP setup:
Gmail: use of phpmailer class
Hotmail: phpmailer with hotmail?
If you are using this internally, you may not need to use SMTP authentication at all, depending on your server settings. Try this and see if it works:
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = False;
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};