not able to send email from PHP page - php

I want to send email from contact page of the my site. I google and it told to use PHP scripts as my domain is LINUX based so I cannot use ASP. I tried couple of them but not able to send. How ever I have used #ECHO to display message, which I got but not the email. Here the codes that I tried:
<?php
$userid='to.useri#example.com';
$subject='New Requirement';
$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Phone=$_POST['Phone'];
$Message=$_POST['Message'];
$body= <<<EOD;
<br><hr><br>
Name: $Name <br>
Email: $Email <br>
Phone: $From <br>
Message: $Message
EOD;
$headers='From: $Email';
mail($userid,$subject,$body,$headers);
echo "Message send!!!";
?>
And I also tried :
<?php
$to = 'to.user#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from.user#example.com' . "\r\n" .
'Reply-To: from.user#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers )) {
ECHO 'Message send successfully';
}
else {
ECHO 'Please try again, Message could not be sent!';
}
?>
Can any one tell me what am I missing here.

Change your if Condition with the below mentioned code, hope it works:-
$mail=mail($to, "Subject: $subject",$message );
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}

try this code
$senderName="John";
$senderEmail= "test#domain.com";
$recipient = "recipient#domain.com";
$subject ="testmail";
$message="test message";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail($recipient, $subject, $message, $headers );

Related

WordPress - prevents server from sending mails and wp_mail does't work?

Why WP prevent me from sending mail?
I created a file mail.php and tested the PHP mail():
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
I uploaded it my server, in the root directory. I run it and I received emails with that.
But when I use
$message = trim($_POST['sender_message']);
$email = trim($_POST['sender_email']);
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ". get_bloginfo('name') . ": " . $subject;
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
// $sent = wp_mail($to, $subject, strip_tags($message), $headers);
$sent = mail($to, $subject, strip_tags($message), $headers);
var_dump($send); // bool true
if($sent) {
// do something
}
I get bool true in the var_dump check. But I never received any email from my server.
Any ideas? Have I missed something to configure in WP?
EDIT:
The server won't send any email from yahoo accounts! Why???
$to = "xxx#yahoo.co.uk"; // works!
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: bbb#yaho.co.uk" . "\r\n" . // won't work!
"CC: lau.tiamkok#gmail.com";
mail($to,$subject,$txt,$headers);

How do I include sender's name and not just the email address while emailing message from user input form

I am trying to take inputs from a form on my website using a simple PHP script as given below:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
This worked perfectly. Well, almost. The email I receive does not include the sender's name. I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.)
So I made a few changes after looking up some threads like this:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;
if(mail($toemail, 'Subject', $message, $header)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
Email response is still same. What do I need to change ?
Try this format, note the spaces and the \r\n's, change your variables accordingly:
$email_headers = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <sender_email#yourdomain.com>' . "\r\n";
$email_headers .='Reply-To: reply_to_email#yourdomain.com' . "\r\n";
mail($recipient_address, $email_subject, $email_body, $email_headers);
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Quoted from http://php.net/manual/en/function.mail.php

Send to user a copy of contact form to his e-mail

this is related to contact form. i am trying to send a copy of email to user also.
so i thought to replace the same function so i repeated if(!isset($hasError)) second time and changing $emailTo = $email
and this is not working. i thing the problem might with the headers... can i write 2 different header tags ?
// to me
if(!isset($hasError)) {
$emailTo = 'myemail#website.com';
$subject = 'Submitted message from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
//User Copy
if(!isset($hasError)) {
$emailTo = '$email';
$subject = 'Thank you for Contacting';
$body = "Here is your copy of email which you have sent us. \n\nName: $name \n\nEmail: $email \n\nComments: $comments \n\n Thank you for your email. we will get back to your soon";
$headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$UserEmailSent = true;
}

Sending Email in php, headers problems

I trying to send email using mail() function...
here is my code:
<html>
<form action ="" method ="post">
<input type="submit" name= "email" value="email">
<form>
<?php
if (isset($_POST['email']))
{
mail("receiver#hotmail.com", "Subject: Hi", "hello" );
echo "Mail Sent";
}
?>
</html>
the code about works just fine, I can get the email but the only problem was when I check the email, the sender will be "webmaster#something.org"
I tried to change the code to:
mail("receiver#hotmail.com", "Subject: Hi","hello", "From: sender#yahoo.com" );
but it didn't work...
Could you please help me to include the name of the person who sent the email...
Thank a lot
Please try this.It is the simplest method to mail someone.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Check php mail manual.
You need to define 'From: xxx' in the header
check your form tag closed or not?
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Use [phpmailer to send the mail.
You can set the sender id and content type for the mail.

The way to send duplicate email to the user?

I made simple email in wordpress but it would only send email to the administrator email. I need to find the way to send duplicate email with Thanks to the user.
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = 'I Have A Question to Ask from '.$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone: $phone \n\nComments: $comments";
$headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
Insight is appreciated. Thank you.
looks like you already have the user's email address in $email, right? you used it in the message you showed in the question.
$subject = '(duplicate) I Have A Question to Ask from '.$name;
$body = "This message was sent to the administrator:\n\nName: $name \n\nEmail: $email \n\nPhone: $phone \n\nComments: $comments";
$headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($email, $subject, $body, $headers);
Just add:
wp_mail($email, "Thank you for your message", $body, $headers);

Categories