i'm setting up a system that whenever a user registers on my site it will automatically send a confirmation email.
My problem is that the email that is using ends with info#buick.websitewelcome.com which is weird because my website is not buick.websitewelcome.com.
My expected email should be info#myaddress.info.
How would I do that, i'm using CPanel FYI.
This is also my code when sending the confirmation address
function SendUserEmailVerificationCode( $code,$username,$email_address ) {
// multiple recipients
$to = $email_address;
// subject
$subject = 'Verification Code';
// message
$message = "
Dear ".$username."<br><br>
You received this email because you registered at http://myaddress.info/.<br>
To complete registration please enter this registration code<h1>".$code."</h1></strong> on the link below:<br>
http://myaddress.info/verifyMe.php?username=".$username."
";
$headers = 'From: info#myaddress.info' . "\r\n";
// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
}
Any help would be greatly appreciated and rewarded!
Try the command line paramter -f as shown on http://php.net/manual/en/function.mail.php#example-3048
<?php
mail('nobody#example.com', 'the subject', 'the message', null,
'-fwebmaster#example.com');
?>
The fourth argument in the mail() function is used for headers as you have already noticed. Just add a From header. This will change the from field.
$headers .= 'From: info#mywebaddress.com'."\r\n";
Additionally, if you also want to change the envelope mail from (which you probably want), you can use the fifth argument. This is used for options that should be passed directly to sendmail. Here, you should add -f info#mywebaddress.com. A simple example is shown below.
mail('recipient#domain.com', 'Subject', 'Message',
'From: info#myaddress.info','-f info#myaddress.info');
And also, all of this is mentioned in the official PHP manual on mail().
-f will set the From address, -r will override the default Return-path that sendmail generates (typically the From address gets used).
Ref: PHP Manual: mail
Related
Everything with my PHP code is working properly, the only thing is, is that when I received an email from the contact form, the "from" for my website is #p3plcpnl0547.prod.phx3.secureserver.net.
Example Image of email from contact form
How can I change the #p3plcpnl0547.prod.phx3.secureserver.net?
<?php
if(isset($_POST['send_email'])) {
//collect the form values
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
// set the email properties
$to = 'alicia.angner#yahoo.com';
$subject = "Contact Form Submission";
$from = $email ;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from";
$headers = "$name \n $phone \n $email";
//attempt to send the mail, catch errors if they occur
try {
mail($to,$subject,$headers,$message);
$msg = "<strong>Your mail was sent successfully!</strong>";
}catch(Exception $e) {
$msg = "An Exception was thrown: ".$e -> getMessage()."<br>";
}
}
?>
You are overwriting the $headers variable on each line, you instead want to append to it. Replace:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from";
$headers = "$name \n $phone \n $email";
With:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
$headers .= "$name \n $phone \n $email";
I'm not too sure what that last line is accomplishing, should it be in the $message instead?
You can also edit the sendmail_from specified in php.ini to set a defaut from parameter.
source: http://php.net/manual/en/function.mail.php.
While your immediate problem has been pointed out by other answers, noting that you're overwriting the value of the variable $headers, resulting in your headers not being set properly, there is also additional information you need to consider when sending emails from PHP this way.
PHP's mail() function typically just talks to your sendmail binary (on *nix systems), which could be an actual MTA or it could just be some script that talks to an email server like postfix, for example.
The reason this is important is because an email can have both a Sender header as well as a From header, and they can be different. Typically the MTA is responsible for setting the Sender header, which if you care about setting this information properly in the email header is important.
You can typically change the sender in your sendmail config from /etc/mail/access or /etc/sendmail/access where you can specify which users are allowed to send from which email addresses. These rules may vary depending on which MTA you're relying on and emails from unauthorized senders may be rejected by your MTA or even the recepient as this could trigger warning headers in the email. So be sure to take that under consideration.
Future Considerations for Email
For learning purposes this is fine, but if you intend to deal with email in production I always advise against using mail() in PHP setting up, configuring, and maintaining your own email server can be a rather arduous process.
Instead consider relying on an email service like AWS' SES or Sendgrid. They're more reliable, less likely to have your emails wind up in spam folders, and a lot less likely to get your IP addresses on blacklists for spam. They also have well documented APIs and are more suitable for sending out bulk emails without typing up your own server or PHP resources.
Well I am using the PHP MAIL function and for some reason every email it sends has a weird;
This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";
My website runs the following code: (I BCC myself so that I have a copy of all the emails that my website sends out)
//prepare email headers
$headers = "From: " . "info#mysite.com" . "\r\n";
$headers .= "Reply-To: ". "info#mysite.com" . "\r\n";
$headers .= 'Bcc: sent#mysite.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = getMsg( ... );
mail( $buyer_email, 'mysite.com - Verify your information.', $message, $headers );
$message = getMsg( ... );
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers );
The emails get sent out perfectly fine. The problem is with the second email that gets BCC'ed to me. The recipient's email address is blank so I can't see who the email was sent to. The first email that's BCC'ed to me is fine, all the info shows up. In other words, I can see $buyer_email, but I can't see $seller_email. Any ideas?
You can debug it like this
echo "Seller Email: $seller_email";
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers )
The page will print the seller email and you can see what it actually is.
Addition
If you can not use the above code because you have to test it a user (which is normal btw), use the following technique.
Since you are getting the first email, send $seller_email as part of test code in that email and see what value it has.
$message = getMsg( ... );
mail( $buyer_email, "mysite.com - Test Seller Email: $seller_email .", $message, $headers );
You will find out the seller email value in the email you get.
Does sending additional headers help? (see mail()) That way you don't have to use 2 mail functions.
Like this:
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
I'm very sorry, but I made a huge mistake. Long story, but basically I was confusing code on one page with very similar code on a different page. I was testing the validity of $seller_email on the wrong page. On the page in question, it was in fact NOT being set. Once again, sorry. I should have posted the entire code.
you can send a status update to a facebook page you own by sending it to a cretin (secret) email address. this is described here:
http://www.facebook.com/help/pages/mobile
the problem is I cant make it work from php
function page_publish_by_mail($page_mail, $status){
$to = $page_mail;
$subject = $status;
$message = "";
$headers = "From: my#mail.address";
return mail($to, $subject, $message, $headers);
}
I can send mail to my email address and I can post by mail from my email address but I can't seem to post by mail from PHP.
I haven't tried to send to facebook mail before, however I feel like it is being filtered out due to lack of header information. Try adding some more header details.
I always send headers like this:
$headers = 'From: Your Name <youremail#domain.com>' . "\r\n";
$headers .= 'Content-type: text/html' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
This is set up to send an html email, but you might try other content-types if that doesn't work.
It may be a good idea to look in the documentation and see if there are other headers that are required as well.
I want to make an email forwarder similar to cPanel's, where I have a database of email addresses, and where they should forward to, and I set a catch-all to pipe to my script.
I have completed this script, however, I would like to make the "To:" field in the header show the address it was sent to, rather than who is was being delivered to. For example, the email was sent to user001#mydomain.com, and the script forwards it to me#gmail.com. How can I make PHP send mail to me#gmail.com, but still show user001#mydomain.com in the headers, like cPanel does?
You can use the headers of the mail function:
$to = 'me#gmail.com';
$subject = 'Testing';
$message = 'This is a test';
$headers .= 'To: User001 <user001#mydomain.com>, User002 <user002#mydomain.com>' . "\r\n";
$headers .= 'From: My Email Script <me#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);