Unable to send HTML mail through mail() - email

The mail sent by the below code includes all the HTML tags. How can I get rid of that tried a few ways but still mail output is scrambled with html tags. And also need help on if any better way of sending array of elements through mail();
My code:
<?php
include('Mail.php');
include('mail\mime.php');
$errors=array();
if(empty ($_POST)===false)
{
//$product = $_POST['product'];
//$quantity = $_POST['quantity'] ;
$email="noreply#cortexonline.in";
$item1 = $_POST['product']['1'];
$item2 = $_POST['product']['2'];
$item3 = $_POST['product']['3'];
$item4 = $_POST['product']['4'];
$item5 = $_POST['product']['5'];
$quan1 = $_POST['quantity']['1'];
$quan2 = $_POST['quantity']['2'];
$quan3 = $_POST['quantity']['3'];
$quan4 = $_POST['quantity']['4'];
$quan5 = $_POST['quantity']['5'];
$message = "<html><head><title></title>
</head> <body> <table>
<tr><td>Shop id=</td>
</tr><tr><td>Product Name</td>
<td>Quantity</td></tr>
<tr><td>".$item1."</td>
<td>".$quan1."</td></tr>
<tr><td>".$item2."</td>
<td>".$quan2."</td></tr>
<tr><td>".$item3."</td>
<td>".$quan3."</td></tr>
<tr><td>".$item4."</td>
<td>".$quan4."</td><td>".$item5."</td><td>".$quan5."</td></tr>
</table></body></html>";
$header = 'From: "Quick order" <noreply#cort.in>'.PHP_EOL.
'Reply-to: <noreply#cort.in>'.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-Type: text/plain; charset=utf-8'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
if(ctype_alpha($quan1)===true||ctype_alpha($quan2)===true||ctype_alpha($quan3)==true||ctype_alpha($quan4)===true||ctype_alpha($quan5)===true)
{
$errors[]=' Quantity cannot be alphabet /n
Cannot send request';
}
if(isset($_POST['send'])===true)
{
if (empty($errors)===false)
{
echo '<script language="javascript">alert("NOT SENT! Qunatity cannot be Alphabet")</script>';
}
else
{
mail('order#cortexonline.in','Quick order',$message,$header);
echo '<script language="javascript">alert("Your order sent!")</script>';
echo '<script language="javascript">window.location = "index.php"</script>';
}
}
else
{
echo '<script language="javascript">alert("ORDER NOT SENT! Please Try Again.")</script>';
echo '<script language="javascript">window.location = "index.php"</script>';
}
}
?>
Also tried using single quotes it is not working.

Try this:
$header = 'From: "Quick order" <noreply#cort.in>'.PHP_EOL.
'Reply-to: <noreply#cort.in>'.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-Type: text/html; charset=utf-8'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;

Related

page has Too many redirects from mail.php file?

I hope someone can help me. I have a page with a form and then the information
is emailed to me. the code below seems to be redirecting to the same page over and over again. It used to work for years now its not always working.
any help appreciated. Thanks Steve
<?php
//****************************************
//edit here
$senderName = 'WEB';
$senderEmail = 'site#example.com';
$targetEmail = 'contact#sellmydiabeticteststrips.com';
$messageSubject = 'Message from web-site';
$redirectToReferer = true;
$redirectURL = 'contact.html';
//****************************************
// mail content
$ufname = $_POST['ufname'];
$uaddress = $_POST['uaddress'];
$uemail = $_POST['uemail'];
$utel = $_POST['utel'];
$ucity = $_POST['ucity'];
$ustate = $_POST['ustate'];
$uzip = $_POST['uzip'];
$ubrand = $_POST['ubrand'];
$unumber = $_POST['unumber'];
$ucheck = $_POST['ucheck'];
$agree = $_POST['agree'];
// collect interests data
$interestsString = '';
for($i = 0; $i < count($interests); $i++) {
$interestsString .= $interests[$i].($i < count($interests) - 1 ? ', ' : '');
}
// prepare message text
$messageText = 'First Name: '.$ufname."\n".
'Address: '.$uaddress."\n".
'Email: '.$uemail."\n".
'Telephone: '.$utel."\n".
'City: '.$ucity."\n".
'State: '.$ustate."\n".
'Zip: '.$uzip."\n".
'Brand of test strips: '.$ubrand."\n".
'Number of boxes: '.$unumber."\n".
'Check: '.$ucheck."\n".
'Terms and Conditions: '.$agree."\n";
if($interestsString) {
$messageText .= 'Kinderen wonen bij mij: '.$interestsString."\n";
}
// send email
$senderName = "=?UTF-8?B?" . base64_encode($senderName) . "?=";
$messageSubject = "=?UTF-8?B?" . base64_encode($messageSubject) . "?=";
$messageHeaders = "From: " . $senderName . " <" . $senderEmail . ">\r\n"
. "MIME-Version: 1.0" . "\r\n"
. "Content-type: text/plain; charset=UTF-8" . "\r\n";
if (preg_match('/^[_.0-9a-z-]+#([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$targetEmail,$matches))
mail($targetEmail, $messageSubject, $messageText, $messageHeaders);
// redirect
if($redirectToReferer) {
header("Location: ".#$_SERVER['HTTP_REFERER'].'#sent');
} else {
header("Location: ".$redirectURL);
}
?>

Two contact forms, send to two different email addresses

I got two contact forms on my website. On in the contact section, and one in the careers section. I want to receive both forms in different email adresses.
<?php
include_once('class.phpmailer.php');
$mail = new PHPMailer;
$mail->setFrom('adres1','test');
$mail->Subject = 'Contactverzoek via careerspagina.';
$html = "Er is een contactverzoek gedaan via de careerspagina.<br>";
$html .= "<br>";
$html .= "<b>Naam:</b> " . $_POST['name']."<br>";
if(isset($_POST['lastname'])) {
$html .= '<b>Achternaam: </b> ' . $_POST['lastname']."<br>";
}
$html .= "<b>E-mail:</b> " . $_POST['email']."<br>";
if(isset($_POST['telefoon'])) {
$html .= '<b>Telefoon: </b> ' . $_POST['telefoon']."<br>";
}
if(isset($_POST['onderwerp'])) { //FIX NOG!!!
$html .= '<b>Onderwerp: </b> ' . $_POST['onderwerp']."<br>";
}
if(isset($_POST['Werkervaring'])) {
$html .= '<b>Werkervaring: </b> ' . $_POST['Werkervaring']."<br>";
}
if(isset($_POST['Functie'])) {
$html .= '<b>Functie: </b> ' . $_POST['Functie']."<br>";
}
if(isset($_POST['Opleidingsniveaus'])) {
$html .= '<b>Opleidingsniveaus: </b> ' . $_POST['Opleidingsniveaus']."<br>";
}
if(isset($_POST['comment'])) {
$html .= '<b>Bericht: </b> ' . $_POST['comment']."<br>";
}
$html .= "<br>";
if (isset($_FILES['resume']) &&
$_FILES['resume']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['resume']['tmp_name'],
$_FILES['resume']['name']);
}
if (isset($_FILES['letter']) &&
$_FILES['letter']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['letter']['tmp_name'],
$_FILES['letter']['name']);
}
$mail->msgHTML($html);
$mail->addAddress('adres1', 'Info ');
$mail->addBCC('adres1', 'Test ');
if(!$mail->send())
{
echo "0";
}
else
{
echo "1";
}
This is my code, and everything seems to work.
So once again. I want to keep sending this contact form to email A. But There needs to be made and else statement which sends the other contact form to email B.
i'm very new to PHP. Excuse me if i'm not clear.
One simple solution is to send a form parameter indicating which email address should be used. Then you check the POST variable for that parameter in an if statement and set the correct email address to be used.
$to = "forma#example.com";
if($_POST['form'] === "formB") {
$to = "formb#example.com";
}
$mail->addAddress($to, 'Recipient name');
Add hidden inputs in your forms named "form" and set the value to either "formA" or "formB":
<input type="hidden" name="form" value="formA">
Do not set the receiving email address in the form and use that since it can easily be abused to send spam from your server.

htmlspecialchars($_SERVER['PHP_SELF'])

if($_POST) {
$mysql_pass = '';
$connect = #mysql_connect($_POST['dbHost'], $_POST['dbUser'], $mysql_pass);
if($connect) {
if(#mysql_select_db($_POST['dbName'])) {
$dir = $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'];
$redirectUrl = str_replace("configForm.php","site/index", $dir);
print $redirectUrl; exit;
$dbConf = dirname(__FILE__).'/dbConfig/dbconf.php';
$handle = fopen($dbConf, 'w') or die('Cannot open file: '.$dbConf);
$data = '<?php $userName="'.$_POST['dbUser'].'";'."\n";
$data .= '$passWord=" ";'."\n";
$data .= '$dbName="'.$_POST['dbName'].'";'."\n";
$data .= '$host="'.$_POST['dbHost'].'"; ?>';
fwrite($handle, $data);
header('Location: site/index');
}
else {
$error = 'Could not select database.';
}
}
else
$error = 'Not connected';
}
when the code above was executed, my webpage just displayed "localhost/www-edusec/site/index". i do not know where i do it wrong. my form action is like below:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The action attribute defaults to the current page anyway, so that piece of code is surely redundant.
The problem is PHP_SELF returns the path to the file too. Just get rid of the action line completely and it should work fine.
EDIT:
Also, the reason your script is cutting off half way, you have an exit; after it prints out the directory.

email not sent when trying to send as HTML

I am able to send plain text but not HTML..
Please assist as why the mail is not being recieved..
THX in advance :)
Below is the code:
$mailBody ='<table width="100%" >
<tr>
<td>
<img src="MailerImages/img_logo.gif" alt="UrMint.com"/>
<h1 style="font:bold 20px tahoma; color:#808080;">Hi #USERNAME,</h1>
<p style="font:13px tahoma; color:#666666; line-height:25px;">
Your password for xyzacccount associated with mobile no password #MOBNO is : <strong style="color:#e65786;">#PASSWORD</strong>
<br />
If you didn\'t request a new password, let us know immediately.
<br />
This message was sent from xyz.com at your request.
</p>
<strong style="font:bold 13px tahoma; color:#666666; line-height:25px;">
Happy Learning ,
<br />
The <span style="color:#42b1d6;">Ur</span><span style="color:#e65786;">urMint</span> Team
</strong>
</table>
';
echo $mailBody;
SendEMail_fun('xyz#gmail.com', $mailBody,'sub','xyzsender','xyz#rediffmail.com');
function SendEMail_fun($strEmailTo,$strEmailBody,$strSubject,$SenderName = '',$SenderEmail='')
{
global $SwiftMessage;
$SenderName = $SenderName <> ''?$SenderName:"support#xyz.com";
$SenderEmail = $SenderEmail <> ''?$SenderEmail:"support#xyz.com";
$email_from_name = $SenderName." <".$SenderEmail.">";
$email_from_mail = $SenderEmail;
$email_to_bcc= "";
$email_to= $strEmailTo;
$email_subject= $strSubject;
$emailbody =$strEmailBody;
try
{
require_once ("lib/Swift.php");
$smtp = new Swift_Connection_SMTP("localhost", 25);
$swift = new Swift($smtp);
$message = new Swift_Message($email_subject, $emailbody, "text/html");
$message->setFrom($email_from_name);
$message->setReplyTo($email_from_mail);
$message->setBcc($email_to_bcc);
if ($swift->send($message, $email_to, $email_from_mail))
{
//echo "Message sent";
echo $SwiftMessage = 'Message Sent';
return true;
}
else
{
//echo "Message failed to send";
echo $SwiftMessage = 'Message failed to send';
return false;
}
$swift->disconnect();
return true;
}
catch (Swift_ConnectionException $e)
{
// echo "There was a problem communicating with SMTP: " . $e->getMessage();
echo $SwiftMessage = "There was a problem communicating with SMTP: " . $e->getMessage();
return false;
}
catch (Swift_Message_MimeException $e)
{
// echo "There was an unexpected problem building the email:" . $e->getMessage();
echo $SwiftMessage = "There was an unexpected problem building the email:" . $e->getMessage();
return false;
}
}
I'm not sure the exact reason yours is not sending. I've included the html email script I always use. Hope it helps.
<?php
$myFile = "email.html";
$fh = fopen($myFile, 'r');
$htmlemail = fread($fh, filesize($myFile));
fclose($fh);
$to = "YOUREMAIL#gmail.com" . ",";
//$to .= "#gmail.com" . ",";
$subject = "SUBJECT";
$message = $htmlemail;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'From: NAME <email#email.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I'm wondering if the mail order has any effect on if it will work or not:
You have
if ($swift->send($message, $email_to, $email_from_mail))
Mine:
mail($to, $subject, $message, $headers);
Worth a shot

Php form redirect to thank you page

I'd like to use this free form template but I want it to redirect me to a thank you page upon submission.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "e-mail here";
$email_subject = "subject here";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
How can I add a code where I will be redirected to a page of my choice?
header("Location: http://page_of_your_choice.com");
Just insert this where you have the
<!-- include your own success html here -->