Using expressmail with PHP form - php

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

Related

How to remove special character from email using codeignitor

How to remove special character from email using codeignitor
Email is adding some special character don't know why and how this is going using codeignitor how to solve this this is my Email.php system file please guide
protected function _build_message()
{
if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
{
$this->_body = $this->word_wrap($this->_body);
}
$this->_set_boundaries();
$this->_write_headers();
$hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
$body = '';
switch ($this->_get_content_type())
{
case 'plain' :
$hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding();
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
$this->_finalbody = $this->_body;
}
else
{
$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
}
return;
case 'html' :
if ($this->send_multipart === FALSE)
{
$hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable';
}
else
{
$hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
$body .= $this->_get_mime_message().$this->newline.$this->newline
.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
.$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
}
$this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
else
{
$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
}
if ($this->send_multipart !== FALSE)
{
$this->_finalbody .= '--'.$this->_alt_boundary.'--';
}
return;
case 'plain-attach' :
$hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
$body .= $this->_get_mime_message().$this->newline
.$this->newline
.'--'.$this->_atc_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
.$this->newline
.$this->_body.$this->newline.$this->newline;
break;
case 'html-attach' :
$hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
$body .= $this->_get_mime_message().$this->newline.$this->newline
.'--'.$this->_atc_boundary.$this->newline
.'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
.$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
.'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
break;
}
$attachment = array();
for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
{
$filename = $this->_attachments[$i]['name'][0];
$basename = ($this->_attachments[$i]['name'][1] === NULL)
? basename($filename) : $this->_attachments[$i]['name'][1];
$attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
.'Content-type: '.$this->_attachments[$i]['type'].'; '
.'name="'.$basename.'"'.$this->newline
.'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
.'Content-Transfer-Encoding: base64'.$this->newline
.(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
$attachment[$z++] = $this->_attachments[$i]['content'];
}
$body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
$this->_finalbody = ($this->_get_protocol() === 'mail')
? $body
: $hdr.$this->newline.$this->newline.$body;
return TRUE;
}
What would be right approach to fix it please guide thanks in advance
one more thing i would like to tell i'm using smtp_crypto => tls that causing issue
You could fix this issue by replacing instances of _prep_quoted_printable in the Email class with the PHP native function quoted_printable_encode.
Just replace inside _build_message()
How this help you !

Not getting CSS with HTML Based Email In PHP?

I am trying to receive a loop based generated HTML Table in email but seems like I am getting email but no CSS embedded with it as I am passing the BootStrap library CSS files with the email but it's not getting any CSS at all..So I am wondering that what would be the problem...??
Here is screenshot as :
Here is my whole code as :
<?php
$message .= '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">';
$message .= '<table class="table table-bordered">';
$message .= '<thead>';
$message .= '<tr>';
$message .= '<th>#</th>';
$message .= '<th>Username</th>';
$message .= '<th>Session From</th>';
$message .= '<th>Session Till</th>';
$message .= '<th>Uptime</th>';
$message .= '<th>Download</th>';
$message .= '<th>Upload</th>';
$message .= '<th>Total Usage</th>';
$message .= '</tr>';
$message .= '</thead>';
$message .= '<tbody>';
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor > 0) $sz = 'KMGT';
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . #$sz[$factor - 1] . 'B';
}
if (isset($_GET)) {
$user = $_GET["user"];
}
$x = 1;
$handle = fopen($user, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$split_data = (explode(" ",$line));
if (in_array('customer=admin', $split_data)) {
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'user=') !== false) {
$username = explode("=", $split_data[$i]);
$username = $username[1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'from-time=') !== false) {
$from_time = explode("=", $split_data[$i]);
$from_time = $from_time[1];
$from_time = $from_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'till-time=') !== false) {
$till_time = explode("=", $split_data[$i]);
$till_time = $till_time[1];
$till_time = $till_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'uptime=') !== false) {
$uptime = explode("=", $split_data[$i]);
$uptime = $uptime[1];
$download = explode("=", $split_data[$i+1]);
$download = $download[1];
$upload = explode("=", $split_data[$i+2]);
$upload = $upload[1];
#$total_download += $download;
#$total_upload += $upload;
$total_usage = $total_download+$total_upload;
}
}
$message .= '<tr>';
$message .= '<th scope="row">'.$x.'</th>';
$message .= '<td>'.$username.'</td>';
$message .= '<td>'.$from_time.'</td>';
$message .= '<td>'.$till_time.'</td>';
$message .= '<td>'.$uptime.'</td>';
$message .= '<td>'.human_filesize($download,2).'</td>';
$message .= '<td>'.human_filesize($upload,2).'</td>';
$message .= '<td>'.human_filesize($total_usage,2).'</td>';
$message .= '</tr>';
$x=$x+1;
}
}
fclose($handle);
}
$message .= '</tbody>';
$message .= '</table>';
$to = 'nicefellow1234#gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
?>
As of September 2016, Gmail accepts embedded styles – CSS within <style> tags in the head section of HTML documents.
This is in addition to inline styles, which were previously the only way to apply CSS in Gmail.
At the same time, Google says nothing about support for external styles, which is likely why your Bootstrap styles are failing to load.
https://developers.google.com/gmail/design/

how to put a php array in an email message?

I have a general question about sending an email trough php. How do you put php arrays in the message part of the mail?
below you can see a simple array that i made and tried to put it in the email but this is totally wrong cause I didnt find it on the internet how to do it (I am a beginner. I just started coding with php).
The array:
<?php
$Array[1] = array('qualitypoint', 'technologies', 'India','Hey');
$Array[2] = array('quality', 'tech', 'Ind','He');
$Array[3] = array('q', 't', 'I','H');
?>
The mailer:
<?php
include "index.php";
while($row = $LevAdres->fetch_assoc()) {
$email=null;
$email= $row['Email'];
}
$to = example#hotmail.com;
$subject = "Bestelling";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: Restaurant#test.be" . "\r\n";
$headers .= "Reply-To: people#info.be". "\r\n";
$headers .= "CC: Restaurant#test.be\r\n";
$message = "<?php
echo '<table>';
echo '<table>';
for($i=0;$i<=3;$i++)
{
echo "<tr>";
for($j=1;$j<=3;$j++)
{
echo "<td>{$Array[$j][$i]}<td>";
}
echo "</tr>";
}
echo '</table>';
?>";
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
?>
This is how the table must look like
Here you go ... complete code
$Array[1] = array('qualitypoint', 'technologies', 'India', 'Hey');
$Array[2] = array('quality', 'tech', 'Ind', 'He');
$Array[3] = array('q', 't', 'I', 'H');
$to = "example#hotmail.com";
$subject = "Bestelling";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: Restaurant#test.be" . "\r\n";
$headers .= "Reply-To: your#email.com" . "\r\n";
$headers .= "CC: Restaurant#test.be\r\n";
$message = "";
$message .= '<table>';
for ($i = 0; $i <= 3; $i++) {
$message .= "<tr>";
for ($j = 1; $j <= 3; $j++) {
$message .= "<td>{$Array[$j][$i]}</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
Change your $message variable to this.
$message = "<table>";
for($i=0;$i<=3;$i++)
{
$message .= "<tr>";
for($j=1;$j<=3;$j++)
{
$message .= "<td>{$Array[$j][$i]}<td>";
}
$message .= "</tr>";
}
$message .= '</table>';
I can see that you have a doublequotes issue. Try putting it this way:
echo '<td>'."{$Array[$j][$i]}".'<td>';
If you want to put it into message then don't echo it but write it in message body
change your echo code to
$array = [
['qualitypoint', 'technologies', 'q'],
['technologies', 'tech', 't'],
['India', 'ind', 'i'],
];
$message = "";
$message .= '<table>';
foreach ($array as $row)
{
$message .= "<tr>";
foreach ($row as $cell)
{
$message .= "<td>$cell</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
However if you want to echo it then you can catch output buffering and use it contents but I don't recommend it. Using foreach makes it more readable as well. Also consider naming your variables with lower camel case.

form php attachment - specify which info I want to show in mail

I have this code that works good, but when i send it in the mail also appears things that I don't want that appears, for example:
submit = Enviar
recaptcha_challenge_field =
03AHJ_VuuCrxaVdxnIIg-Us7zrqZBDMBXeOuU21J60IblOCtFTQhMgtx-TsfsY6oHl5xbgdeqIRUQ7bTui
recaptcha_response_field = rsinmBlockquote
So I want to ask if there is a way of especify wich items I want to send something like:
$body="Contacto:
Nombre: " .$_POST["nombre"] . "\n
Empresa: " .$_POST["empresa"] . "\n
Ubicacion: " .$_POST["ubicacion"] . "\n
Telefono: " .$_POST["telefono"] . "\n
I'm not a programmer so I don't know how to put it into the actual code that is working:
function form_mail($sPara, $sAsunto, $sTexto, $sDe)
{
$bHayFicheros = 0;
$sCabeceraTexto = "";
$sAdjuntos = "";
if ($sDe)$sCabeceras = "From:".$sDe."\n";
else $sCabeceras = "";
$sCabeceras .= "MIME-version: 1.0\n";
foreach ($_POST as $sNombre => $sValor)
$sTexto = $sTexto."\n".$sNombre." = ".$sValor;
foreach ($_FILES as $vAdjunto)
{
if ($bHayFicheros == 0)
{
$bHayFicheros = 1;
$sCabeceras .= "Content-type: multipart/mixed;";
$sCabeceras .= "boundary=\"--_Separador-de-mensajes_--\"\n";
$sCabeceraTexto = "----_Separador-de-mensajes_--\n";
$sCabeceraTexto .= "Content-type: text/plain;charset=iso-8859-1\n";
$sCabeceraTexto .= "Content-transfer-encoding: 7BIT\n";
$sTexto = $sCabeceraTexto.$sTexto;
}
if ($vAdjunto["size"] > 0)
{
$sAdjuntos .= "\n\n----_Separador-de-mensajes_--\n";
$sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";;
$sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
$sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";
$oFichero = fopen($vAdjunto["tmp_name"], 'r');
$sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
$sAdjuntos .= chunk_split(base64_encode($sContenido));
fclose($oFichero);
}
}
if ($bHayFicheros)
$sTexto .= $sAdjuntos."\n\n----_Separador-de-mensajes_----\n";
return(mail($sPara, $sAsunto, $sTexto, $sCabeceras, '-faltitux#altitux.mx'));
}
/* enviando correo */
if (form_mail("mymail#mail.com", $_POST[asunto],
"Los datos introducidos en el formulario son:\n\n", $_POST[email]))
$resultMenuUrlName = "bolsa_gracias.html";
echo "<META HTTP-EQUIV=Refresh CONTENT=0;URL=$resultMenuUrlName>";
} else {
echo "Lo sentimos pero no ha colocado el texto correctamente! Intente nuevamente...";
}
thx in advice and sorry for the bad english
this should work:
remove
foreach ($_POST as $sNombre => $sValor)
$sTexto = $sTexto."\n".$sNombre." = ".$sValor;
in the same spot add:
$sTexto="Contacto:
Nombre: " .$_POST["nombre"] . "\n
Empresa: " .$_POST["empresa"] . "\n
Ubicacion: " .$_POST["ubicacion"] . "\n
Telefono: " .$_POST["telefono"] . "\n

Remove empty fields in an array after foreach in PHP

I am new to PHP. This is my code from our mailing.php. When a user submits a request, there are 5-7 select-able fields and 20-25 fields that end up not being selected. The output lists all fields and values regardless of whether they are empty or have been selected. I understand I need to use either unset or array_filter, but cannot figure out how and where I need to insert into code.
if($_POST && count($_POST)) {
$body = '';
foreach($_POST as $key=>$value)
$body .= $key . ": " . $value . "\r\n";
mail("email#email.com", "Email Received at email#email.com", $body);
You can try this
if($_POST && count($_POST)) {
$_POST = array_filter($_POST);
$body = '';
foreach($_POST as $key=>$value)
$body .= $key . ": " . $value . "\r\n";
mail("email#email.com", "Email Received at email#email.com", $body);
OR
if($_POST && count($_POST)) {
$body = '';
foreach($_POST as $key=>$value){
$trim_value = trim($value);
if (!empty($trim_value)){
$body .= $key . ": " . $value . "\r\n";
}
}
mail("email#email.com", "Email Received at email#email.com", $body);
Just before foreach loop you should use this
$_POST = array_filter($_POST);
Another option is to use a conditional inside foreach loop
foreach($_POST as $key=>$value)
if ($value != '' && $value != null)
$body .= $key . ": " . $value . "\r\n";

Categories