using the PHP mail function - php

I am using the PHP mail function to send my users emails about new features and updates. I can send an email and message no problem. But I would like to control who the message is from, well more who the Email Client says it is from.
For Example:
<?php
$to = "someone#gmail.com";
$subject = "Test";
$msg = "Hello there!";
$headers = "From: someone#shareit.me";
mail($to,$subject,$msg, $headers);
?>
In the mail client (Ie. Gmail) the user will see:
Someone Test - Hello There
I would like to control the name of the sender via my headers. Is that possible?
So the email will be from someone#shareit.me but the name of the sender will be Shareit.
Thanks

Use the following headers: (link to docs)
$headers = "From: Shareit <someone#shareit.me>";

To use the From attribute inside of headers always remember to keep email addresses inside of these brackets: < and >
So now your email would be: <someone#shareit.me>
and your line would be: $headers = "From: <someone#shareit.me>"
or "From: Shareit <someone#shareit.me>"

$headers = "From: Shareit <someone#shareit.me>";
Try the above code.
Change the name from Shareit to the name of your choice.

Related

Email header not working PHP

I'm using BlueHost and I can't seem to get my email form to work. This is the PHP:
$to = "test#email.com";
$subject = "test";
$message = "test message";
$from = $_POST['cf_email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo $headers;
This doesn't seem to send the email. However, if I add ANY string in front of the email, then it works. e.g.:
$from = "zz".$_POST['cf_email'];
Can anyone tell me what I'm doing wrong? Thanks.
You cannot use any personal email ($_POST['cf_email']) in the 'From' header.
Also you have to add 'reply-to'.
Please check below thread for more info.
problem with php mail 'From' header

image in email just printing url

I have a simple php email script where I wish to include an image at the bottom. When I add the image tags like below the email just shows <img src="http://domain.com/images/logo.png" /> instead of the actual image. Any ideas why?
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$headers .= "Content-type: text/html\r\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: info#domain.com\n";
$usermessage =
"
Thank you for joining our mailing list.
We hope to see you very soon!
Address 1
Address 2
<img src=\"http://domain.com/images/logo.png\" />
";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
$fh = fopen("email.xml", "a");
fwrite($fh, "$email\r\n");
fclose($fh);
?>
You're not passing the Content-Type header with the right message. $headers does contain the right header, but it is sent with a plain text message, whereas $userheaders does not contain the Content-Type header, but the message associated with it does contain some HTML
Replace
$userheaders = "From: info#domain.com\n";
with
$userheaders = "From: info#domain.com\r\n";
$userheaders = "Content-type: text/html\r\n";
and it should work perfectly
This is a word press plugin but if you delete everything BUT the XmailBaby class it should work for you nicely.
That code is a nice piece of work that sends emails very good.
It is just a basic version but it should be enough for you.
Take a look through the code, you might find it interesting.
http://plugins.svn.wordpress.org/xmail-the-right-way/trunk/xmail.php
You need to specify html headers. Rather than do this yourself, you can use a well-established method that supports sending HTML emails, such as PHPMailer:
http://phpmailer.worxware.com/

change email address that is being used in server

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

mailed by php how to use it?

How can i use the mail by header in php?
This is what i use now:
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: test#gmail.com\r\nReply-To:test#gmail.com";
//send the email
$to = "test#gmail.com";
$subject = "This is a subject";
$body = "Hi there!";
if (mail($to, $subject, $body,$headers))
{
echo("\nMessages successfully sent!");
} else {
echo("\nMessages delivery failed...");
}
I get this on my gmail when i click on show details:
from test#gmail.com
reply-to testreply#gmail.com
to test#gmail.com
date Sat, May 14, 2011 at 12:06 AM
subject stefanos neofitidis! You commented on your poem:Tree present
mailed-by ip-1-1-1-1.ip.secureserver.net
i do not want to the ip-1-1-1-1.ip.secureserver.net to show up to the users...this is what i am trying to change!
Do you mean the X-Mailer?
$headers = "From: test#gmail.com\r\nReply-To:test#gmail.com\r\nX-Mailer: PHP/" . phpversion();
What you want to do is add a fifth parameter in your mail() function, which needs to use the "-f" sendmail option.
For example:
mail($to, $subject, $body, $headers, "-fsender#domain.com");
Sending a message with that last parameter will change the envelope sender to "sender#domain.com". Most of the time, email providers like gmail won't even show that address if it is set by hand(which is what you want, I'm assuming).
See http://us2.php.net/function.mail for more details.

PHP Mail Don't Show "To" in "To" Header

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

Categories