This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
PHP mail function is not working, I can't get email from my domain test#test.com. It worked perfectly before few days back, but not now.
Tried every solutions on Stack Overflow but still not resolved.
HTML form code : 1.html
<form method="post" name="from" action="2.php">
Company Name : <input type="text" name="cname"><input type="submit" value="submit" name="submit">
</form>
PHP mail code : 2.php
<?php
if(isset($_POST['submit']))
{
$cname=trim($_POST['cname']);
//$cname='"'.$cname.'"';
echo $cname;
/* mail to Admin */
$to = 'test#test.com';
$subject = 'Details : From $cname';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = 'From: $cname';
$message = 'Company name : $cname';
if(mail($to, $subject, $message, $headers))
{
echo "Mailer sent!";
} else {
echo "Message Error:";
}
}
?>
Your 2.php should be like this: ( You were printing message at wrong places)
$to should be some working email address where you can check emails. Also it will work on LIVE server. Mostly not worked with localhost.
<?php
if(isset($_POST['submit']))
{
$cname=trim($_POST['cname']);
//$cname='"'.$cname.'"';
echo $cname;
/* mail to Admin */
$to = 'test#test.com';
$subject = 'Details : From $cname';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . strip_tags($cname) . "\r\n";
$message = 'Company name : $cname';
if(mail($to,$subject,$message,$headers))
{
echo "Message sent!";
} else {
echo "Mailer Error: ";
}
}
?>
EDIT2:
Here is the exact code that I have tested and is working.
<?php
if(isset($_POST['submit']))
{
$cname=trim($_POST['cname']);
//$cname='"'.$cname.'"';
echo $cname;
/* mail to Admin */
$to = 'test#test.com';
$subject = 'Details : From '.$cname;
$headers = 'From: '.$cname;
$message = 'Company name : '.$cname;
if(mail($to,$subject,$message,$headers))
{
echo "Mailer sent!";
} else {
echo "Message Error:";
}
}
?>
The only other thing that I changed aside from the extra space in the header variable, was the concatenation of the variable with the input data ($cname).
The email would still send without the concatenation change, but it would display "$cname" instead of the entered data.
EDIT: For the header variable:
$headers = 'From : $cname';
Should actually be:
$headers = 'From: $cname';
I didn't notice the extra space between From and : originally, but I noticed it after I tested it. Email was not received unless the space was removed.
Try this.
Also, the if statement for the mail function will trigger as true if the message is sent. So you want to swap the echo lines as the "error" output should be under the else portion.
<?php
if(isset($_POST['submit']))
{
$cname=trim($_POST['cname']);
//$cname='"'.$cname.'"';
echo $cname;
/* mail to Admin */
$to = 'test#test.com';
$subject = 'Details : From $cname';
$headers = 'From: $cname';
$message = 'Company name : $cname';
if(mail($to,$subject,$message,$headers))
{
echo "Mailer sent!";
} else {
echo "Message Error:";
}
}
?>
Related
I am sending email using php class. It's successfully send a email message but in my Gmail email account it's showing me a empty message. I don't understand why it's showing me empty message content. It's showy unknown sender and a empty message body. Here is the pictures what is shwoing in my Gmail account. Can you please help me about it.
Your help is much more appreciate :)
and
Here is my full code which I'm using to send email using php.
<?php
$name="";
$from="";
$message="";
if(isset($_POST['submit'])){
$name=mysql_real_escape_string($_POST['name']);
$from=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['comments']);
$to="myemail#gmail.com"; // Add your e-mail here
include("library/send_email.php");
}
?>
send_emaiil.php page
<?php
require_once("class_email_sender.php");
$send_email= new class_email_sender($name,$from,$to,$message);
$send_email->send_email();
?>
class_email_sender.php page
<?php
class class_email_sender{
private $name;
private $from;
private $to;
private $to_email;
private $subject;
private $headers;
private $mail_message;
public function __construct($name,$from,$to_email,$mail_message){
$this->name=$name;
$this->from=$from;
$this->to_email=$to_email;
$this->mail_message=$mail_message;
}
public function send_email(){
$this->to=$this->to_email;
//$this->headers="From : $this->from<$this->from>";
$this->headers = 'From:'.$this->from. "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$this->subject="Contact Message From your site";
$this->message="Name :" .$this->name. "\n";
$this->message.="Email :" .$this->from."\n\n";
$this->message.="Message :" . $this->mail_message."\n\n";
mail($this->to_email,$this->subject,$this->message,$this->headers);
echo '<script>alert("Thanks for your mail.We will notify you shortly")
</script>';
}
}
?>
for example here is the code which worked for me
<?php
$username = $_POST["username"]; //Email address you want it to 'appear' to come
$email = $_POST["email"];
if(strlen($username) && strlen($email))
{
$mailText ="The Contact Details: <br>";
}
$mailText=$mailText."<table border=1 cellspacing=0 cellpadding=0>";
while(list($Key, $Val)= each($_POST))
{
$mailText=$mailText."<tr><td width=50%>";
$mailText=$mailText."<b>".$Key."</b></td>";
$mailText=$mailText."<td width=50%>";
$mailText=$mailText.$Val;
$mailText=$mailText."</td></tr>";
}
$to = "youremailid.com";
if(strlen($username) && strlen($email))
{
$subject = "your subject"; //Subject Line
$headers .= "From:yourmail.com\n";
$headers .= "X-Sender: yourheader.com\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$mailforms = mail($to, $subject, $mailText, $headers);
}
?>
try to keep like this acc to ur code and check
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.
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?
I'm very new to PHP and am using a basic template 'send-mail' form on a contact page.
It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?
<?php
$mail_to = 'daniel30293#gmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin#bondofperfection.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Your help is greatly appreciated.
You implode an array of recipients:
$recipients = array('jack#gmail.com', 'jill#gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php
Receiver, or receivers of the mail.
The formatting of this string must comply with ยป RFC 2822. Some examples are:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
Just add multiple recipients comma seperated in your $mail_to variable like so:
$mail_to = 'nobody#example.com,anotheruser#example.com,yetanotheruser#example.com';
See
mail() function in PHP
Here is a simple example:
<?php
// Has the form been submitted?
// formSubmit: <input type="submit" name="formSubmit">
if (isset($_POST['formSubmit'])) {
// Set some variables
$required_fields = array('name', 'email');
$errors = array();
$success_message = "Congrats! Your message has been sent successfully!";
$sendmail_error_message = "Oops! Something has gone wrong, please try later.";
// Cool the form has been submitted! Let's loop through the required fields and check
// if they meet our condition(s)
foreach ($required_fields as $fieldName) {
// If the current field in the loop is NOT part of the form submission -OR-
// if the current field in the loop is empty, then...
if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {
// add a reference to the errors array, indicating that these conditions have failed
$errors[$fieldName] = "The {$fieldName} is required!";
}
}
// Proceed if there aren't any errors
if (empty($errors)) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
$email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );
// Email Sender Settings
$to_emails = "anonymous1#example.com, anonymous2#example.com";
$subject = 'Web Prayer Request from ' . $name;
$message = "From: {$name}";
$message .= "Email: {$email}";
$headers = "From: {$name}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if (mail($to_emails, $subject, $message, $headers)) {
echo $success_message;
} else {
echo $sendmail_error_message;
}
} else {
foreach($errors as $invalid_field_msg) {
echo "<p>{$invalid_field_msg}</p>";
}
}
}
I am using the following script to send mail
<?
extract($_POST);
$subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
$mail = #mail($send,$subject,$content);
if($mail) { echo "Your feedback has been sent"; }
else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>
But this is always ending up in the spam Folder. Why?
You have to use headers while you send mail, to prove that the mail arrives from a genuine source and not a bot.
Try this!
<?
extract($_POST);
$subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:'.$email."\r\n";
$headers .= 'Reply-To: '.$email;
$mail = #mail($feedback,$subject,$content,$headers);
if($mail) { echo "Your feedback is send"; }
else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>