php mail() headers problem - php

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 :)

Related

$_SERVER['SERVER_SOFTWARE'] doesn't work on command line

I try to run a PHP script from command line to check if a specific webserver is running, but it doesn't work.
This one works:
$to = 'mail#address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail#address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';
mail($to, $subject, $message, $headers);
This one doesn't work:
$to = 'mail#address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail#address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';
if (strpos($_SERVER[$serverSoftware], $isWebserver) !== false) {
mail($to, $subject, $message, $headers);
}
strpos($_SERVER[$serverSoftware] doesn't work, but why?

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.

How do I change the sender name of a PHP email?

I have a script that sends an email, but instead of the sender name being "admin#foo.bar", I want it to say "Foobar Admin". How can I do this?
Thanks!
Do you want something like this??
$email_from = $full_name.'<'.$email_from.'>';
try this
<?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 reference use http://www.php.net/manual/en/function.mail.php
Use this;
<?php
$to = 'to#mail.com';
$subject = 'Your subject';
$message = 'Your message';
$headers = 'From: Foobar Admin<admin#foo.bar>' . "\r\n" .
'Reply-To: admin#foo.bar' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
mail("recipient#aol.com","subject","message","From:Foobar Admin<foobar#admin.com>");

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 function error, mail sent from wrong address

I would like to sent e-mail to registered users with the following code:
$to = $ownerMail;
$subject = 'SGKM - Online Ticket';
$message = 'SGKM - Online Ticket';
$headers = 'From: sgkm#ku.edu.tr' . "\r\n" .
'Reply-To: sgkm#ku.edu.tr' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
but unfortunately, in mail: "from sgkm#ku.edu.tr via venus.nswebhost.com" so, I still see venus.nswebhost.com in sender's mail part. Can't I delete that ?
What should I do ?
Thanks
You need to use the 'additional_parameters' flag in the mail() call to specify an "envelope".
$sent = mail($to, $subject, $message, $headers, "-f webmaster#example.com");
Unless I'm mistaken, you're not using the $headers variable in your mail() function.
From: http://php.net/manual/en/function.mail.php
<?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);
?>
mail($to, $subject, $message, $headers);
You forgot to use the $headers variable you've set up! Try:
$sent = mail($to, $subject, $message, $headers);

Categories