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';
Related
How can I send an HTML-formatted email with pictures using PHP?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
The main problem is to attach files. How can I do that?
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob#example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.
Below is the PHP code for your reference: It’s taken from mail:
<?php
// Multiple recipients
$to = 'aidan#example.com' . ', '; // Note the comma
$to .= 'wez#example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have this code and it will run perfectly for my site:
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/>support#pms.com</td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
Mail function
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing#gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
Sending an HTML email is not much different from sending normal emails using PHP. What is necessary to add is the content type along the header parameter of the PHP mail() function. Here is an example.
<?php
$to = "toEmail#domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</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\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
You can also check here for more detailed explanations by W3Schools.
You can easily send the email with HTML content via PHP. Use the following script.
<?php
$to = 'user#example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header 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: CodexWorld<info#codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome#example.com' . "\r\n";
$headers .= 'Bcc: welcome2#example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
The simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symfony.
You can do it with the standard mail function too, but you'll need a bit more knowledge on how to attach pictures.
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mail() function documentation.
Use PHPMailer.
To send HTML mail, you have to set $mail->isHTML() only, and you can set your body with HTML tags.
Here is a well written tutorial:
How to send mail using PHP
The trick is to know the content id of the image MIME part when building the HTML body part.
It boils down to making the img tag—<img src="cid:entercontentidhere" />
Kronolith.php
Look at the function buildMimeMessage for a working example.
I have added a quick contact form in every page so clients can easily get in touch. However, three different things are happening as below:
sometimes getting inquiries from client. (Genuine I can say, we worked together)
sometimes getting blank message. with Unknown sender every alternate day.
Getting nothing. (Whenever I am trying to check whether all pages working or not. I am not getting email on first time. (Sometimes second, third, fourth time works).
My code is as follows:
PHP script I am using:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>' some message '
);
//echo '<pre>';print_r($_POST);exit;
$name = #trim(stripslashes($_POST['name']));
$email_from = #trim($_POST['email']);
$websitelink = #trim(stripslashes($_POST['websitelink']));
$subject = 'iMarketingJunkies: Inquiry';
$message = #trim(stripslashes($_POST['message']));
// 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";
$headers .= 'From: <'.$email_from.'>' . "\r\n";
//$email_from = $email;
$email_to = 'myemail#email.com';//replace with your email
$body = 'Name: ' . $name . '<br><br>' . 'Email: ' . $email_from . '<br><br>' . 'URL: ' . $websitelink . '<br><br>' . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, $headers);
echo json_encode($status);
die;
?>
Can anybody please help me to get rid of this. I have tested all pages and I am sure we can't add submit button without any data. I am using Cloudfare.
Thank you
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 7 years ago.
Here is the simple using of php mail() function codes :
<?php
$from = "From email goes here";
$to = "To email goes here";
ini_set("display_errors", "On");
ini_set("sendmail_from", $from);
ini_set("SMTP", "localhost");
ini_set("smtp_port", "25");
$subject = "Hello, This is subject";
$message = "<p style='color: green;font-weight: bold;'>Hello World, This is Body</p>";
$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Content-Type: text/html; Charset=UTF-8' . "\r\n";
$headers .= 'From: Birthday Reminder <' . $from . '>' . "\r\n";
$headers .= 'To: Mary <' . $to . '>' . "\r\n";
$headers .= 'Reply-To: Birthday Reminder <' . $from . '>' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message,$headers))
{
echo("<p style='color: blue;font-weight: bold;'>Email Sent :D</p>");
}
else
{
echo("<p style='color: red;font-weight: bold;'>Email Message delivery failed...</p>");
}
?>
Why emails were sent using these codes always go to the spam folder?
Is the order of headers ok?
Which extra headers should i use for prevent spam?
Edit :
I know there are some threads in stack about that.
But i am looking for an updated answers.
So it's not duplicate.
This not only about your script, you may check the reputation of your IP, you shall also set SPF record and DKIM key for help
https://mxtoolbox.com/
This online tool will help you to setup properly your mail server
You may check the senderscore of your IP, since it can affect badly the delivery of your mails
https://www.senderscore.org
I brought a shared sever, and got a little storage space. When i run my web site, the mail function works perfectly. Mail has been sent with subject and message to all receivers. But the mail which is sent mentioned "From : ". Headers what i included are not working.
I want to send with my headers Eg:Form: mymail#mysite.com . How can i do it?
<?php
$output="";
if(isset($_POST['submit']) && !empty($_POST['message']))
{
$sub=$_POST['subject'];
$msg=$_POST['message'];
$sql = "select email from login where status='client'";
$query = mysqli_query($con,$sql);
$to=array();
while($row = mysqli_fetch_array($query))
{
$to[] = $row['email'];
$parts = implode(',',$to);
$headers = 'From: mymail#mysiet.com;';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$subject = $sub;
$content = $msg;
}
if(mail($parts,$subject,$content,$headers))
{
$output="mail sent";
}
else
{
$output="Error in connection, Mail could not be sent. Please try again";
}
}
?>
You need to manually specify the headers, this is the function I use and it delivers to pretty much every provider beautifully. The headers you want are From: and Reply-To: and I also advise the X-Mailer follows what is seen here, I found it years ago via SO as well.
<?php
$to = "user#anotherdomain.com";
$headers = 'From: My Name <email#domain.com.au>' . "\r\n" .
'Reply-To: My Name <email2#domain.com.au>' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-type: text/html; charset=iso-8859-1';
$subject = "My subject line here";
$message = 'Message HTML and Content Here.';
if(mail($to, $subject, $message, $headers)){
// Success Here
}else{
// Failed Here
}
?>
I am not 100% certain it will work for you as I am on a dedicated machine, having said that - I do use it on multiple domains and have never messed with my Apache or php configuration in relation to mail settings.
Read More about the Mail function in PHP: http://php.net/manual/en/function.mail.php
You can try below code, just ripped from php.net
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
Off topic but good to use PHPMailer.
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.