PHP email() function not sending message correct? - php

I'm at the final part of my code, the email verification. However, the email don't seem to be sent correctly. For the sake of spam, I'll replace my email with _EMAIL_.
$hash = genRandomString();
$link = 'http://www.website.com/verify.php?email=' . $email . '&hash=' . $hash;
$message = 'Please confirm your email address by click this following link, or copy and pasting it into your web browser.'
. $link;
mail($email, 'Account verification for website.com', $message);
All the data is going correctly into the database (No incorrect variables), but the email comes though something like this:
Please confirm your email address by click this following link, or copy and pasting it into your web browser.
_EMAIL_&hash=0uhcoawgi3qsek8l7rjoheo'>http://www.website.com/verify.php?email=_EMAIL_&hash=0uhcoawgi3qsek8l7rjoheo
I'm assuming this is something I'm doing wrong, but I can't spot it after various pieces of troubleshooting.

By default, mail sends plain text emails. If you want to send HTML emails, you have to add the appropriate header to the message. Keep in mind that not all email clients support HTML mail, and some people disable it.
With that said, if you still want to send HTML mail (lots of people do), here's how:
mail($To, $Subject, $Message, 'Content-type: text/html');
You also seem to have some issues with your message string. HTML links look like this:
link-text
So, try making your message string something like this:
$message = 'Please confirm your email address by click this following link, or copy and pasting it into your web browser. Activation Link';

Where's your PHP mail content-type? To send HTML mail, the Content-type header must be set.
<?php
// message
$hash = genRandomString();
$link = 'http://www.website.com/verify.php?email=' . $email . '&hash=' . $hash;
$message = 'Please confirm your email address by click this following link.'
. $link;
// 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($email, 'Account verification for website.com', $message, $headers);
?>
That's it. Send it again and everything should be hyperlinked.

Related

PHP Mail function adding a <br /> in the middle of my message link

For learning purposes I am trying to make a PHP email verification/signup script. Everything is working as expected except for my email message. When sending the email, I include an <a> tag with a link to the verification page. Echoing the link displays the proper link, however when I receive the email there is a <br /> in both the link and the display. The break is not in the middle of any of my variables, so I am unsure what is causing it.
My Code:
function SendVerificationEmail($email, $username, $verification) {
$to = $email;
$subject = "Email Verification";
$link = "http://localhost:80/verify.php?username=$username&validation=$verification";
echo $link;
$message = '
Hello, ' . $username . '!' . '
Thank you for signing up for Example.com!
Please click the following link to verify your email.
' . $link . '
';
$headers = "From:noreply#example.com\r\n";
mail($to, $subject, $message, $headers);
}
When I receive the email:
Screenshot of Received Email
Clicking the link then redirects me to:
http://localhost:80/verify.php?username=gray&valid=<br%20/>ation=ed265bc903a5a097f61d3ec064d96d2e
Which of course does not work, when I remove the =<br%20/> from the URL manually, everything works as expected.
Why is this break appearing in the email? It also seems to appear differently in the href and between the <a> and </a> tag.
SOLUTION:
Solved by adding Mime type to headers -
Changed headers to:
$headers = "From:noreply#example.com\r\nMIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n";

php email script for just email address

Im new to this PHP stuff so please excuse my ignorance
Im after having just one input text box in my flash website where a person just enters there email address and at the click of a button it sends an email to me to a pre defined email address with a predefined subject heading and the email address that was entered in the body of the email
Anyone know of any links or can give some help
all the ones i have found want names email subject message and so on
Any help is appreciated
Mark
EDIT
ok I have the following
In flash I have an input text converted to a movieclip called "addy". Inside the movie clip which has the inputbox which has the variable name "emailaddy"
A Button called "email"
The code i Have running when "email" is clicked is
on (release) {
form.loadVariables("email.php", "POST");
}
the email.php script is as follows
<?php
$sendTo = "mark#here.co.uk";
$subject = "Subscribe to Website";
$headers = "From: Website";
$headers .= "<" . $_POST["addy"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["addy"] . "\r\n";
$headers .= "Return-Path: " . $_POST["addy"];
$message = "Please Subscribe me to Website";
mail(recipient, subject, message, other headers);
mail($sendTo, $subject, $message, $headers);
?>
when I click the button nothing happens
what im after is when the button is clicked for and email to be sent in the following format
To: "mark#here.co.uk"
From: email address specified in text field "addy"
Subject: "Subscribe to Website";
body: "Please subscribe me to Website"
Your help is greatly appreciated
mark
The following code might help:
<?php
$user_mail=$_POST["mail"]; //or $user_mail=$_GET["mail"]; Set to your convenience!
$to_mail="abcde#xyz.com"; //Change to your email address
$message="New user's Email: ".$user_mail; //Change to your requirements
$subject="New user registered"; //Change to your preferred subject
$from="registration#yourwebsite.com"; //Change to your website mail id
mail($to_mail,$subject,$message,"From: $from\n");
To know more about the mail function, please see the documentation.
Well in depth you can do like this
<?php
if($_POST){
$userEmail = $_POST0["emailaddy"]; // textbox variable name comes here
$to = 'abc#xyz.com'; //write down here your email
$subject = 'Subscribe to website'; // your subject goes here
$message = 'Please Subscribe me to Website'; // Mail body message
$headers = 'From: ' . $userEmail . "\r\n" .
'Reply-To: ' . $userEmail . "\r\n" .
'Return-Path: ' . $userEmail; //can send x-Mailer also
mail($to, $subject, $message, $headers);
}else{
echo "Invalid Request";
return false;
}
Don't forget to check first $_POST is happened or not. No need to send Return-path instead of this use x-Mailer which sound good for other mail service providers.
To know more about this read documentation here.
If you want to connect to an SMTP server ,like Postfix or gmail there is a neat php library called PhpMailer.
It is well documentated, so you should be good to go by googleing it :)

sending mail with link and parameter in PHP

I have a strange issue with sending a mail with php, here is my code:
$link = "http://www.somelink/verify.php?id=".$uniqID;
$message ="<html><head></head><body><p>Full Name:".$data[0]." ".$data[1]."</p>";
$message .="<p>Flight Number: ".$data[4]."</p>";
$message .="<p>The Ride: ".$curPlace." to ".$data[5]."</p>";
$message .="<p>Number of Passengers: ".$data[3]."</p>";
$message .="<p>Date:".$data[6]." time:".$data[7]."</p>";
$message .="<p>The price: ".$data[8]."</p>";
$message .="<p>To verify the reservation press this link</p></body></html>";
$headers = 'From: my#email.com' . "\r\n" .'Reply-To: some#email.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Sensitivity: Personal"."\r\n";
$sent=mail($data[2],"Taxi Reservation",$message, $headers);
if($sent){
echo "Message was sent to ".$data[2];
}
else{
echo "Message was not sent";
}
The problem is with Gmail.If I send it as is, I get the success message, but get nothing in my gmail, but do get in another not gmail account.
BUT!
If I delete a tag from this line:
$message .="<p>To verify the reservation press this link</p></body></html>";
I do get the message in Gmail.So I can't understand what's the problem.
What can couse this problem?
Thank you for your help.
If your email is not arriving at Gmail, but the same code sending the same email is arriving at a different account, I suspect that the problem is that Gmail are seeing your email and classifying it as spam - have you checked to see if it's gone into your spam folder instead of your inbox?
Gmail has a set of rules to identify what they think is spam - presumably, including the link is enough to push your email into the spam category.

Can't post to facebook page by email from php

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.

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

Categories