Sending HTML email from php with variables - php

I'm not very good at php so this should (hopefully) be easy for one of you.
I'm trying to send an HTML email from php which includes variables. I'm able to send a none html email with variables, or an HTML email without variables, but I can't seem to do both at the same time. Here's my code:
<?
$to = "me#something.co.uk";
$from = "m2#something.com";
$subject = "Hello! This is HTML email";
'
//begin of HTML message
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
'. $_POST["Opt_out_phone"] .'
</p>
</html> EOF;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo "Message has been sent....!"; ?>
I've never come across <<< EOF before and have no idea what it's doing.

You're using HEREDOC syntax and attempting to terminate the string and append the variable incorrectly and unnecessarily. To declare your string with the variable, do this:
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
{$_POST["Opt_out_phone"]}
</p>
</html> EOF;
Note that the PHP manual entry on heredoc syntax is with the rest of the string docs, here.

Related

Sending email in PHP - Form

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

Php html mail not displaying in html

I am trying to send an html email. Mail is working properly but not going as html email and it is just displaying ".$message.". The data is coming from the mysql database.
My script is as follows:
$to = $email;
$subject = $subject;
$message = "$message";
$htmlContent = '
<html>
<body>
".$message."
<br /><br />Regards<br /><b>TM</b>
</body>
</html>';
$headers = 'From: myemail#gmail.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
if(mail($to, $subject, $htmlContent, $headers))
echo "Mail Sent";
else
echo "Problem sending email";
You have mixed single and double quotes. Use only one type when concatenating strings.
$htmlContent = "
<html>
<body>
".$message."
<br /><br />Regards<br /><b>RainbowCTM</b>
</body>
</html>";
To complete previous answer, there is a huge difference between single and double quotes:
Single quote not analyze characters inside, so if you did: echo '$message', it just display $message.
Double quotes analyze characters inside, so if you did: echo "$message", it displays the content of $message variable.
Finwe is totally right.
Explanation: single quotes always take the string as it is without processing variables or metasigns like \t or \n.
just a hint in addition: try using only \n and not \r\n since there was a bug in some PHP-version that produced erros. even if this is not a problem at the moment, it might become one after an update (depending on your PHP-version).

Combining PHP code run on server with ready-made HTML email template

I was wondering how can I possibly combine on my FTP server the PHP sourcecode that meant to send emails while launched on server side with an e-mail template which I have already made using some email generator.
The main problem I've got is that I can't find any answer on how to setup these files on server and combine them with eachother, should I use one file or several named differently. I'm sorry that I represent such novice level, so excuse me for my ignorance.
Here's my PHP sourcecode that is meant to be a base for an e-mail sending, I just don't know how can I possibly combine it with my HTML email template.
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "Message example";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>
Is there any way I can simply include the sourcecode of my emails HTML template to this code ?
As far as I understood the problem, you can easily write php and html in a common file. Open and close the php, html tags as and when needed.
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "Message example";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>
<html>
<body>
<p>Write HTML here</p>
</body>
</html>
You need to include your HTML in the $message variable, one way to do it is like this:
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "<div>Your email HTML Template here</div>";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>

Few PHP script E-Mails Not getting delivered

I have created folders under my domain.
Emails scripts in folder-1 are getting delivered but scripts in folder-2 show message = "email sent successfully". But these emails are not receiving in email address.
I am using phpmailer
When this sampleemail.php file is kept in Folder1, Email get delivered. But when same file is kept in Folder2, Error Message is there.
Code is as follows :
<?
$msg="";
if(isset($_POST['submit']))
{
$from_add = "name#my-web-site.com";
$to_add = "myemail#gmail.com"; //<-- put your yahoo/gmail email address here
$subject = "Test Subject";
$message = "Test Message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
}
?>
And HTML Sample Form As Follows :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test form to email</title>
</head>
<body>
<?php echo $msg ?>
<p>
<form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>
<input type='submit' name='submit' value='Submit'>
</form>
</p>
</body>
</html>
I would suggest you use PHPmailer because it has the function which you needed. The reason i am not suggest you use your method because it need to change the sendmail() function in your localhost.
For example: if u using xampp, you must go into the sendmail directory and go for sendmail.php to edit the SMTP to mail.google.com and so on.
Please try this tutorial:
http://codeforgeek.com/2014/11/phpmailer-ultimate-tutorial/
Hope it can helps you.
Looking at your code I see you are NOT using "PHPMailer", but "PHP's mailer".
Regarding your problem of undelivered emails and error message, here are some general hints:
If you send HTML mail, always send a multipart email containing both, HTML and TEXT
Don't use "X-Mailer: PHP". There are mail servers who will give your mail an increased spam score just because of that.
If you get an error, try this:
$errLevel = error_reporting(E_ALL ^ E_NOTICE);
mail(...);
error_reporting($errLevel);
Try using a different delivery method (smpt, sendmail, ...)

Make 'To' field only show one recipient

This is the PHP code I am using upon clicking 'Submit' button at the form.
Currently, when user inputs the email addresses, the message will be sent to those people. Upon checking my email when I was testing, I noticed that at the 'To:' field, it shows all the recipients.
I would like to hide the rest of the recipients' emails. How do I do this?
<?php
// request variables like beneath // important
// $name=$_REQUEST["name"];
// multiple recipients
$to = $_REQUEST['email1'] . ', '; // note the comma
$to .= $_REQUEST['email2'];
// subject
$subject = 'Movie World: Stay Tuned!';
$message = '
<html>
<head>
<title>Movie World</title>
</head>
<body>
<p>Movie World you would not wanna miss!</p>
</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";
// Additional headers
$headers .= 'From: Movie World <movie#movieworld.com>';
// Mail it
mail($to, $subject, $message, $headers);
?>
<script type="text/javascript">
alert("Success! Thank you for your enquiry.");
<!--
window.location = "form.html"
//-->
</script>
You need to add an additional header for BCC, Blind Carbon Copy
$headers .= 'Bcc: ' . $_REQUEST['email2'] . "\r\n";
If you don't want to use a bcc then you need to use a foreach or similar.
Collect an array of email addresses using
<input type="text" name="address[]" />
then loop through them:
foreach($_POST['address'] as $email) :
$to = null;
$to = $email;
// validate the email
// set up the headers
mail($to, $subject, $message, $headers);
endforeach;
might be worth looking at a proper mail library for anything more advanced.
And be careful as this kind of form can be a goldmine to spammers unless you control the input properly by limiting the amount of emails, proper sanitization etc.

Categories