Parsing HTML in an email with PHP - php

I have a form I am using to simply capture a name and email from a user. When the user submits the form an email is then fired to the specified recipient containing a link.
I had this all working but realised I needed to add a html footer to the email which was originally being read as plain text.
After doing some digging I have now tried to set the footer to read as html ,but the email no longer sends. I am not a php developer.
My question is have I missed something or am I just way off?
<?php
// configure
$from = 'Ether Solutions Sales <email#domain.co.uk>';
$sendTo = $_POST['remail'];
$subject = "test email";
$name = $_POST['name'];
$okMessage = 'your message has been sent successfully, please ask the recipient to check their inbox';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<p style="margin-bottom:30px;">regards dave</p>';
$message .= 'www.domain.co.uk';
$message .= '<p style="font-color:blue;">Office Tel: 0844 000 000</p>';
$message .= '<p style="font-color:blue;">Mobile: 0000 000 0000</p>';
$message .= '<p style="font-color:blue;">Technical Support: 0845 000 0000</p>
';
$message .= '<p style="font-color:blue; font-weight:bold;">Some tagline here</p>
';
$message .= '<img src="imageurl/test.png" alt="sample alt tag"/>';
$message .= '</body></html>';
// let's do the sending
try
{
$emailText = "{$name} you have been sent an electronic document to be signed.\nTo view your document please click the following link URL: /www.domain.com";
mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}

I tested your code and got this warning:
PHP Warning: mail() expects at most 5 parameters, 6 given in /home/ruemercc/public_html/lab/email2/mail.php on line 47
I would suggest you use PHPMailer library found here phpmailer
. It will allow you to even send bulk emails(Which I think will be an added advantege in your case).
Have fun coding

Your call of the mail() wrong. You are setting the parameters not correct.
Take a look in the PHP Manual for mail().
From the php manual
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Your code mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from) uses message as $additional_headers which is wrong.
You have to merge your $message with the $emailText.

Related

what is wrong in php script to send mails [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
I am trying to send emails using a php script whose code is below. What is the error in the code? Parsing error is:
PHP Parse error: syntax error, unexpected 'https' (T_STRING) in /workspace/Main.php on line 9
"""
<?php
$our_email = "agrawal.shivam#godonphone.website";
$to = "agrawal.shivam2602#gmail.com";
$from = $our_email;
$subject = "God Apps on Mobile Phone";
$message = "
God on Mobile! Please go through these contents for your personal growth & distribute to others as well:
echo 'krishna.science.blog';
Share the Love & Knowledge in your physical proximity also.
Physical address: Krishna, Vrindavan, Uttar Pradesh, India (IN), Pin Code:- 281121
echo 'Unsubscribe ";
$mail = mail($to, $subject, $message, "From: $our_email");
if ($mail) {
echo "Mail Sent";
}
else {
echo "Error - Mail Not Sent";
}
?>
Your message is not your website PHP file. You just need to show that the message you are sending is HTML, and write pure HTML instead of using echo PHP statements.
To send HTML use
<?php
$to = 'maryjane#email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
As per https://www.tutorialrepublic.com/php-tutorial/php-send-email.php

HTML tags are displayed in mail [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 3 years ago.
I am trying to send a text by mail in php. The message is placed inside a HTML tag. While sending, the mail part works but the text I receive has HTML tags in it.
Here is the code for better understanding of my issue:
$car = "1223455667";
$to = "something#sample.com";
$subject = 'Test Mail';
$message = '<p>' . $car . '</p>';
$from = 'mymail#sample.com';
if(mail($to, $subject, $message )){
echo 'success';
}else{
echo 'Unable to send the email. Please try again.';
}
Output I receive in mail:
I expect the output like this: 1223455667
Pass Headers in your mail
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers )){
echo 'success';
}else{
echo 'Unable to send the email. Please try again.';
}

Sending form mail to more than one person PHP?

my form is working as intended but for some reason the email will only send to one of my email accounts and not the other I am putting the right email in the email field so that isn't the issue however I can't seem to see where I am going wrong I assume it's because I'm using $email to grab the email address to where the 2nd email is suppose to go...here is my php where am I going wrong?
<?php
$from = 'Pixel Wars - Press Inquiry';
$to = "my-email#gmail.com, $email";
$subject = 'Press Inquiry from Pixelwars.com';
function errorHandler ($message) {
die(json_encode(array(
'type' => 'error',
'response' => $message
)));
}
function successHandler ($message) {
die(json_encode(array(
'type' => 'success',
'response' => $message
)));
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "Name: $name\r\n Email: $email\r\n\r\n Message:\r\n $message";
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message)) {
errorHandler('Header injection detected.');
}
// Check if name has been entered
if (!$_POST['name']) {
errorHandler('Please enter your name.');
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
errorHandler('Please enter a valid email address.');
}
// Check if message has been entered
if (!$_POST['message']) {
errorHandler('Please enter your message.');
}
// prepare headers
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/plain; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $to" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
// send the email
$result = #mail($to, $subject, $body . "\r\n\n" .'------------------ '. "\r\n\n" .'Hello '.$name.' we will contact you as soon as possible about your query.' ."\n". 'Dont forget to keep visiting www.pixelwars.com for more updates and awesome content.' ."\n". 'We will email you back on the provided email below, thank you and have a nice day.' . "\r\n\n" .'-- '.$email, $headers);
if ($result) {
successHandler('Thank You! we will be in touch');
} else {
errorHandler('Sorry there was an error sending your message.');
}
} else {
errorHandler('Allowed only XMLHttpRequest.');
}
?>
Thank you in advance if anyone can crack it
You don't have $email assigned when you are defining $to so your second address is not set.
Demo: https://3v4l.org/QIIJu
Solution, move the $to assignment to later in the script. Also use error reporting, this would have thrown an undefined variable notice.
e.g.
<?php
$from = 'Pixel Wars - Press Inquiry';
$subject = 'Press Inquiry from Pixelwars.com';
....
$to = "my-email#gmail.com, $email";
$result = #mail($to, $subject, $body ....
because at this point the $email is defined. Also don't use error suppression, that is just hiding useful information. If you don't want it displayed hide the error displaying but still log them.
You need to add the multiple email address in $to
$to = "address#one.com, address#two.com, address#three.com"
Needs to be a comma delimited list of email adrresses.
mail($email_to, $email_subject, $thankyou);
Thanks

message does not appear when sending email to other than gmail [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am having a problem sending an email to the following domains:
#yahoo.com,
#hotmail.com, and
#mncgroup.com
using the PHP mail() function. But there's no problem if I send an email to #gmail.com
Is there something wrong with my code?
$to = "$email";
$subject = "[NO-REPLY]Confirmation Account Pengaduan Keluhan I-news Tv";
$header = "From: inewsit#mncgroup.com \r\n";
$header .= "Akun Information MNC Biro";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Selamat Akun Anda Sudah Aktif</h1>
<p>Detail Account Username :</p>
<br>Your username : $hasil[username]
<br> Your Full Name : $hasil[nama]
<br> Your Email Address : $hasil[email]
<br> Your Status Akun : $status_akun1
<br> Your Lever authentication : $hasil[level]
<br> Your Register Date : $hasil[tanggal_register]
<br>Login sekarang ke : <a href='http://mncgroup.hol.es'><i>http://mncgroup.hol.es</i></a>
</body>
</html>";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
?>
<div class="alert alert-success alert-dismissable">
<a class="panel-close close" data-dismiss="alert">x</a>
<center>Silahkan Cek <strong>Email</strong> Anda</center>
</div>
<?php
when i run this code and i try to send email to 3 domain above, the message does not get into the email
First, try this in your code:
echo (mail($to, $subject, $message, $headers)) ? 'Message sent!' : 'Message not sent!';
Because of mail() function returns true or false depending on whether the mail was accepted for delivery. I recommend you check your spam folder at those addresses. I have heard that mail sent from the free servers due to the amount of abuse goes directly to spam.
Try this sample code for checking purpose:
<?php
$to = 'ANY EMAIL#yahoo.com';
$subject = 'ANY SUBJECT';
$message = 'ANY MESSAGE';
$headers = 'From: check#check.com' . "\r\n" .
'Reply-To: no-reply#check#check.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo (mail($to, $subject, $message, $headers)) ? 'Message sent!' : 'Message not sent!';
?>
Try replacing all occurrences of \r\n with PHP_EOL. Something like this:
$to = "$email";
$eol = PHP_EOL;
$subject = "[NO-REPLY]Confirmation Account Pengaduan Keluhan I-news Tv";
$header = "From: inewsit#mncgroup.com $eol";
$header .= "Akun Information MNC Biro";
$headers .= "MIME-Version: 1.0$eol";
$headers .= "Content-Type: text/html; charset=ISO-8859-1$eol";
See if that changes anything.

Why the mail content sent are the raw html codes?

Mail content:
<html><head></head><body><p> Message : 鏽嫌╒杜米土 杯屎</p>Share Link : Press here to enter <br><img src ='http://203.80.1.28/FlippingBook/development/demo/medium/Web081112_P070_medium.jpg' /></body></html>
PHP:
if (isset($_POST["data"])){
$info = explode("&", $_POST["data"]);
$headers = 'MIME-Version: 1.0\r\n';
$headers .= "Content-Type: text/html; charset = \"UTF-8\";\n";
$headers = "From: =?UTF-8?B?" . base64_encode(substr($info[0],strpos($info[0],'=')+1, strlen($info[0]))) . "?=";
$to = substr($info[1],strpos($info[1],'=')+1, strlen($info[1]));
$subject = "=?UTF-8?B?" . base64_encode('日報分享') . "?=";
$message = trim(substr($info[2],strpos($info[2],'=')+1, strlen($info[2])));
$message = '<html><head></head><body><p> Message : '.$message;
$url = substr($info[3],strpos($info[3],'=')+1, strlen($info[3]));
$message = $message. '</p>Share Link : Press here to enter ';
if (isset($info[4])){
$firstImg = substr($info[4],strpos($info[4],'=')+1, strlen($info[4]));
$message = $message."<br><img src ='".$firstImg."' />";
}
if (isset($info[5])){
$secondImg = substr($info[5],strpos($info[5],'=')+1, strlen($info[5]));
$message = $message."<br><img src ='".$secondImg."' />";
}
$message = $message.'</body></html>';
if (mail($to, $subject, $message, $headers))
die ('Mail sent');
else
die ('Fail');
}else{
die ('Fail');
}
I am writing a simple program to send email. However, my mail content is not english based so I used utf-8 to encode.
When I changed the encode method, it can not send the processed html code, instead the mail content is the raw html code shown above, how to fix this problem?
If using PEAR lib is not a problem than you can look for Pear Mail-Mime lib to send mail or HTML contents. More details you can get from here - http://pear.php.net/manual/en/package.mail.mail-mime.example.php

Categories