Adding html properties in a php - php

In brief, I am trying to write a message to the user once they have submitted the contact form using php. The problem is that I am trying to style the message a bit by adding stylistic element like bold, and spacing certain lines out with <br>. It does seem to apply.
Below is the code.
$message = "Welcome". $full_name . "to Company!" . "<br>". "<br>". "<b>". " Company is
the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". "
Letting you interact with you mail by viewing and deciding what to do with it as it is received at a
Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the following link"."<br>". "<a href=''http://company.com/userinterfaceSelect.php'>Your Dashboard</a>" ;
In it, there are <br>, <b>, and a link, in which none seems to apply.
Update;
<?php
if(isset($_POST['submit'])){
$to = $_POST['email'];// this is your Email address
$from = "info#company.com"; // this is the sender's Email address
$full_name = $_POST['fullName'];
$address = $_POST['address'];
$subject = "Welcome to company";
$subject2 = "Copy: New Registration";
$message2 = "New User". "<br>" .$full_name . "\n\n" ;
$message = "Welcome". $full_name . "to company!" . "<br>". "<br>". "<b>". " company is
the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". "
Letting you interact with you mail by viewing and deciding what to do with it as it is received at a
Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the following link"."<br>". "<a href=''http://company/userinterfaceSelect.php'>Your Dashboard</a>" ;
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Your mail has been sent successfuly ! Thank you for your feedback";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>

You are very close to getting it to work.
You need to add a header to indicate that the content is HTML:
Content-Type: text/html; charset=ISO-8859-1
For example, your header might be formed as follows:
$headers = "From: you#yoursite.com\r\n";
$headers .= "Reply-To: me#example.com\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
See the details in the references below.
References:
http://php.net/manual/en/function.mail.php
http://css-tricks.com/sending-nice-html-email-with-php/

Related

Echoing image in php mail , showing error

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.

php mail reply subject

So I managed to use "reply-to" and set an email to be replied to, however I'm wondering if its possible to change the subject of the email after its replied? instead of Re: 'Original Subject'?
$header = "From: " . $from ."\r\nReply-to: " . $email . "\r\n";
maybe something like this?
P.S. this 2nd code doesn't work, its just to help me get my point across
$header = "From: " . $from ."\r\nReply-to: " . $email . "\r\nReply-subject: Company Support Message\r\n";

Can we include 2 Mail() functions in 1 Page?

I want to sent 2 different E-Mails to 2 different persons on 'Send Message' Button. I have few questions regarding this.
Is it possible to send Multiple Emails from same Page.
I have written the following code but its not sending Any Email and returning Error Message in return.
<?php
include 'connect.php';
$subject = $_REQUEST['subject'] ; // Subject of your email
$personal_email= "abc#hotmail.com";
$message .= 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= $_REQUEST['product_type']. "<br>";
$message .= $_REQUEST['message'];
$subject_to_sender= "Confirmation";
$message_to_sender = "Thanks for contacting us, Our representative will contact you shortly.";
$name= mysql_real_escape_string($_REQUEST['name']);
$email= mysql_real_escape_string($_REQUEST['email']);
$message= mysql_real_escape_string($_REQUEST['message']);
$product_type= mysql_real_escape_string($_REQUEST['product_type']);
$address= mysql_real_escape_string($_REQUEST['address']);
if($address== "")
{
$address= "No Address is given.";
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['email'] . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (#mail($email, $subject_to_sender, $message_to_sender, $headers))
{
mail($personal_email, $subject, $message, $headers);
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
To answer your former question, I can say Yes, its possible to use mail() function multiple times.
For the later question, the opening bracket on last if() statement is not closed. Add } after echo 'sent'; statement.

PHP - Email is not sending

There are two steps first the email i sent to the person who wants the newsletter and the second email needs to be sent to info#dirtytrend.com. The problem is that the email is sent in the first step but the email in the second step is never getting through but the code seems to carry on without any errors.
The code is below
<?php
$name = $_POST["nameofperson"];
$to = $_POST["emailofperson"];
$subject = "Hi!";
$body = "Hi " . $name . ",<br><br>Thank you for subscribing?\n\nWe have logged your email to process your newsletter and you will recieve an email from us confirming your subscription.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (mail($to, $subject, $body, $headers)) {
$body2 = "Name: " . $name . "\n";
$body2 .= "Email: " . $to;
if(mail("info#dirtytrend.com", "Subscription Request", $body2)){
header("Location: http://www.dirtytrend.com/events.html");
}
else{
}
} else {
echo "ERROR: Email not sent please contact the system administrator";
}
?>
In the second mail() you have only 3 parameters and you're missing the 4th one with the headers so maybe it is recognized as spam?

PHP Problem Sending Mail with headers

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

Categories