Reply-To header not picking the php variable - email

I am creating a page that sends the response from contact form within mail but reply-to is not working (the variable i am using is having value, but is not added within headers)
here's my code:
<?php
//$uname=$_REQUEST['uname'];
if(isset($_REQUEST['name']))
{
$name=$_REQUEST['name'];
}
if(isset($_REQUEST['email']))
{
$email=$_REQUEST['email'];
}
if(isset($_REQUEST['phone']))
{
$phone=$_REQUEST['phone'];
}
if(isset($_REQUEST['message']))
{
$message=$_REQUEST['message'];
}
// TESTING IF VARIABLES HAVE VALUES
echo "$name $email $phone $message";
// RESULT: TRUE TILL HERE
if($name=="" || $email=="" || $phone=="" || $message=="")
{
header("location:../?inst=invalid");
}
else
{
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to="mail#example.com";
// Your subject
$subject="$name Contacted you via Contact Form";
// From
$headers = "From: ME <no-reply#example.com>\r\n";
$headers .= 'Reply-To:' . $email . "\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: PHP\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
print $message;
// send email
$sentmail = mail($to,$subject,$message,$headers);
//$sentmail = mail($to1,$subject,$message,$header);
}
// if your email succesfully sent
if($sentmail){
echo "Mail Sent.";
}
else {
header("location:../landing/?status=verification-pending");
}
?>
Now when i checked headers in my gmail, the value for $email doesn't appear in header information, Also no message is received. All I get is a blank message or may be $message is not printing anything like the same case i am facing with reply-to.
please help me a little with this. Thanks in advance.

Check that you have php warnings and notices enabled. When you are echoing out $name, $email, etc you are doing a redirect using headers after that. If your php notices etc aren't turned on, your header redirect will fail due to having already echoed something, and you won't know that you had invalid input. This is part of the reason you shouldn't echo things out during logic, but should store and out put values later.

Actually there was a little mistake with the above code. I mistakenly added = instead of == while comparison.
This line:
if($name=="" || $email="" || $phone="" || $message="")
Should read as:
if($name=="" || $email=="" || $phone=="" || $message=="")
Since = is for equation and not a condition for comparison ==
Fixed it in the Question

Related

Zend_Mail error 500 > postfix/sendmail fatal: -n option not supported

I'm working on a Zend project in a very specific server configuration, our production environment is made of two dedicated servers, one for the company's email which host a postfix server and an other server for our web-application which is running on Apache2/Zend.
Those servers have two different IPs, but works on the same website domain.
Now when i try to send an email with an email from the mail server as a sender, i get an error 500 from Zend_Mail and the email.err log tells me :
postfix/sendmail[15782]: fatal: -n option not supported
But when i put a local adresse or a blank email as a sender it works, so i guess i get kicked out by the postfix of the webserver because it doesn't manage localy those emails.
So my question is: is there any way to use a domain email as sender from a distant server without merging the two servers?
Edit: i forgot to add: i can't use the distant server SMTP, i only can use a local sendmail.
I haven't find any solution or explanations so i ended up by writing a custom action helper based on the mail command of PHP.
I hope it can help someone:
class Zend_Controller_Action_SentEmail extends Zend_Controller_Action_Helper_Abstract{
public function sendEmail($from, array $to, $subject, $message){
//Header set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From: ".$from."<email#domain.com>\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
//To
$stringTo = "";
foreach($to as $k => $v) {
$stringTo .= $k." <".$v.">, ";
}
$stringTo = trim($stringTo, ", ");
//Send the email
if(mail($stringTo, $subject, $message, $headers, "-f bounce#domain.com")){
return true;
}
else{
//Oh! Noes!
return false;
}
}
}

CodeIgniter: Cannot redirect after $this->email->send()

i have the following code which sends an email from a posted form:
$this->load->library('email');
$config['charset'] = 'iso-8859-1';
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('info#mysite.com', 'Scarabee');
$this->email->to('info#mysite.com');
$this->email->subject('Message via website');
$data['msg'] = nl2br($this->input->post('msg'));
$data['msg'] .= '<br><br><b>Verstuurd door:</b><br>';
if($this->input->post('bedrijf')){
$data['msg'] .= $this->input->post('bedrijf').'<br>';
}
$data['msg'] .= $this->input->post('naam').'<br>';
$data['msg'] .= $this->input->post('adres').' - '.$this->input->post('postcode').', '.$this->input->post('gemeente').'<br>';
$data['msg'] .= $this->input->post('tel').'<br>';
$data['msg'] .= $this->input->post('email');
$message = $this->load->view('email', $data, TRUE);
$this->email->message($message);
if($this->email->send()){
$success = 'Message has been sent';
$this->session->set_flashdata('msg', $success);
redirect('contact/'.$this->input->post('lang'));
}
else{
show_error('Email could not be sent.');
}
Problem: the email gets sent with proper formatting (from the email view template), but the page then goes blank. For instance, if I try to echo out $message just below the $this->email->send() call, nothing shows up. Redirecting, as I’m attempting above, obviously doesn’t work either. Am I missing something? Thanks for any help…
Update
Traced the problem down to a function inside /system/libraries/Email.php (CI's default email library). Commenting out the code inside this function allows the mail to get sent, as well as a proper redirect:
protected function _set_error_message($msg, $val = '')
{
$CI =& get_instance();
$CI->lang->load('email');
if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
{
$this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
}
else
{
$this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
}
}
The line that caused the error is: $CI->lang->load('email');
Go figure...
UPDATE 2: SOLVED
I had this in my controller's construct:
function __construct() {
parent::__construct();
$this->lang = $this->uri->segment(2, 'nl');
}
I guess there was a conflict between $this->lang and the _set_error_message function which also returns a "lang" variable (see var_dump further down).
Solution: changed $this->lang to $this->language, and everything's hunky dory!
Thought I'd post the answer here in case anyone else is ever losing hair with a similar problem :-)
It's likely that an error is occurring when you attempt to send an e-mail which is then killing your script during execution. Check your apache and PHP error logs ( /var/log/apache/ ) and enable full error reporting.
You could, for debugging purposes, try doing this:
...
$this->email->message($message);
$this->email->send();
redirect('contact/'.$this->input->post('lang'));
This should redirect you, regardless of your mail being sent or not (if you still recieve it, you can assume that the redirect would be the next thing that happens).
My own solution looks like this:
...
if ( $this->email->send() )
{
log_message('debug', 'mail library sent mail');
redirect('contact/thankyou');
exit;
}
log_message('error', 'mail library failed to send');
show_error('E-mail could not be send');
$ref2 = $this->input->server('HTTP_REFERER', TRUE);
redirect($ref);
By using the above code you can redirect to the current page itself
and you can set your flash message above the redirect function.

CodeIgniter unable to send email using PHP mail()

I'm trying to send an e-mail with Codeigniter like this:
$this->load->library('email');
$this->email->from("myemail#email.com");
$this->email->reply_to("myemail#email.com");
$this->email->to("myemail#email.com");
$this->email->subject("Test mail");
$this->email->message("Email body");
$this->email->set_alt_message("Email body txt");
$this->email->send();
and I got this on the email debugger: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
If I do e simple PHP mail() function with the same addresses, it works but when I use CodeIgniter it gives me the error. So why would it work with simple mail() but not with CodeIgniter ? Any ideas ?
Thanks.
Had similar issue.
That's working code from the controller :
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->from($fromEmail, $fromName);
$this->email->to($email);
$this->email->subject('Тест Email');
$this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));
$this->email->send();
Clearly, there does not seem to be a definitive 'one size fits all' answer. What worked for me was changing
$config['protocol'] = 'smtp';
TO:
$config['protocol'] = 'mail';
Hope this helps...
Do you have an email.php file in your config folder? Maybe there's a problem with your configuration in there.
Nobody seemed to really find a definitive answer, so I did some digging around and found out why.
in system/libraries/Email.php, first look at line 1552:
if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
it seems to send everything all peachy like. I had the exact same symptoms too. To see if I was crazy, i inserted immediately before...
mail($this->_recipients, $this->_subject, $this->_finalbody)
so I basically removed all the headers and let PHP put in the defaults. Bingo! Without CI's headers, it works. With the CI headers, it doesn't. So what is it?
Digging around some more, I looked up to where html is initialized and used. Turns out it doesn't really do anything until around 1046, where it builds the message body.
from line 1048:
if ($this->send_multipart === FALSE)
{
$hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
$hdr .= "Content-Transfer-Encoding: quoted-printable";
}
else
{
$hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
$body .= $this->_get_mime_message() . $this->newline . $this->newline;
$body .= "--" . $this->_alt_boundary . $this->newline;
$body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
$body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
$body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
$body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
$body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
}
Flipping send_multipart between TRUE and FALSE will make the mail class work or not work.
Looked through the Code Ignitor's email class docs reveals nothing. Going to line 52:
var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
So there you have it. Possibly an error in how CI does multipart messages? The hidden config preference
$config['send_multipart'] = FALSE;
in the email.php seems to do the trick.
Be sure domain name in
$this->email->from("myemail#**email.com**");
match to server domain name
Add a protocol variable to the config array and assign it the value "sendmail". The email.php file in the config folder should read as shown below. Mine works like this:
$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
Ensure that Apache can send emails.
To check your current sendmail status:
sestatus -b | grep httpd_can_sendmail
Change it to this if it is off:
sudo setsebool -P httpd_can_sendmail on
I read a comment in the file email.php :
// most documentation of sendmail using the "-f" flag lacks a space after it, however
// we've encountered servers that seem to require it to be in place.
return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
"-f" flag - this problem!!!
Had the same problem,
Make sure your 'from' address is a valid email address.
I had the same problem and though it seems silly to me now, I had some of the config array options capitalized when they all need to be lowercase:
was:
$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'email#example.com';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'TLS'; //ERROR
$mail_config['protocol'] = 'SMTP'; //ERROR
$mail_config['mailtype'] = 'HTML'; //ERROR
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);
Fixed:
$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'email#example.com';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls'; //FIXED
$mail_config['protocol'] = 'smtp'; //FIXED
$mail_config['mailtype'] = 'html'; //FIXED
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);
That worked for me
Its worth saying that if you're on WAMP (Windows) you will need to have sendmail installed otherwise there is no default SMTP method of sending. I wanted to use Gmail but couldn't because there is simply no default mail mechanism.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'pass',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email',$config);
**$this->email->set_newline("\r\n");** <- add this line
this code worked for me.

How come sandbox paypal isn't sending me IPN?

My handler and paypal had communicated for a few days ago then something happened which made the latter stopped sending IPN anymore. I tried simulating the IPN sending via my own script without the post back capability for validation using cURL. My handler is perfectly working. I also place a simple line of code(already tested via cURL) before the lines that post back to paypal so I could check paypal's response. That line of code simply records the IPN into the database. It didn't record either. It should right? Because paypal sends IPN twice. I made sure IPN is turned on in my merchant account and the link points to my handler. Now I am beginning to suspect paypal isn't sending me anything. Here's my handler:
<?php
require_once 'classes/Mysql.php';
require_once 'classes/Email.php';
$mysql = new Mysql();
$myemail = new Email();
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['txn_type'];
$payment_amount = $_POST['mc_amount3'];
$payment_currency = $_POST['mc_currency'];
$subscr_id = $_POST['subscr_id'];
$receiver_email = urldecode($_POST['receiver_email']);
$payer_email = $_POST['payer_email'];
$subscr_type = $_POST['option_selection1'];
$username = $_POST['custom'];
//Save a copy of $req to the database. If paypal sends IPN, this should at least grab the first one before validation.
$memberID = $mysql->get_member_id($username);
$mysql->test_message($req.$payment_amount.$payment_currency, $memberID, $username, $payment_amount, $payment_currency);
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
//This if block does the payment processing. If I place this before the validation/post back occurs, it doesn't work either. But it works when testing using my cURL script--- before the validation.
if($memberID)
{
if($payment_status=='subscr_signup' && $payment_currency=='USD' && !$mysql->check_if_transactionID_exists($subscr_id))
{
$mysql->activate_subscription($memberID, $subscr_id, $subscr_type, $payment_amount);
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
I heard so many stories paypal is so frustrating to set. Could it be that paypal is just isn't sending IPN or there's something wrong with my handler?
I've run into a similar issue-- if you dump the var $res from the line $res = fgets ($fp, 1024); you'll see HTTP/1.1 200 OK, not the VERIFIED/INVALID that you're looking for.
Try checking out this answer for more info: Paypal IPN returning HTTP/1.1 200 OK

PayPal IPN "FAIL"

I'm trying to figure out how to use PayPal's IPN and I've run into a wall.
I want a buyer to be forwarded to a success page after making a purchase, and I want that page to show the details of their transaction. I choose IPN instead of the PDT because I also want to do some other behind the scenes stuff with their data.
Anyway, here's the code I'm using -- I'm testing in sandbox mode -- but it returns "FAIL" every time.
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// PAYMENT VALIDATED & VERIFIED!
echo "Validated!";
}
else if (strcmp ($res, "INVALID") == 0) {
// PAYMENT INVALID & INVESTIGATE MANUALY!
echo "Invalid!";
}
}
fclose ($fp);
}
i got the same problem in the test enviroment because my item_name has especial character,
then i change item_name to only english word and number. it works fine.
but in real enviroment i still find this problem,i read the https://www.x.com/docs/DOC-1551, but i still don't know why
I didn't realize that the sandbox account I set up was Unverified. I re-made the pre-configured sandbox account and then it started to work perfectly.