I would like to mail some text as bold in my email content. I have used the following code
$to = 'some#gmail.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$from = $param['email']; // this is the sender's Email address
$headers = "From:" . $from;
$name = $param['name'];
$subject = "Test";
$message = "message.\n\n"
."<b>Name:</b> ".$name . "\n\n"
."Adress: \n"
.$param['address'] . "\n"
.$param['zip'] . ", "
.$param['postal_area'] . "\n\n"
."E-post: ".$param['email'] . "\n\n\n"
mail($to,$subject,$message,$headers);
I have used <strong> also instead of <b> tag, but nothing works.
I get mail in the below format :
<b>Namn:</b> some name
Adress:
BorĂ¥s, Sweden
4837, Boras
E-post: somemail#gmail.com
You are overwriting the header variable again. You need to append to it.
$headers .= "From:" . $from; //You forgot to concatenate here...
// ---^ //Add the .
Try setting the font weight property in a span:
$message = "E-post: <span style='font-weight:strong;'>".$param['email']."</span>";
But also see other reply, you are overwriting the header so it is not reading the message as HTML.
Related
I have a form when a user enters data and submits, it will go to his mail, now I am having problem with keeping my logo in the mail, below is my php code for mail
<?php
if(isset($_POST['submit'])){
$to = "contact#bolstersolutions.com"; // this is your Email address
$from = $_POST['name1']; // this is the sender's Email address
$first_name = $_POST['name2'];
$last_name = $_POST['email2'];
$last_name1 = $_POST['number1'];
$subject = "Referal";
$subject2 = "Your Friend " . $from . " Has Referred You To Us For UK Process";
$message = $from . " has refered the following student :" . "\n\n" . $first_name. "\n\n" .$last_name. "\n\n" .$last_name1;
$message2 = "Your friend " . $from . " has referred you to Bolster Solutions for UK process. We will be more than happy to process your applications, thus helping you in achieving your goals to study MS in UK." . "\n\n" . "Feel free to go to the following url for more information or else you can also call us # +91 9666999430 / 7661919191." . "\n\n" . "URL: https://consultancy.bolstersolutions.com/mail/" . "\n\n" . "Thanks and Regards." . "\n\n" . echo "<img src='https://consultancy.bolstersolutions.com/mail/assets/img/logo.png'>" ;
$headers = "From:" . $from;
$headers2 = "From: Bolster Solutions " . $to;
mail($to,$subject,$message,$headers);
mail($last_name,$subject2,$message2,$headers2);
}
?>
the following error is happening:
Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\mail\index.php
Can anyone please help me with this, thanks in advance.
Remove echo in $message2
Set headers like this
$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=ISO-8859-1\r\n";
Then send : mail($last_name,$subject2,$message2,$headers);
There is a syntax error near: ..."\n\n" . echo "<img src.... Removing echo should solve it.
I'm trying to send the message as HTML, but it's arrived as XML Escape!!
Example in screenshot:
second problem is, if i typing the subject in "Arabic language" it's encoded in ANSI.
but if i test to send the same message via Gmail the subject be fine, but the content arrived as XML Escape!!
PHP CODE:
<?php
if(isset($_POST['submit'])){
$to = "email#gmail.com";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = $first_name . " " . $last_name . " " . "\n\n";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$headers = "From: Brand Name <info#my-dmoain.me>" . "\r\n";
mail($to,$subject,$message,$headers,"-f info#my-dmoain.me");
echo "Thanks";
}
?>
I cant figure out what is wrong.
Thanks
Add concat shorthand .= to this line:
$headers = "From: Ali Najm <ali-najm#iraqnaa.me>" . "\r\n";
You're reassigning $headers var.
Sending nice html with php
I cannot get this header to show the variable needed for some strange reason. I know its something very slight. I know that this works:
$headers .= 'Bcc: example#aol.com' . "\r\n";
Can someone please help me with this. FULL CODE:
<?php
if ($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .= "<hr />Sent from your Website at example.com.";
$to = 'person#aol.com';
$from = $email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// THE EMAIL IS NOT SENDING THE SENDER OF THE EMAIL
// the from and bcc do not work as needed!
// More headers, put on double quotes
$headers .= 'From:'.$from."\r\n";
$headers .= 'Bcc: '.$from."\r\n";
// send
if (mail($to,$subject,$message,$headers))
{
echo "Message was successfully sent! $from";
} else {
echo "Something went wrong..";
}
} else {
}
?>
You are missing a dot (period) after your first 'Bcc: '.
it should be
$headers .= 'Bcc: ' . $from . "\r\n";
UPDATE:
You need to use double quotes when using special characters like \r and \n. In your original example code you used double quotes, but I see now with your full code that you're not actually using them, you're using single quotes.
Single quoted strings in PHP show special characters as literals.
So your lines should be
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Bcc: ' . $from . "\r\n";
Use this will work perfectly:
$headers .= 'From: "'.$from.'"' . "\r\n";
easiest and the best way
"From: ". $email. "\r\n";
So the issue is I want multiple recipients for my PHP form.
What happens is the site user enters there email and then another email for another peroson (for example there doctor).
So what I need is the the doctor to be emailed to.
This is what I am using to know success
$mail_to = $field_emaildoc .$field_email;
This doesent seem to work?
Any ideas would be great :)
Thanks
one option is to add a "Cc" to your header:
$sender_email = 'email#domain.com';
$sender_name = 'YOUR NAME';
$send_to = 'email#domain.com';
$send_to_copy = 'anotheremail#domain.com';
$message = 'THIS IS YOUR MESSAGE';
$subject = 'THIS IS YOUR SUBJECT';
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= "From: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "X-Sender: $sender_name<" . $sender_email . ">" . "\n";
$headers .= "X-Mailer: PHP " . phpversion() . "\n";
$headers .= "X-Priority: 3" . "\n";
$headers .= "X-Sender-IP: " . $_SERVER['REMOTE_ADDR'] . "\n";
$headers .= "Return-Path: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Reply-To: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Cc: $send_to_copy" . "\r\n";
mail($send_to,$subject,$message,$headers);
The problem with this, is that the person receiving the email can see who was copied in. An alternative would be to use: "Bcc" instead of "Cc" or just use the mail() function twice and remove the "Cc" or "Bcc":
mail($send_to1,$subject,$message,$headers);
mail($send_to2,$subject,$message,$headers);
You should put comma between mail addresses . Look explanation of to parameter, here : http://php.net/manual/en/function.mail.php
You'll need a comma:
$mail_to = $field_emaildoc . ',' . $field_email;
Use the default mail function from PHP.
The recipients are devided by a Comma.
When you want CC and BCC you can set the header. Example from PHP.net:
$header .= 'To: Simone <simone#example.com>, Andreas <andreas#example.com>' . "\r\n";
$header .= 'From: Geburtstags-Erinnerungen <geburtstag#example.com>' . "\r\n";
$header .= 'Cc: geburtstagsarchiv#example.com' . "\r\n";
$header .= 'Bcc: geburtstagscheck#example.com' . "\r\n";
mail($empfaenger, $betreff, $nachricht, $header);
I have a contact form that sends 2 mails one to the user and the other to the admin.
The email that is sent to the user has no problems, yet the other is not working and giving a success code.
Here is my code:
This is the config file (emails and contents removed):
$adminEmail = "*********#*****.com";
$adminSubj = "Admin Subject";
$userSubj = "User Subject";
////////////////////////////////////////////////////////
//DON`T EDIT BELOW THIS LINE
////////////////////////////////////////////////////////
$userName = $_GET['name'];
$userEmail = $_GET['email'];
$userPhone = $_GET['phone'];
$userMessage = $_GET['message'];
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: " . $userName ."<" . $userEmail . ">" . "\r\n";
$userHeaders = 'MIME-Version: 1.0' . "\r\n";
$userHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$userHeaders .= "To: " . $userName . "<" . $userEmail . ">" . "\r\n";
$userHeaders .= "From: website <no-reply#website>" . "\r\n";
And this is the file that sends the mail:
include("config.php");
$userSendMessage = "Your Message:<br />
$userMessage<br /><br /><br />
We will get back you.
<br /><br />
Thanks
Website Team.
";
$adminSendMessage = "
$userName<br />
$userEmail<br />
$userPhone<br />
$userMessage";
$toUser = mail($userEmail,$userSubj,$userSendMessage,$userHeaders);
$toAdmin = mail($adminEmail,$adminSubj,$adminSendMessage,$adminHeaders);
if($toAdmin && $toUser)
echo 1;
else
echo 0;
I guess that the problem is in the $adminHeaders
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: " . $userName ."<" . $userEmail . ">" . "\r\n";
when I use the headers as this it works:
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: Someone <someEmail#someServer.com" . "\r\n";
Any idea how to send the mail to the admin by the user email?
The hard work has been done for you. There are many libraries that do what you're needing with my favorite being here:
http://phpmailer.worxware.com/
It's easy to use, very robust and keeps the spam from webforms down quite well.
You can consider using PHP Mailer.
It can be used as a wrapper for the mail() function, and it's that easy:
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$mail->SetFrom($fromAddress, 'First Last');
$mail->AddReplyTo($replyToAddress, "First Last");
$mail->AddAddress($toAddress, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
// $mail->AddAttachment("images/phpmailer.gif"); // attachment
// $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Some mail domains have checks to ensure the sender is not pretending to be some other domain. The from address is checked if no sender is specified.
This may be fixed by adding another header:
Sender: someone#yoursendingserver.com
Also please note your script is vulnerable to header manipulation as it's not properly escaped. This can be only easily be done in PHP 5.3 by using quoted_printable_encode e.g.
'From: ' . quoted_printable_encode($userName . '<' . $userEmail . '>');
earlier PHP versions don't have a simple way of doing this correctly without a library.
I agree with the other answers that you should look at using a library to do this, however I don't suggest you use php mailer as its no longer in active development (although it might be feature rich).
I use the mail class that comes from Zend Framework (as I use the framework as well):
http://framework.zend.com/manual/en/zend.mail.introduction.html
Apache Zeta components (formerly ez components), does a good implementation:
http://qafoo.com/blog/011_apache_zeta_components_doing_mail_right.html
When I write scripts like these, I always check whitespace and eliminate as much as I possibly can so they headers are sent correctly. Otherwise, glance at the examples on the mail function.
I noticed that you have 2 square brackets at the end of the line below when the adminHeaders are set.
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
When you changed the headers to the following:
$adminHeaders .= "From: Someone <someEmail#someServer.com" . "\r\n";
You missed a closing brace after the "Someone" email address.
In more of the examples on the manual page, they show the "To" and "From" parameters only as the email address. Try removing the braces and duplication of the email all together. You may also want to secure your values from the $_GET variable, which could potentially be exploited. It may also be worth debugging what the $_GET variables are giving you for the values of $userName and $userEmail (this can be as simple as echoing or vardumping the variables).
I was also facing the same issue and after some R&D i found that if we eliminate the newline characters from the message field then our PHP mail function start working
for e.g. The previous message which i was using --
$message = '<html>
<head>
<title>App Approval</title>
</head>
<body>
<p>Hi amir!</p>
<table>
<tr>
<td>Amir Khan has created a new app HH</td>
</tr>
<tr>
<td>Click the following link in order to view the app.</td>
</tr>
</table>
</body>
</html>
';
after changing it to
$message = '<html><head><title>App Approval</title></head><body><p>Hi amir!</p><table><tr><td>Amir Khan has created a new app HH</td></tr><tr><td>Click the following link in order to view the app.</td></tr></table></body></html>';
It started working.
Also if you are confused regarding how to use variables in message content in between single qoutes then my suggestion is to just use it as double quotes
as for this
$message = 'blah blah blah';
use this
$message = 'blah'.$blah.' blah';