I have been in PHP/email 'hell' - I got close, and can't seem to get to the 'finish line'....
Om using phpmailer to send support requests in a clients site. my process looks like this:
FORM -> PROCESS (generate feedback message AND cc message to support) -> mail to sender -> mail to support -> redirect to thank you page.
the issue is two fold:
1) the emails go thru as expected IF i have debuging turned on, but I get the debug AND no redirect
2) if I turn off debug - the email DOES NOT go out AND I get a blank page - with NO redirect
* addendum *
The emails just came thru - so it's ONLY a redirect problem... either with or without debug, My meta refresh does not get sent - maybe there's a better way????
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.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;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
Your problem may be that some output has already been sent to the browser before the redirect is attempted. You can't normally do a redirect in that situation. If that's the case you may be able to use output buffering as in the following example:
ob_start();
//statements that output data to the browser
print "some text";
if (!headers_sent()) {
header('Location: /success.php');
exit;
}
ob_end_flush();
This can also be turned on by default in the php.ini file with the output buffering directive, in which case you won't need the ob_start() and ob_end_flush() statements. My php.ini file has this:
output_buffering = 4096
Related
I've got an issue with PHPmailer getting stuck on my mail.php screen (usually it forwards the user to a success.html page). I've turned on error reporting, but still nothing shows up. I'm quite a noob at PHP, but it was working on my test URL before I moved it to the actual URL I wanted it to be on (just a few directories over on my server). Now I'm stuck on a white screen with no errors.
ALSO, I'm having an issue where on hotmail (and maybe some other providers--Gmail is working fine now) it's marking the emails as spam. I fixed a reverse DNS issue, but it's still marking the emails as spam. Any ideas on how to fix it? Please check my body and see if the content raises any red flags.
Here's the code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('/var/www/includes/PHPMailer/PHPMailerAutoload.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
//gather variables from form//
$gmname = $_POST['gmname'];
$charname = $_POST['charname'];
$email = $_POST['email'];
$date = $_POST['date'];
$time = $_POST['time'];
$bantype = $_POST['radiogroup'];
$banreason = $_POST['banreason'];
//end gather//
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$body ="Attention account holder,<br \>This is a notice informing you that your Ashran account has been suspended. Please review the following information.<br \><br \>Account Name: $email<br \>Character Name: $charname<br \>Server: US - Grommash<br \>Ban Reason: $banreason<br \>Ban Type: $bantype<br \>Ban End Date: $date $time Server Time<br \>Banning staff member: $gmname<br \><br \>If you would like to appeal your ban because you feel that you were incorrectly punished, please follow the instructions in the following thread: Click Here<br \><br \>Do NOT reply to this email.";
//$body = eregi_replace("[\]",'',$body);//
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable 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; // set the SMTP port for the GMAIL server
$mail->Username = "----------#gmail.com"; // GMAIL username
$mail->Password = "----------"; // GMAIL password
$mail->SetFrom('----------#gmail.com', '------ -----');
$mail->AddReplyTo("---------#gmail.com","----- -----");
$mail->Subject = "Ashran - Account Suspension Notice";
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "$email";
$mail->AddAddress($address);
//Sets URL for forward after completion
$url = 'success.html';
//
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header( "Location: $url" );
echo "Ban notice sent!!!";
}
?>
For the spam folder issue: If you find you're in a process where you're tweaking your content and then checking if it fixed your delivery problems, you can use inboxtrail.com's seed-based email deliverability test to see which email providers are dumping you to the spam folder after each content change. It's probably a good idea to re-test all the top providers, since for all you know a content change that solves the problem on hotmail could create new problems on gmail or yahoo or something. This should also tell you if the way your email server is configured is setting off any red flags.
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 a send button who's id is 'btnEmail'.My code is sending mail to only the specified email.I want to send it to multiple people without sharing the email id.My code goes here:
<?php
if(isset($_POST['btnEmail']))
{
require_once("../include/phpmailer/class.phpmailer.php");
$mail1 = new PHPMailer(); // create a new object
$mail1->IsSMTP(); // enable SMTP
$mail1->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail1->SMTPAuth = true; // authentication enabled
$mail1->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail1->Host = "smtp.gmail.com";
$mail1->Port = 465; // or 587
$mail1->IsHTML(true);
$mail1->Username = "some_mail#gmail.com"; // SMTP username
$mail1->Password = "Sup3rS3cr3T"; // SMTP password
$mail1->SetFrom("some_mail#gmail.com");
$mail1->Subject = "Testing mail for php";
$mail1->Body = "<html><h4>Tested Successfully</h4></html>";
$mail1->AddAddress('another_mail#gmail.com');
}
You seem to be using phpmailer. Have you checked the examples it gives?
https://github.com/Synchro/PHPMailer/blob/master/examples/mailing_list.phps
This should be present on your computer or server. You can also read the documentation, it is helpful.
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.
I posted earlier as an email and relocation issue, however after trying several things, and tracing (in production! yech!)
I have found the culprit - but have NO IDEA why or where to go from here.
you will see in the below code I include 'email.php' twice - because one receipt goes to the user, the other includes some meta data about the user and goes to support...
if i comment out the SECOND include, my redirect works, if i leave it in, it bombs...
the notify email is valid, and is the only thing that is different.
Im at a loss
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
//I make it this far in my trace - then nothing
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.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;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
It's the require in email.php that is causing the problems. If you want to make the least change possible and make it work, change it to require_once. This will ensure "phpmailer/class.phpmailer.php" is only loaded once, therefore the class will only be defined once.
take the require() line from email.php and put it in the calling file