Sending email in PHP - Form - php

I have been trying to do tests to send an email through a form with php, but it does not work for me. The following message is returned: Error sending the email. That is, the value of the variable boolean $ sent is false.
I use Apache, XAMPP for Windows 10 with the Netbeans 8.2 development environment
Let's see if anyone can help me, thanks in advance.
This is the code that is giving faults:
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html><head><title> Email with HTML </title></head>
<body><h1> Email with HTML </h1>
This is an email that is sent in HTML format
<hr>Sent by my program in PHP</body></html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers. = 'Content-type: text / html; charset = utf-8 '. "\r\n";
$headers. = 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>

as far as I can see, you have some concatenation issues, I m not sure those actually exist in your code or are due to copy paste. Try following, take care of $header concatenation and semicolons ; at the end of statements.
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html>'.
'<head> <title> Email with HTML </title> </head>'.
'<body> <h1> Email with HTML </h1>'.
'This is an email that is sent in HTML format.'
'<hr>'.
'Sent by my program in PHP'.
'</body>'.
'</html>';
$headers = 'MIME-Version: 1.0'. "\ r \ n";
$headers .= 'Content-type: text / html; charset = utf-8 '. "\ r \ n";
$headers .= 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>

Related

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.';
}

Parsing HTML in an email with 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.

Insert PHP variable to href link in HTML

I am sending a confirmation mail to user to confirm his account. The confirmation mail is styled using HTML and has an href element which points the users to a PHP file where I do the confirmation process. This href link also needs to have a PHP randomstring attached, the same randomstring is saved in Database and is also sent to user so that the cross-checking can be done in PHP once the user clicks on it.
<td align="center" style="margin:0;text-align:center">
<a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>"
style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>
The PHP code includes the above HTML as follows.
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>
However, the PHP variable is't getting available in the link.
Please try this. Remove file_get_contents and use include with output buffer function. I think this will insert your variable to your template. Please check the below updated code. Anyway your method is not a secure. Please create more complicated random code
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
ob_start();
include "email_template.php";
$htmlContent=ob_get_contents();
ob_end_clean();
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>

Hyperlink not getting displayed in Php mail

I'm trying to email a link thru PHPMail. Somehow link is showing as plain text in email.
I want that link should be clickable when received in email. Currently it is showing as plain code.
Below is the code, please help!
<?php
$name=$_POST['firstname'];
$email=$_POST['email'];
$phoneno=$_POST['phoneno'];
$com=$_POST['comment'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
Print Boss your receipt
<?php
//$link="hello.php?a=$name&b=$email&c=$phoneno&d=$com";
$to = "ketanjoshi1993#gmail.com";
//$from = $email;
$subject = "send email in pdf format";
$message = "<a href='#'>test</a>";
$email = mail($to,$subject,$message);
if( $email == true )
{
echo "sent";
}
else
{
echo "not sent";
}
?>
You forgot to use your headers in your mail call:
$email = mail($to, $subject, $message, $headers);
So you forget to add the headers
try adding it as
$email = mail($to,$subject,$message, $headers);
It will work
You forget to add the headers
You can do like this :
$email = mail($to,$subject,$message, $headers);
If you don't pass the header parameter in PHP mail() function , default headers will be set & your will be treated as simple text body.

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