This question already has answers here:
How to change envelope from address using PHP mail?
(6 answers)
Closed 8 years ago.
$to = "me#mydomain.com";
$subject = "Auction Registration Confirmation";
$from = "From: donot-reply#mydomain.com";
$body = "Test Message";
if (mail($to, $subject, $body, $from)) {
echo("<b>Message sent</b>");
header( "Location: http://www.mydomain.com/thankyou.html" );
} else {
echo("<b>Message failed</b>");
}
Now the problem is that when an email is sent, the from address is not what I require, but the server login name.
Any ideas as to replace the server login name with the from email id.
YOu could try
mail($to, $subject, $body, $from, "-f $from");
this works in some configurations, really depending on the server setup.
Otherwise, edit your MTA settings as recommended above, or skip mail() altogether and use a class like PHPmailer that connects directly to the SMTP server.
There doesn't look to be anything wrong with your PHP code - I have very similar code doing the same job and it works fine.
Possibly there's something misconfigured with your email setup?
The PHP mail function relies on the MTA running on your server. I would bet that your MTA is setup to force the $from variable to your login name.
did you try this? it works for me fine
<?php
$to = "email#to.com";
$subject = "subject";
$message = "message";
$from = "email#from.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Related
I have a few questions regarding sending email in PHP. I've been on google for the last few days and I'm still having trouble getting this to fully work.
My first question is how do I change the "From" section of my email? I have "To: support#mydomain.com" in my "from" section:
I'd like to have just the proper name of my domain (eg: "testingstuff.com" -> "Testing Stuff"). How could I achieve this?
Once I actually open the email everything in it is fine and correct, including the From email address being "support#mydomain.com".
Also my mail won't send to gmail addresses. It shows up in my mail queue and my logs say it is sent but it never is received on my gmail. Do I have to take extra steps for Google to accept my email? If so what are those? Do other major mail provides require the same steps, or are they different steps?
This is my code so far:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("sendmail_from", "support#mydomain.com");
class email {
public static function send($to, $subject, $message) {
$headers = "From: Testing Stuff <support#mydomaincom>\r\n";
$headers .= "Reply-To: support#mydomain.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
}
}
?>
Usage:
require_once("../mail.php");
email::send("support#mydomaincom", "testing email subject", "testing email body");
Am I doing anything wrong in my code?
You need to check if the email is sent properly checking the mail() result, in this way:
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
this is inside your static function,
Also check your spam folder if the mail function return "success".
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps
I'm sending out activation code for user sign ups but both Mail() and Pear Mail are literally visiting the links that are placed in the body when it is sent. I've never seen this before and I'm assuming it's a PHP setting turned on that's need to be disabled.
Below is a sample, if you visit test.php it sends an email, however PHP is also reading the link in the body and visits testme.php. That file then sends an email to me as well. I get 2 emails visiting just test.php. It's impossible to create an activation link right now because I keep being activated before receiving an email.
This took me literally 6 hours to realize what was happening and I'm fed up.
Code as a basic test:
test.php:
<?php
$to = 'xxxxxxx';
$subject = "From Test.php";
$body = "Thank you for registering.\n\n To activate your account, please click on this link:\n\n http://xxxxx.com/testme.php\n\n Thanks\n";
$additionalheaders = "From: <XXXXXXX>\r\n";
$additionalheaders .= "Reply-To: XXXXXXX";
mail($to, $subject, $body, $additionalheaders);
echo "done";
?>
testme.php:
<?php
$to = 'xxxxxxx';
$subject = "From Test Me";
$body = "You shouldn't receive this";
$additionalheaders = "From: <xxxxxxx>\r\n";
$additionalheaders .= "Reply-To: xxxxxxx";
mail($to, $subject, $body, $additionalheaders);
?>
Any ideas what would be causing it? Server is running PHP Version 5.4.45
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
<?php
$su="";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$to='jikkas#gmail.com';
$subject = 'the subject';
$message = 'Subject: '.$subject.'</br></br></br>'.$message;
$headers = 'Name: '.$name.'</br>'.'Email: '.$email;
$send= mail($to, $subject, $message, $headers);
if ($send)
{
$su="Thanks for being with us...";
}
}
?>
This my email function. But it's not working. My site is hosted on ipage server.
For starters, your code needs tidying up, so I have done this for you. Also, what do you mean it's not working? You won't know if it is working because you haven't told it to tell you. You have just set the variable $su and not done anything with it. Here is what your code should look like:
<?php
$su="";
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'jikkas#gmail.com';
$subject = 'the subject';
$message = 'Subject: '.$subject.'</br></br></br>'.$message;
$headers = 'Name: '.$name.'</br>'.'Email: '.$email;
$send = mail($to, $subject, $message, $headers);
if($send)
{
$su = "Thanks for being with us...";
echo $su;
}
else
{
$su = "Error: unable to send";
echo $su;
//See note below
}
}
?>
I have included an else statement in there just as an example if you want to use your own error handling function. However, error_reporting should ideally be set to E_ALL to ensure you are capturing any errors that may be occurring. Also, ensure you have the correct mail settings in your php.ini. Look here for more details on mail settings in php.ini.
I would recommend running the following script:
<?php
// Set error reporting level to all errors
error_reporting(E_ALL);
// View php.ini and look for the mail settings
phpinfo();
?>
Hope this helps!
I think you had to create a Mail Account in the Panel.
I have Plesk with a Network Solutions virtual server. When my site sends out emails using PHP, the email header indicates that the message is coming from an address like this one:
anonymous#002a1c9.netsolvps.com
How do I change this address?
Using the PHP mail() command, you can set the envelope from address using the -f command, and you can include the from address in the message headers as well, like so:
$to = "to#to.com";
$from = "from#from.com";
$subject = "subject";
$message = "this is the message body";
$headers = "From: $from";
$ok = #mail($to, $subject, $message, $headers, "-f " . $from);