What is wrong with my PHP Send Mail Function below? - php

What is wrong with my PHP Send Mail function? I am hosting this script on a live server.
$to = 'myemail#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: exampleemail#gmail.com' . "\r\n" .
'Reply-To: exampleemail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){}
else{
echo "Email sending failed";
}
I keep receiving the "Email sending Failed" message.

Related

Using die() after mail() causes email not to send

I have the code below and the redirect works. However when I have the die() command at the bottom the email doesn't get sent. The email goes fine without the die() command.
Is there a way I can stop the php script continuing without stopping the email from working?
$to = 'hello#nospam.com';
$subject = "Test Subject";
$message = "Test Message";
$headers = 'From: robocop#nospam.com' . "\r\n" .
'Reply-To: robocop#nosopam.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location:http://nospam.com/home/bouncer.php');
die();
Try this for testing. I don't think that there are really such problem.
$to = 'hello#nospam.com';
$subject = "Test Subject";
$message = "Test Message";
$headers = 'From: robocop#nospam.com' . "\r\n" .
'Reply-To: robocop#nosopam.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
header('Location:http://nospam.com/home/bouncer.php');
} else {
echo "Fail";
}
die(); // and use for also exit(); for testing
That's very interesting if it is real problem.

php: Can't send email

I've made a website with a mailing form, and the php to send it is as follows:
<?php
$to = "example#outlook.com";
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
It won't send anything.
Can anyone tell what's wrong in the script?
If it helps on anything, I'm trying to send to an Outlook email.
Thanks

Sending Email in PHP from a button

Let me start by saying I do not have a lot of programming knowledge. I use a program called PHPRunner to generate most of the php that I do. It is mainly for small office stuff nothing fancy.
My question is:
I have a table that has 6 fields one of them called email. When I go into a record I wanted to put a button called "send notification" that would email the record details to the email address associated with that record.
Any help would be greatly appreciated. I know it is probably out here on the web, possibly searching the wrong way.
You can use the mail function in php. Example from the provided link:
<?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);
?>
For sending email using PHP, use mail() function
http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$from = 'webmaster#example.com';
$headers = 'From: '.$from . "\r\n" .
'Reply-To: '.$from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Send mails with PHP change From Address?

When I send a message with PHP the recipients gets a from address such as this one:
user123#p3nlhg147.shr.prod.phx3.secureserver.net
How can I use my custom email address like info#mydomain.com as sender address?
Part of the mail function is the ability to include headers.
You should send a From header, like so:
$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);
source: http://php.net/manual/en/function.mail.php
If you mean how to change the field From, use this snippet from the official doc:
<?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);
?>

php mail() headers problem

here is my code....
$subject = "This is Subject";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$to = 'foo#foo.com';
$body = 'Mail Content Here';
mail($to, $subject, $body, $headers);
but when i open this file it sends a mail to $to successfully but with wrong headers....and my hosting server default address i.e mars.myhosting.com, instead of mydomain#domain.com how can i fix that
Look at this from php.net
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Webmaster <webmaster#example.com>' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Add the from header
Here is what I would do via PHP:
<?PHP
$to = 'email#address.com';
$subject = 'desired subject';
$message = 'desired message';
$headers = 'From: example#email.com' . "\r\n" .
'Reply-To: example#email.com' . "\r\n" .
'Return-Path: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I hope that helps some :)

Categories