change email associated with php mail function - php

I'm using the php mail() function and I'd like to change where the mail is comming form, ie: from the default site email to a specific email address. I'm using Dreamhost as my hosting provider.
I've tried this:
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$comment = $_GET['comment'];
$todayis = date("l, F j, Y, g:i a") ;
$subject = "A message sent on ".$todayis." from ".$name." via the playatics website";
$message = " Message: $comment \r \n From: $name \r \n Reply to: $email";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Domain Name contact#domain.com' . "\r\n";
mail("somemail#domain.com", $subject, $message);
?>

You are a whisker away from the answer here. You are setting a variable $headers, but you are not using it when calling the mail() function.
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$comment = $_GET['comment'];
$todayis = date("l, F j, Y, g:i a") ;
$subject = "A message sent on ".$todayis." from ".$name." via the playatics website";
$message = " Message: $comment \r \n From: $name \r \n Reply to: $email";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Domain Name contact#domain.com' . "\r\n";
mail("somemail#domain.com", $subject, $message, $headers);
?>
That should do it.

You need to use your headers I think (see http://php.net/manual/en/function.mail.php)

Not directly an answer to your question, but check out PHPMailer if you plan on doing a fair bit of emailing in PHP. It makes things nice and easy :)

Related

php mail From: & Reply-to: headers issue

Ok so I am writing a php contact form that will send two emails. One to the webmaster with the info and a similar confirmation email sent to the form submitter.
The problem is with the confirmation email. I am trying to configure the From: header and the Reply-to: but and for some reason catching a hurdle.
Originally I had my php set up as so declaring each header parameter...
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$guests = $_POST['guests'];
$type = $_POST['type'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$message = $_POST['message'];
$formcontent="THIS IS A FORM SUBMISSION FROM domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message";
$comfirmcontent="THIS IS A CONFIRMATION OF YOUR FORM SUBMISSION TO domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message \n\n\n If you have any further questions please email info#mydomain.com";
$confirmsubject="Confirmation for your inquiry to mydomain.COM";
$confirmheader="From: mydomain.com" . "\r\n" .
"Reply-To: info#mydomain.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$recipient = "info#domain.com";
$subject = "Party Inquiry from Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
header('Location: party-form-thank-you.html')
This as I understand is the proper way, but I am still having the From: say "mydomain.com#myhostdomain.com as both the senders name and senders email. Which is how it was before I even declared the headers and it was null.
So then I tried an override using
mail($email, $confirmsubject, $comfirmcontent, $confirmheader,'-finfo#mydomain.com') or die("Error!");
Which returned the same results.
So I have resorted to simply using the following, w/o declared headers.
mail($email, $confirmsubject, $comfirmcontent, null,'-finfo#mydomain.com') or die("Error!");
This give me the proper from/reply-to address, but also puts it as the senders name.
So my question is if there is any way to write the headers so the email formulates as such:
Senders Name: myDomain.com
Senders Email: info#mydomain.com
I use an array for my headers, then implode them on "\r\n":
$headers = array();
$headers[] = 'Content-type: text/html; charset="UTF-8";';
$headers[] = 'Date: ' . date('r', $_SERVER['REQUEST_TIME']);
$headers[] = 'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . (empty($_SERVER['SERVER_NAME']) ? '' : '#' . $_SERVER['SERVER_NAME']) . '>';
// here is the from and reply-to part:
$headers[] = 'From: "' . $fromName . '" <' . $fromAddress . '>';
$headers[] = 'Reply-To: "' . $replyToName . '" <' . $replyToAddress . '>';
$headers[] = 'X-Mailer: PHP v' . phpversion();
if (!empty($_SERVER['SERVER_ADDR'])) {
$headers[] = 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'];
}
// Use the implode function to collapse the array into a single string
mail($to, $subject, $message, implode("\r\n", $headers));

Sending form data to my email address and address entered in form by user

Hi I asked this question a year ago but couldnt solve it and am now having another go.
link to the original question:
Send email to address from form input
I need to send the form data from my form to the specified address 'diysoakwells#hotmail.com' and also the email address from the form field which is assigned the variable $emaile. As it is the email arrives at diysoakwells#hotmail.com but not the variable address ($emaile). I cant understand why but it is definitely picking up the form data as it has all the details included in the email when I receive it at diysoakwells#hotmail.com. I basically want a copy of the order sent to the user as well as myself.
<?php
include_once("wsp_captcha.php");
if(WSP_CheckImageCode() != "OK") {
header('location:/form-rejected.php');
die();
}
$subject = 'Order Inquiry';
$jcitems = " <p><b>ORDER:</b></p><p> " . $_POST['jcitems']."<p/>" . "<p><b>Total:</b> $" . $_POST['jctotal']."</p>";
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: inquiry#DIYSoakwells.com' . "\r\n" .
'Reply-To: noreply#diysoakwells.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
$phone = $_POST['phone'];
$emaile = $_POST['emaile'];
$textbox = $_POST['textbox'];
$to = "diysoakwells#hotmail.com,$emaile";
$text = "<html><body><p>This form was submitted on Your Web Site on \n $date at\n $time</p><p><b>Message:</b>\n$textbox</p><p><b>Customers Email Address:</b> $emaile</p><p><b>Customers Name:</b> $name </p><p><b>Customers Phone Number:</b> $phone </p></html></body>";
$body = $text . $jcitems;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
?>
Please help if you can I will be monitoring this for the next few days. Open to suggestions/explanations!!!
$to = "diysoakwells#hotmail.com;" . filter_var($emaile, FILTER_VALIDATE_EMAIL);
You may also want to try this....
$to = "diysoakwells#hotmail.com;<" . filter_var($emaile, FILTER_VALIDATE_EMAIL) . ">";

Why won't my PHP mail() function send linebreaks?

Here's the script:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$body = "[From: " . $name . "]\r\n\r\n" . $message;
$headers = "From: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$success = mail($recipient, $subject, $body, $headers);
echo $success;
?>
On this page: christianselig.com/contact.html
My message will send without any linebreaks. I have it set to separate the [From: xxx] section from the message with a double linebreak, so it should look like this:
[From: John]
Hey.
But it doesn't.
If I make the message multiple lines long, it also concatenates them on one line. How do I prevent this behaviour? My code used to allow it, but I broke it somehow...
Because you're sending it with an html formatting. Try:
$body = nl2br("[From: " . $name . "]\r\n\r\n" . $message);

Send email to address from form input

I have a pretty standard contact form with inputs for name, email address and phone number. As well as sending the email to the specified email address as per a standard form,
$to = 'diysoakwells#hotmail.com';
I would also like to send the email to the user using the address from the email address input from the form. I was thinking using the post variable from the email input like this would work:
$to = 'diysoakwells#hotmail.com', $email;
but no luck. Can someone point me in the right directiona and are there any security risks in using this approach? I ultimately aim to provide the user with a checkbox that if checked sends a copy of the email to themselves.
Here is a link to my form
http://www.diysoakwells.com.au/cart.php
Thankyou in advance : )
<?php
include_once("wsp_captcha.php");
if(WSP_CheckImageCode() != "OK") {
header('location:/form-rejected.php');
die();
}
$to = 'diysoakwells#hotmail.com';
$subject = 'Order Inquiry';
$jcitems = " <p><b>ORDER:</b></p><p> " . $_POST['jcitems']."<p/>" . "<p><b>Total:</b> $" . $_POST['jctotal']."</p>";
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: inquiry#diysoakwells.com' . "\r\n" .
'Reply-To: noreply#diysoakwells.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
$phone = $_POST['phone'];
$emaile = $_POST['emaile'];
$textbox = $_POST['textbox'];
$text = "<html><body><p><b>Message:</b>\n$textbox</p><p>This form was submitted on Your Web Site on \n $date at\n $time</p><p><b>Customers Email Address:</b> $emaile</p><p><b>Customers Name:</b> $name </p><p><b>Customers Phone Number:</b> $phone </p></html> </body>";
$body = $text . $jcitems;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
?>
What your doing is not what you want to do. Concatenating two strings in PHP is done with the . not the , so the correct syntax is:
$to = 'diysoakwells#hotmail.com'.", ".$emaile;
or simply
$to = "diysoakwells#hotmail.com, $emaile";
That's assuming that the code in charge of sending the email uses php's mail() function, which allows multiple emails in the $to argument. If that doesn't work, I can't be of more use without seeing the actual code.
The 'to' field on emails accepts a string, with the email address comma-separated.
$to = 'diysoakwell#hotmail.com, ' . $emaile;
should do the trick.
You should check the email address that they provide is formatted as an email address, and it would be a good idea to have a CAPTCHA to prevent automated bots from using your form as a spamming tool.
If you use php mail() function you can send copy by specifying additional headers like that:
$headers = 'Cc: '.$emaile;
mail($to, $subject, $message, $headers);

Can't get php mail() to work

I've got Mamp running on my mac and trying to get mail() to work.
This is what I've got to work with.
$to = 'mymail#gmail.com';
$subject = 'The subject!';
$message = 'Hi there!';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$headers .= 'From: Test <test#test.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers))
{ print 'success!'; }
else
{ print 'fail!'; }
?>
It just keeps on returning false. Any idea what I'm doing wrong?
Some settings with php/apache I need to check?
if you using your snippet on localhost, put on server and then try.
php mail() function needs to be on sever if you want it to work. on localhost you always get fail!
Try this:
<?php
$Name = "Da Duder"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
ini_set('sendmail_from', 'me#domain.com');
mail($recipient, $subject, $mail_body, $header);
?>
http://be.php.net/manual/en/function.mail.php
each line of text may not be bigger than 70 chars and needs to be cut off with a LF (\n)
EDIT: as #brad suggested: SwiftMailer is realy good!

Categories