Send email according to input select value - php

I need to make changes to an existing form by adding a dropdown menu where I will have two inputs and their values. The purpose is to send the form to recipient_one if Address 1 is selected or to recipient_two when Address 2 is selected. Address 1 needs to be the default value when nothing is selected.
Here is just the added HTML:
<form method="post" action="./index.php" enctype="multipart/form-data">
<fieldset class="elist">
<legend>Select shop:</legend>
<select name="shop">
<option name="address-chosen" value="Tammsaare" >Tammsaare</option>
<option name="address-chosen" value="Ülemiste" >Ülemiste</option>
</select>
</fieldset>
</form>
and the PHP:
$action = isset($_POST['action']) ? $_POST['action'] : null;
$page = null;
$pages = array('info', 'en');
if( isset($_GET['page']) && in_array($_GET['page'], $pages) ) {
$page = $_GET['page'];
}
if( !in_array($page, $pages) ) {
$page = '';
}
$mail_sent = false;
if( $action == 'add' ) {
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
$message = '';
$message .= 'Name: '.safe($_POST['name'])."\r\n";
$message .= 'E-mail: '.safe($_POST['email'])."\r\n";
$message .= 'Phone: '.safe($_POST['telephone'])."\r\n";
$message .= 'Mark: '.safe($_POST['mark'])."\r\n";
$message .= 'Model: '.safe($_POST['model'])."\r\n";
$message .= 'Shop: '.safe($_POST['address-chosen'])."\r\n";
$message .= "Wants newsletter: ".$soovib_uudiskirja = isset($_POST['newsletter']) ? "Yes" : "No";
$message .= "\r\n";
$message .= "\r\n";
$message .= "\r\n";
$message .= 'Aeg: '.date('d.m.Y H:i')."\r\n";
$message .= 'IP: '.$ip."\r\n";
$mail_data = array(
'to_email' => 'email#mail.com',
'from_email' => 'email#mail.com',
'from_name' => 'Stock Cars',
'subject' => 'Reservation',
'message' => $message,
);
mail_send($mail_data);
$mail_sent = true;
}
function safe( $name ) {
return( str_ireplace(array( "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:" ), "", $name ) );
}
function mail_send($arr)
{
if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
throw new HelperException('mail(); not all parameters provided.');
}
$to = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
$from = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: text/plain; charset="UTF-8";',
'Content-Transfer-Encoding: 7bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '#' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));}
if (isset($_GET['page'])) {}
So the question is how do I reconstruct the array?

Set the email variable based on the form value:
//default is email 1
$email='email#mail.com';
if(isset($_POST['shop']) && $_POST['shop']=='Ülemiste') {$email='email2#mail2.com';}
$mail_data = array(
'to_email' => $email,
'from_email' => $email,
'from_name' => 'Stock cars',
'subject' => 'Reservation',
'message' => $message,
);
edited as per your edit - not sure how the umlaut will effect things though

Related

Using expressmail with PHP form

We have a php script that emails form field values when a user submits the form. The form action points to the script below.
We've been asked to configure things to use expressmail explicitly. My question is, would this entail a modification to the script or is this a config setting on the server somewhere?
<?php
if (! $_POST) {
header('HTTP/1.0 405 Method Not Allowed');
exit;
}
$redirectTo = html_entity_decode($_POST['post']);
$body = '<html><body>';
$content = array();
foreach ($_POST as $key => $value) {
if ('-label' !== substr($key, -6)) {
continue;
}
$field = substr($key, 0, strlen($key) - 6);
$content[$field]['value'] = $_POST[$field];
$content[$field]['label'] = $_POST[$key];
}
$body .= '<h1>' . htmlentities($_POST['formName']) . '</h1>';
foreach ($content as $field => $value) {
$data = $value['value'];
$label = $value['label'];
$body .= '<p><b>' . htmlentities($label) . '</b><br />';
if (false === is_array($data) && (null === $data OR "" === trim($data))) {
$body .= 'N/A';
} elseif (is_array($data)) {
$body .= '<ul>';
foreach ($data as $val) {
$val = htmlentities($val);
$body .= '<li>' . $val . '</li>';
}
$body .= '</ul>';
} else {
$body .= htmlentities($data);
}
$body .= '</p>';
}
$body .= '</body></html>';
$to = strip_tags($_POST['emailTo']);
$subject = strip_tags($_POST['emailSubject']);
$headers = "From: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $body, $headers);
header('Location: ' . $redirectTo);
?>

Opencart order confirmation email subject is empty

My opencart does not input into the order confirmation email subject. The email comes with no subject. Any idea why that happens?
I cannot figure out, what could be the problem. Any ideas are appreciated.
Thank you in advance
<?php
class Mail {
protected $to;
protected $from;
protected $sender;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = 'mail';
public $hostname;
public $username;
public $password;
public $port = 25;
public $timeout = 5;
public $newline = "\n";
public $crlf = "\r\n";
public $verp = false;
public $parameter = '';
public function setTo($to) {
$this->to = $to;
}
public function setFrom($from) {
$this->from = $from;
}
public function setSender($sender) {
$this->sender = $sender;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setText($text) {
$this->text = $text;
}
public function setHtml($html) {
$this->html = $html;
}
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}
if (!$this->from) {
trigger_error('Error: E-Mail from required!');
exit();
}
if (!$this->sender) {
trigger_error('Error: E-Mail sender required!');
exit();
}
if (!$this->subject) {
trigger_error('Error: E-Mail subject required!');
exit();
}
if ((!$this->text) && (!$this->html)) {
trigger_error('Error: E-Mail message required!');
exit();
}
if (is_array($this->to)) {
$to = implode(',', $this->to);
} else {
$to = $this->to;
}
$boundary = '----=_NextPart_' . md5(time());
$header = '';
$header .= 'MIME-Version: 1.0' . $this->newline;
if ($this->protocol != 'mail') {
$header .= 'To: ' . $to . $this->newline;
$header .= 'Subject: ' . '=?UTF-8?B?' . base64_encode($this->subject) . '?=' . $this->newline;
}
$header .= 'Date: ' . date('D, d M Y H:i:s O') . $this->newline;
$header = 'From: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Reply-To: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Return-Path: ' . $this->from . $this->newline;
$header .= 'X-Mailer: PHP/' . phpversion() . $this->newline;
$header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline . $this->newline;
if (!$this->html) {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->text . $this->newline;
} else {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline;
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
if ($this->text) {
$message .= $this->text . $this->newline;
} else {
$message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline;
}
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->html . $this->newline;
$message .= '--' . $boundary . '_alt--' . $this->newline;
}
foreach ($this->attachments as $attachment) {
if (file_exists($attachment)) {
$handle = fopen($attachment, 'r');
$content = fread($handle, filesize($attachment));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline;
$message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;
if ($this->protocol == 'mail') {
ini_set('sendmail_from', $this->from);
if ($this->parameter) {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter);
} else {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
}
} elseif ($this->protocol == 'smtp') {
$handle = fsockopen($this->hostname, $this->port, $errno, $errstr, $this->timeout);
if (!$handle) {
trigger_error('Error: ' . $errstr . ' (' . $errno . ')');
exit();
} else {
if (substr(PHP_OS, 0, 3) != 'WIN') {
socket_set_timeout($handle, $this->timeout, 0);
}
while ($line = fgets($handle, 515)) {
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($this->hostname, 0, 3) == 'tls') {
fputs($handle, 'STARTTLS' . $this->crlf);
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 220) {
trigger_error('Error: STARTTLS not accepted from server!');
exit();
}
}
if (!empty($this->username) && !empty($this->password)) {
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
fputs($handle, 'AUTH LOGIN' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: AUTH LOGIN not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->username) . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: Username not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->password) . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 235) {
trigger_error('Error: Password not accepted from server!');
exit();
}
} else {
fputs($handle, 'HELO ' . getenv('SERVER_NAME') . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: HELO not accepted from server!');
exit();
}
}
if ($this->verp) {
fputs($handle, 'MAIL FROM: <' . $this->from . '>XVERP' . $this->crlf);
} else {
fputs($handle, 'MAIL FROM: <' . $this->from . '>' . $this->crlf);
}
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: MAIL FROM not accepted from server!');
exit();
}
if (!is_array($this->to)) {
fputs($handle, 'RCPT TO: <' . $this->to . '>' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
} else {
foreach ($this->to as $recipient) {
fputs($handle, 'RCPT TO: <' . $recipient . '>' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
}
}
fputs($handle, 'DATA' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 354) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
// According to rfc 821 we should not send more than 1000 including the CRLF
$message = str_replace("\r\n", "\n", $header . $message);
$message = str_replace("\r", "\n", $message);
$lines = explode("\n", $message);
foreach ($lines as $line) {
$results = str_split($line, 998);
foreach ($results as $result) {
if (substr(PHP_OS, 0, 3) != 'WIN') {
fputs($handle, $result . $this->crlf);
} else {
fputs($handle, str_replace("\n", "\r\n", $result) . $this->crlf);
}
}
}
fputs($handle, '.' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
fputs($handle, 'QUIT' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 221) {
trigger_error('Error: QUIT not accepted from server!');
exit();
}
fclose($handle);
}
}
}
}
?>
when you right the code for mail in the model section will you use this??
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->hostname = $this->config->get('config_smtp_host');
$mail->username = $this->config->get('config_smtp_username');
$mail->password = $this->config->get('config_smtp_password');
$mail->port = $this->config->get('config_smtp_port');
$mail->timeout = $this->config->get('config_smtp_timeout');
$mail->setTo($order_info['email']);
$mail->setFrom($this->config->get('config_email'));
$mail->setSender($order_info['store_name']);
$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
$mail->send();
The mail is generated from model/checkout/order in function addOrderHistory(). You might want to see from there. This way you could solve the issue.

PHP mail function returning utf8 errors in hotmail/outlook

Lately I've been having some big problems with sending e-mails through my website, as it seems to always show major encoding differences on the e-mail clients. As it almost always works on Gmail and others, on Hotmail/Outlook there's always an UTF8 error in the title/subject of the message. I tried encoding/decoding several variables to keep that from happening, but every solution ends up leaving an error.
Here's the function to send the e-mails via form:
function enviarEmail($nomeRemetente = '', $emailRemetente = 'email#dominio.com', $emailDestinatario = 'email#dominio.com', $emailResposta = 'email#dominio.com', $assunto = '', $campos = array(), $dados = array(), $customMsg = false, $mensagemHTML = '')
{
$retorno = true;
$quebra_linha = "\n";
if (PHP_OS == 'Linux') {
$quebra_linha = "\n";
} elseif (PHP_OS == 'WINNT') {
$quebra_linha = "\r\n";
}
if (!$customMsg) {
$mensagemHTML = '<table width="490" border="0" cellpadding="0" cellspacing="6">
<tr>
<td colspan="2">
<p style="font-size: 14px; font-family: Arial, Helvetica, sans-serif; color: #A93118;">..: ' . $assunto . '</p>
<p>Formulário preenchido em ' . date('d/m/Y') . ' as ' . date('H:i') . '</p>
</td>
</tr>
';
$qtde = count($campos);
for ($i = 0; $i < $qtde; $i++) {
$mensagemHTML .= '
<tr>
<td align="right"><strong>' . $campos[$i] . ': </strong></td>
<td>' . $dados[$i] . '</td>
</tr>
';
}
$mensagemHTML .= '</table>';
}
$headers = implode($quebra_linha, #
array('MIME-Version: 1.1', #
'Content-type: text/html; charset=utf-8', #
'From: ' . html_entity_decode($nomeRemetente) . ' <' . $emailRemetente . '>', #
'Return-Path: ' . utf8_decode($nomeRemetente) . ' <' . $emailRemetente . '>', #
'Reply-To: ' . $emailResposta, #
'Subject: ' . $assunto, #
'X-Priority: 3'
));
$emailDestinatario = is_array($emailDestinatario) ? $emailDestinatario : array($emailDestinatario);
foreach ($emailDestinatario as $emailDestino) {
mail($emailDestino, $assunto, $mensagemHTML, $headers) or $retorno = false;// die('Erro no servidor!');
}
return $retorno;
}
And this function is called here:
function enviarContato()
{
$nomeRemetente = PROJECT_SHORT_TITLE;
$emailRemetente = $emailResposta = PROJECT_EMAIL;
$subject = 'Contato no site Modelo Site Rápido - ' . date('d/m/Y H:i:s');
$emailDestinatario = array('programacao#monge.com.br'/*, PROJECT_EMAIL*/);
$campos = array();
$dados = array();
$campos[] = 'Nome';
$dados[] = isset($_REQUEST['contatoNome']) ? $_REQUEST['contatoNome'] : '';
$campos[] = 'Email';
$emailResposta = $dados[] = isset($_REQUEST['contatoEmail']) ? $_REQUEST['contatoEmail'] : '';
$campos[] = 'Telefone';
$dados[] = isset($_REQUEST['contatoTelCel']) ? htmlspecialchars($_REQUEST['contatoTelCel'], ENT_COMPAT, 'UTF-8') : '';
$campos[] = 'Mensagem';
$dados[] = isset($_REQUEST['contatoMensagem']) ? nl2br(stripcslashes($_REQUEST['contatoMensagem'])) : '';
$conf = enviarEmail($nomeRemetente, $emailRemetente, $emailDestinatario, $emailResposta, utf8_decode($subject), $campos, $dados);
$link = 'http://' . PROJECT_URL . '/contato.php'; // usado sem mod_rewrite
if (isset($MG_MR_Settings['active']) && $MG_MR_Settings['active']) {
$link = 'http://' . PROJECT_URL . '/contato'; // usado com mod_rewrite
}
if ($conf) {
echo "<script type='text/javascript'>";
echo "alert('Contato enviado com sucesso!');";
echo "document.location.replace('$link');";
echo "</script>";
die();
} else {
echo "<script type='text/javascript'>";
echo "alert('Erro ao enviar contato, contate o administrador.');";
echo "document.location.replace('$link');";
echo "</script>";
die();
}
}
$msgContato = '';
if (!empty($_POST['SubmitContato'])) {
$msgContato = enviarContato();
}
The problem that this is returning on Hotmail/Outlook is like this:
Contato no site Centro Estético Bela - 17/12/2013 11:48:53
177.97.93.251
It works well on Gmail. If anyone can point to the right direction I would greatly appreciate it. Please ask for any info that might help you solve this, hope it seems clear enough.
Thank you in advance.
Good day:
i´m use this subject
$subject = "=?UTF-8?B?" . base64_encode('Confirmación de Compra (' . $order_id .")" ) . "?=";
in your case:
$subject = "=?UTF-8?B?" . base64_encode('Contato no site Modelo Site Rápido - ' . date('d/m/Y H:i:s') ) . "?=";

My PHP form will not send to multiple users

Hi I have a php form that works perfectly when it sends an email to one person but when I add another email address it doesn't send an email to either address. I have been looking on php sites but can't see why my form is now refusing to email once the second email address is added.
<?php
function isRequestSet( $name ) {
if ( isset ( $_REQUEST[$name] ) ) {
return ( $_REQUEST[$name] != "" ) ;
}
return false;
}
$name = "";
if ( isRequestSet('name' ) ) {
$name = $_REQUEST['name'];
}
$number = "";
if ( isRequestSet('number') ) {
$number = $_REQUEST['number'];
}
$email = "";
if ( isRequestSet( 'email' ) ) {
$email = $_REQUEST['email'];
}
$postcode = "";
if ( isRequestSet('postcode' ) ) {
$location = $_REQEUST['postcode'];
}
$how_did_you_hear_about_us = array();
if ( isset( $_REQUEST['how_did_you_hear_about_us'] ) ) {
$how_did_you_hear_about_us = $_REQUEST['how_did_you_hear_about_us'];
}
$message = "";
if ( isRequestSet('message' ) ) {
$location = $_REQEUST['message'];
}
$apartment_price_range = array();
if ( isset( $_REQUEST['apartment_price_range'] ) ) {
$apartment_price_range = $_REQUEST['apartment_price_range'];
}
$url = "";{
$url = $_REQUEST['url'];
}
$property = "";{
$property = $_REQUEST['property'];
}
if ( ($name !="") && ($number != "") && ($email != "") && ($isspam !="yes") ) {
$to = 'name#email.com,name#email2.com';
$from = $to;
$headers = 'From: ' . $to . "\n" .
'Reply-To: ' . $to . "\n";
$vars = array( 'name' , 'number' , 'email' , 'postcode' , 'message' ) ;
$message = "-----------\n" ;
foreach ( $vars as $v ) {
$value = $_REQUEST[$v];
$message .= "$v:\t$value\n";
}
$message .= "-----------\n" ;
$message .= "\nHow did you hear about apartments?:\n" ;
foreach ( $how_did_you_hear_about_us as $how_did_you_hear_about_us ) {
$message .= "$how_did_you_hear_about_us\n" ;
}
$message .= "-----------\n" ;
$message .= "\nApartment price range:\n" ;
foreach ( $apartment_price_range as $apartment_price_range ) {
$message .= "$apartment_price_range\n" ;
}
$subject = "From: $name <$email>";
mail( $to , $subject , $message , $headers, "-f $from" );
$confirm = true;
//redirect to the 'thank you' page
header("Location:http://website.com/file/thankyou.php");
} else {
$confirm = false;
}
?>
Most likely it is because you use multiple addresses for From and Reply-to fields:
$to = 'name#email.com,name#email2.com';
$from = $to;
Change it to use either first email or something like your-service-name#you-domain-name.com
Use only one address for From and Reply-To.
$from = 'me#my-domain.com';
$headers = 'From: ' . $from . "\n" .
'Reply-To: ' . $from . "\n";
"In the line below you should break the $to field into an array.
mail( $to , $subject , $message , $headers, "-f $from" );
For example
$address_array = split(",", $to);
foreach($address_array as $address)
{
mail( $address, $subject , $message , $headers, "-f $from" );
}
This allows for you $to string to contain as many emails as desired.
N.b if you wish to skip the split line just store the $to as an array
$to = array("name#email.com","name#email2.com");
make your email part into a function.
will much more easier for you to pass the email address.
function email_to_user($email_address){
$to = $email_address;
<rest of the codes>
}
and you can easily pass the email address
email_to_user(name1#mail.com);
email_to_user(name2#mail.com);

PHP Form Checkboxes

How do I get the values of php checkboxes in a form to show when emailed to the recipient?
I am learning how to use php but I can't figure this one out with the form i have generated.
Below is the checkbox code from the form itself:
<input type="checkbox" value="Please send me a Travel Planner" name="options[]">
<input type="checkbox" value="Please send me a Visitor Map" name="options[]" />
<input type="checkbox" value="Please sign me up for the email newsletter" name="options[]" />
Now here's the form code from the feedback page that processes it:
#<?php
// ------------- CONFIGURABLE SECTION ------------------------
// $mailto - set to the email address you want the form
// sent to, eg
//$mailto = "youremailaddress#example.com" ;
$mailto = 'xxxxx#xxxxxxxxx.com' ;
// $subject - set to the Subject line of the email, eg
//$subject = "Feedback Form" ;
$subject = "Request For Visitor Guide" ;
// the pages to be displayed, eg
//$formurl = "http://www.example.com/feedback.html" ;
//$errorurl = "http://www.example.com/error.html" ;
//$thankyouurl = "http://www.example.com/thankyou.html" ;
$formurl = "http://www.example.com/requestform_mtg.php" ;
$errorurl = "http://www.example.com/error.php" ;
$thankyouurl = "http://www.example.com/thankyou.php" ;
$email_is_required = 1;
$name_is_required = 1;
$address_is_required = 1;
$contactname_is_required = 1;
$city_is_required = 1;
$zip_is_required = 1;
$phone_is_required = 1;
$uself = 0;
$use_envsender = 0;
$use_webmaster_email_for_from = 1;
$use_utf8 = 1;
// -------------------- END OF CONFIGURABLE SECTION ---------------
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
$envsender = "-f$mailto" ;
$name = $_POST['name'] ;
$contactname = $_POST['contactname'] ;
$title = $_POST['title'] ;
$email = $_POST['email'] ;
$address = $_POST['address'] ;
$city = $_POST['city'] ;
$state = $_POST['state'] ;
$zip = $_POST['zip'] ;
$fax = $_POST['fax'] ;
$phone = $_POST['phone'] ;
$mtgname = $_POST['mtgname'] ;
$dates = $_POST['dates'] ;
$attendance = $_POST['attendance'] ;
$guestroom = $_POST['guestroom'] ;
$mtgroom = $_POST['mtgroom'] ;
$timeframe = $_POST['timeframe'] ;
$options = $_POST['options'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );
if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (($email_is_required && (empty($email) || !ereg("#", $email))) || ($name_is_required && empty($name)) || ($address_is_required && empty($address)) || ($contactname_is_required && empty($contactname)) || ($city_is_required && empty($city)) || ($zip_is_required && empty($zip)) || ($phone_is_required && empty($phone))) {
header( "Location: $errorurl" );
exit ;
}
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) || ereg( "[\r\n]", $address ) || ereg( "[\r\n]", $contactname ) ) {
header( "Location: $errorurl" );
exit ;
}
if (empty($email)) {
$email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}
$messageproper =
"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Organization Name: $name\n" .
"Contact Name: $contactname\n" .
"Email of sender: $email\n" .
"Address of sender: $address\n" .
"City of sender: $city\n" .
"State of sender: $state\n" .
"Zip Code of sender: $zip\n" .
"Fax of sender: $fax\n" .
"Phone of sender: $phone\n" .
"Meeting Name: $mtgname\n" .
"Preferred Dates: $dates\n" .
"Expected Attendance: $attendance\n" .
"Guest Rooms: $guestroom\n" .
"Largest Meeting Room Needed: $mtgroom\n" .
"Decision Timeframe: $timeframe\n" .
"Options: $options\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;
$headers =
"From: \"$name\" <$fromemail>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.13.0" .
$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
if ($use_envsender) {
mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;
?>
All I get via email for the checkboxes is "array".
Thanks
Update:
Just noticed I get these errors if I DON'T select a checkbox and submit:
Warning: implode() [function.implode]: Invalid arguments passed in /home/content/o/l/t/oltvcb/html/feedback_mtg.php on line 148
Warning: Cannot modify header information - headers already sent by (output started at /home/content/o/l/t/oltvcb/html/feedback_mtg.php:148) in /home/content/o/l/t/oltvcb/html/feedback_mtg.php on line 162
I did notice that the form data actually came through in my email.
$options is an array. Try imploding it.
$options = implode(', ', $options);
Checkboxes are treated as an array when they are submitted.
foreach($options as $option) {
print $option."\n";
}
or
print implode("\n", $options);
Here's a suggestion. Instead of this:
"Options: $options\n" .
Try this:
"Options:".implode("\n",$options)."\n".
There's always the possibility that no array of $options will exist (no check boxes were checked). In this case, you can do something like:
"Options:".(isset($options) ? implode("\n",$options) : "")."\n".
$options = join(', ', $_POST['options']);
First, you need to get rid of your "[ ]" in the name of your variable for the HTML checkboxes.
If you name it "options" when it is submitted to the processing page it comes through as an array.
From that point, you now have an array of the values selected.
$_POST['options']

Categories