Inserting line breaks in message of php mail function (userdetails) - php

This is the code that I am using to send mail. But to have line breaks in message that is to be delivered in mail is not inserting new lines. I tried using "","/n","/r/n". But i am getting these along with the message or else message is not at all getting delivered.
<?php
$to="******";
$sub="contact_details";
$userdetails="Name:".$_REQUEST["name"];
$userdetails.="<br/>Phone number:".$_REQUEST["phone"];
$userdetails.="<br/>Email:".$_REQUEST["email"]";
$userdetails.="<br/>Message:".$_REQUEST["message"].`"<br>"`;
if($_REQUEST["name"] && $_REQUEST["phone"] && $_REQUEST["email"] && $_REQUEST["message"])
{
$mail=mail($to,$sub,$userdetails);
if($mail){
echo "<script type='text/javascript'>alert('Submitted Successfully!')
</script>";
}
else{
echo"<script type='text/javascript'>alert('Failed!')</script>";
}
}
else {
echo "<script type='text/javascript'>alert('Please enter all fields!');
</script>";
}
?>
Thanks in advance.

The email that you are sending is not html supported, yow will need to add \n and in order to make the html work, you will need to add headers like this:
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);

You have to correct the syntax for errors, you have added extra "(double quote) after email, remove this, and you can use "\n" for adding the line breaks.
<?php
$to="******";
$sub="contact_details";
$userdetails="Name:".$_REQUEST["name"]. "\n";
$userdetails.="<br/>Phone number:".$_REQUEST["phone"]. "\n";
$userdetails.="<br/>Email:".$_REQUEST["email"]. "\n";
$userdetails.="<br/>Message:".$_REQUEST["message"]. "\n";
?>
Also header are not mandatory to send the mail, you can send the mail even if header are not set.

Related

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

Insert html in a php script

I want to use html code to appear in $message when it is sent, thank you.
Here it's the php script
<?php
// read the list of emails from the file.
$email_list = file("elist.txt");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list into a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$subject = "TEST";
$message = "TEST";
$headers = 'From: TEST <TEST#gmail.com>' . "\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
Add your html into $message and add Content-type.
$subject = "TEST";
$message = "<html><body><p>Text</p></body></html>";
$headers = 'From: TEST <TEST#gmail.com>' . "\r\n";
// 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";
To make it easier to read:
$html = <<<MSG
<html>
<head>
....
</head>
<body>
<h2> text title </h2>
<span>$variable</span>
....
</body>
</html>
MSG;
Be careful not to leave blank spaces after <<<MSG or before MSG;

mail not getting sent on server

I have 2 websites on hosted but on one web site I can send mail using php script using other web site I cannot mail using the php script. I don't know where I am going wrong. I even replaced php.ini file. Is there any other thing which I left doing or something.
This is my code
$to="abc#gmail.com";
$subject="test mail";
$body="test mail";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers.='From: Domain <def#gmail.com>' . "\r\n";
if(mail($to,$subject,$body,$header))
{
echo "mailed";
}
else
{
echo "not mailed";
}
I even tried directly embedding message into the mail function that also doesn't seems to be working.
This is the code after embedding message
if(mail("abc#gmail.com", "Test subject", "Test Message", "From: def#gmail.com"))
{
echo "mailed";
}
else
{
echo "error:".mysql_error();
}
I tried contacting my hosting providers technical people. Even they could not find the solution.
it's Sample And Works for me:
$to ='Info#sample.com';
$message = '<a>Hellow</a>';
$subject ='Hello world';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: <sender#email.com>" . "\r\n";
$mail_sent=#mail($to,$subject,$message,$headers);
echo $mail_sent ? "Success" : "Error";
if this code not working , Call to Your server Administrator For check Server...

HTML not formatting in mail function

I am using the following code:
$message = "Hi ".$user.
",\r\rThe following names are on the guestlist for <b>".$night.
"</b> on ".$date2.":\r\r". $user ."<br>". implode(", ", $names).
"\r\rThank you for using Guestvibe.";
mail($email, "Your Guestvibe list", $message);
The carriage returns in PHP are working fine, but neither the <b> or <br> tags are coming out. Is it just my mail client (Apple Mail) playing up, or is there a fix for this?
You are sending the e-mail as text only not HTML, I believe you need to set these headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
as in example #4 of the docs.
PHP's mail function sends plaintext mail by default.
See the nr.4 example at mail's manual page for the correct way to send HTML mail.
You need to add your headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($email, "Your Guestvibe list", $message, $headers);
You should firstly wrap the whole message into:
<html><head></head><body> msg </body></html>
Plus headers from m.edmondson post.

Send email with just a jpg image

i need to send an email to some list with just a jpg as the body of the message, this are the headers i'm using, but it's not working when i add the image (instead, if i write some random text the email is sent correctly)
This is what i'm using so far:
message = '<img src="http://www.gulfview.info/admin/birthday_card_goes_here/birthday.jpg" width="600" height="800" />';
$subject = 'Happy Birthday from your Gulfview Friends !!';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: Bob Fregalette <info#thegiftstores.org>' . "\r\n";
if(!mail($email, $subject, $message, $headers)){
echo $email." not sent <br>";
}
else {
echo $email ."Sent succesfully<br>";
}
Any hint?
Thanks
I'm assuming you've assigned something to the value of $email somewhere else?
Your code works as written for me if I assign something to the $email value before this code.
If I run it without a value of $email, it has a fatal error that there's no recipient message in the message header.

Categories