PHP : How to use dynamic variables - php

I'm trying to send HTML email from my Custom Controller.
Here's code.
public function execute()
{
$to = "dev#test.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>'.$_POST["company_name"].'</td>
</tr>
</table>
</body>
</html>
";
// 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>';
mail($to,$subject,$message,$headers);
How Can I use dynamic variables based on POST request so that Email Send perfectly with Dynamic form.
I've tried as I paste above code but still getting syntax erros in PHP Strom.
Any Help would be appreciated.

Instead of:
<td>'.$_POST["company_name"].'</td>
use:
<td>".$_POST['company_name']."</td>
Your HTML is quoted with double quotation so to return to PHP you also need to use double one.

Two things. For multiline strings, I'd recommend using heredoc. Secondly, save the form data into a variable before using it in the string.
$company_name = $_POST['company_name'];
$message = <<<EOD
<html>
<h1>$company_name</h1>
<html>
EOD;

Related

How to redirect to home page after submitting the Contact Form?

i have created contact form but i am getting two problems
Reply-To mail is not going.
After Submitting the form page has to redirect to Home page.
For reference please find the attached image and code
Below is the PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name']; // Get Name value from HTML Form
$email_id = $_POST['email']; // Get Email Value
$mobile_no = $_POST['Mobile']; // Get Mobile No
$msg = $_POST['message']; // Get Message Value
$to = "somasekhar.n#vitalticks.com"; // You can change here your Email
$subject = "'$name' has been sent a mail"; // This is your subject
// HTML Message Starts here
$message ="
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>Name: </strong></td>
<td style='width:400px'>$name</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$email_id</td>
</tr>
<tr>
<td style='width:150px'><strong>Mobile No: </strong></td>
<td style='width:400px'>$mobile_no</td>
</tr>
<tr>
<td style='width:150px'><strong>Message: </strong></td>
<td style='width:400px'>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
// 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: New Contact Form <".$_POST["email"].">\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
$headers .= 'Cc: somumstr210#gmail.com' . "\r\n"; // If you want add cc
// $headers .= 'Bcc: somasekhar.n#vitalticks.com' . "\r\n"; // If you want add Bcc
$headers .= "Reply-To: ".$_POST["email"]."\r\n";
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
}
else{
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
?>
The mail function is deprecated and may not work right! I recommand phpmailer https://github.com/PHPMailer/PHPMailer
How do I make a redirect in PHP?
header("Location: path/to/file");
please check post variables
$variable = $_POST['variable-name'] ?? "default content if $_POST['variable-name'] is undefined";
EDIT: mail() is not deprecated but please use the PHPmailer because it's better

Unable to send mail using php mail() function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
http://webprojects.co/dev_wed/mail.php trying to send mail from server using this script but i am not been able to send mail from server
<?php
$to = "test#gmail.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webprojects#webprojects.com>' . "\r\n";
$headers .= 'Cc: webprojects#webprojects.com' . "\r\n";
$result = mail($to,$subject,$message,$headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
?>
always display error trying to send email using php mail() function
Are you testing the code online or via localhost? The mail() function doesn't work in localhost without SMTP.

Customers email displays HTML tags on PHP system generated mail

When I send a really basic HTML email via a file using PHP mail it displays perfectly for me. But for my client he sees all the HTML tags in the email.
I have my own dedicated server, to try to problemsolve I ran the exact same file from a Heart Internet shared server I have and the email displayed perfectly to my client (no HTML tags) so it points to something to do with the server.
<?php
$to = 'tom#myemailaddress.com';
$subject = 'TOM Testing: HTML displaying correctly?';
$message = '
<html>
<head>
<title>This is a test</title>
</head>
<body>
<p>To see if</p>
<table>
<tr>
<th>You</th><th>See</th><th>The</th><th>HTML</th>
</tr>
<tr>
<td>or</td><td>it</td><td>is</td><td>formatted</td>
</tr>
<tr>
<td>correctrly</td><td>1</td><td>2</td><td>abc</td>
</tr>
</table>
</body>
</html>
';
// 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";
// Mail it
mail($to, $subject, $message, $headers);
?>
Here is my php info: http://www.saloponline.co.uk/phpinfo.php

how to send image mailer using php mail function

i am trying to send html image mailer using php mail function on linux platform. There is one issue when i am trying to send simple html content then it successfully get delivered to its subscriber. but when i try to send emailer which contains few images then it fails to deliver it. below are two codes 1 by which simple html is delivered. 2 which is not getting delivered.
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
code 2:
<?php
//change this to your email.
$to = "abhinav#xyz.in";
$from = "abhinav.1#gmail.com";
$subject = "Hello! This is HTML email";
//begin of HTML message
$message= <<<EOF
<html>
<body bgcolor="#DCEEFC">
<center>
<b>Looool!!! I am reciving HTML email......</b> <br>
<font color="red">Thanks Mohammed!</font> <br>
* maaking.com
</center>
<br><br>*** Now you Can send HTML Email <br> Regards<br>MOhammed Ahmed - Palestine
</body>
</html>
EOF;
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email]maa#p-i-s.cXom[/email]";
//$headers .= "Bcc: [email]email#maaking.cXom[/email]";
// now lets send the email.
mail($to, $subject, $message, $headers);
echo "Message has been sent....!";
?>
I corrected the code by which I am able to send image mailer below is the code:
<?php
$to= 'abhinav#xyz.com' . ',';
$to .= 'abhinav#gmail.com';
$sub='test1';
$msg= <<<EOF
<html>
<body>
<table>
<tr>
<td><img src="http://d32vlg867bsa1v.cloudfront.net/z/prod/w/2/i/zovi-logo2.png" />
</td>
</tr>
</table>
</body>
</html>
EOF;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <centos_test#example.com>' . "\r\n";
mail($to,$sub,$msg,$headers);
?>
I am still working to sent highly customized mailer and will describe how it can once I found how it can be done.

hyperlink in php html email not working

I am using below code to send email using php
<?php
$to = $_POST['emailbox'] ;
$message1 = nl2br($_REQUEST['output_textarea']);
$subject = 'script';
$message = "
<html>
<body>
<table bgcolor='Lavender' width='100%'>
<tr><td><font face=consolas>$message1</font></td></tr>
</table>
<br/><br/>
Hyperlink Code
</body>
</html>
";
$headers = "From: ase#abc.com\r\n";
$headers .= "Reply-To: anoop#abc.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
?>
But the hyper link mention in above code Hyperlink Code giving me below error:
Parse error: syntax error, unexpected T_STRING in eval()’d code on line 24
If I remove the the hyperlink line code from code, its working fine. how can I mention the hyperlink in email ?
Notice how the syntax highlighting breaks in your code? Notice that it happens when you use double quotes?
The simple answer is the change out:
Hyperlink Code
For:
Hyperlink Code
But you can also do:
$message = <<<_E_
<html>
<body>
<table bgcolor='Lavender' width='100%'>
<tr><td><font face=consolas>$message1</font></td></tr>
</table>
<br/><br/>
Hyperlink Code
</body>
</html>
_E_;
It's called Heredoc syntax.
I think that the double quotes ("") are making some noise ¿?
Maybe you can try single quotes (''), like:
<a href='http://www.hyperlinkcode.com'>Hyperlink Code</a>
Sorry if i'm wrong..

Categories