E-mail message/body section won't change - php

I have a login form when the user registers it sends out an E-mail to activate the account. The problem is that the e-mail body section is not changing, my code to send out the E-mail looks like this
$to ="$e";
$from ="jalilmotaz#gmail";
$subject = 'collegeloop activation';
$body ="hi";
$message="hi";
$headers ='From: $from';
mail($to, $message, $subject, $headers);
echo "signup_success";
exit();
when the E-mail is sent it looks like this http://imgur.com/rvxIv3g this is what the message originally was, but when I want to change it, as you see above It should say "hi" , it is not changing.
any suggestions?

Everything works as it should, really.
You changed $body variable but message is in $message. Try to change correct variable ;)

This looks like a caching problem. PHP can cache the rendered pages to speed up performance and email body is rendered as an HTML like all other pages.
If you are using APC then use the following link to clear cache and try again,
http://www.inmotionhosting.com/support/website/php-configuration/view-and-clear-php-apc-cache
If your server is on linux, then you can try the following,
find . -name "*.php.cache" | xargs rm -f

Related

Is there an e-mail template file like *.oft, which can be opened in all e-mail programs?

Currently I'm writing a web interface which is full of data. I would like to export this data as an e-mail template, so you can edit the e-mail afterwards.
Is there a format such as *.oft, which can be read by all email programs?
I know that there is such a function in HTML (<a href="mailto:[...]">). Since the e-mail text is very long and I want to attach files, this doesn't seems to be a good solution.
Can someone help me?
There's no such format as you're looking for. The internet engineers who design email protocols and format concern themselves with what goes out over the network, but not with what's internal to the machine. Email is highly interoperable when sending and receiving messages as a result, but there's no analogous standards body for internal API's. Sometimes there are de facto standards that arise from a imitating the behavior of popular piece of software, but that hasn't happened for the email feature you're looking for.
Maybe you mean .EML files? I remember that this file can be opened with nearly all e-mail clients such as Outlook, Thunderbird etc.
As far as I can tell you can go one of two ways:
1 - Use a prebuilt solution such as the one by these guys at postageapp.com their API looks pretty simple and does everything you want to achieve.
2 - More time consuming, but you can create your own templete using PHP. I would go for something along these lines. This is a very simple way of doing it. You could write your own CSS in the head and go nuts. Just remember that tables are king when it comes to HTML email :) Once the template has been written, you could just pass through the $content to fill it....
sender.php
<?php
$email = "Hello, \n";
$email.= "Very <strong>impressed</strong> with your email! \n\n";
$email.= "Faithfully\n";
$email.= "Mr Example\n";
$to = "me#me.me"; // <-- Set this as yours for testing...
$from = "someone#nice.me";
$subject = "An email";
$headers = "From: Someone nice <" . $from . ">\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= nl2br($email); // <---- if your email contains newline characters - it will convert them to <br />
$message .= '<br />www.example.com';
$message .= "</body></html>";
mail($to, $subject, $message, $headers);
// ADD LOTS OF PROTECTION IF NOT HARD CODING :)
?>
To extend this - if you for example use Gmail, then you could use Google's SMTP to send it - so that the email wouldn't say 'Sent on behalf of'.
Just create a email using HTML without CSS. Any data or binary can be passed through PHP and then emailed to the recipient.
To get you started, Create a file using fopen();. PHP has other functions that would allow you to edit, open, search&replace and even convert a file to a format of your choice. This is equivalent to a .oft file if not better, because all emails are be sent in text or a html equivalent regardless of the email client.
If you'd would like some more info on how to create this, let me know.

Sending mail on PHP using LAMP

I have been trying for awhile now to send mail using PHP, and I can't seem to get it to work.
I looked at the tutorials, and they make it seem simple enough. Yet, it seems to actually require more work than what is mentioned. Through research (to make this short), it seems that instead of the "From: " header actually having the address I give to it, it seems that... sendmail replaces it or something with daemon#ubuntu? Ok, even if I don't include a header I still get spam from my gmail account from the daemon#ubuntu thing (I am running ubuntu on a vm, and sending mail to my gmail account). I guess this could explain why I incessantly get errors when I try to send mail saying how there is an error in the "MAIL FROM command".
So before I get myself involved with messing around with sendmail, is this the (likely)culprit of the daemon#ubuntu nonsense? The tutorials make mention of having things like sendmail, but they really don't go into any details of what to do with them. I did make sure that sendmail is being used in my php configuration file.
I have heard of alternative mail servers like PHPmail or something like that. I really don't want to get involved in installing and configuring more software unless I really have to. So unless you guys really think sendmail isn't worth it, I would prefer advice on sendmail if it is the problem.
Thank you for your time.
edit: I am using the PHP mail() function on an Ubuntu vm by VMware Player.
So someone wanted to see my code. I somewhat wanted to avoid this simply because I tried many different possibilities. I will just include what I have in my file right now:
<?php
$myEmail = "#####gmail.com";
$fromAddr = $_POST['sender-email'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromAddr\r\n";
$headers .= "'X-Mailer: PHP/'" . phpversion() . "\r\n";
$message = $_POST['message'];
$status = mail($myEmail, $_POST['subj'], $message);
if($status) {
echo "Your message has been sent, " .$fromAddr. "!";
}
else {
echo "An error occurred while trying to send the mail.";
}
?>
The MIME type thing, and Content-type thing I added later on. I have tried this without them. I have also tried sending mail without using any headers at all, and still apparently get the daemon#ubuntu thing. Also, I edited out my e-mail address to post the code on here. Ok I guess I should add this too: I am getting the data from an HTML form from another page. On submit, I come to this php page where it grabs that data and then sends the email (this isn't for anything professional, I am just doing this to learn more about web stuff).
Postfix is a very easy to use and install email server, in my experience. You need an email server installed and configured before you're able to send email on your localhost.

PHP Content form will not submit email and shows php code in browser

I used this tutorial as a guide to create a simple contact form. I am new to using php and this seemed easy enough to figure out, but there seems to be some bugs in the code. After implementing the code on my site, the submit button brings me to a page displaying all of my php code in the browser and no email is sent.
$email_to = 'skustrimovic#gmail.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e {}})();
/* ]]> */
</script>'; //the address to which the email will be sent
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/*the $header variable is for the additional headers in the mail function,
we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
That way when we want to reply the email gmail(or yahoo or hotmail...) will know
who are we replying to. */
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or this one to tell it that it wasn't sent
}
Should I just try to create my own PHP to work the existing HTML/jquery?
Any help would be much appreciated.
The send_email.php script on that tutorial site doesn't have a <?php opening . . . that might be your issue?
Alternatively, you'll want to make sure that basic PHP code can run on your server, e.g. a simple helloworld.php:
<?php
echo "hello world"
?>
If not, you'll need to troubleshoot as to why PHP is not running - e.g. is the webserver running, is php installed, etc.

Odd behaviour with links in PHP Mail function

I'm currently trying to get links working in emails sent via the PHP mail function. Here is my code - I've also included some things I've tried (commented out along with notes):
$to = "test#testing.com";
$subject = "Testing email";
//$body = '<strong>This is strong text</strong>'; <-- Works
//and the text is correctly emphasised.
//$body = 'Link Test'; <-- Works
//but without http:// at the start makes the link relative to the server root
//$body = "<a href='http://www.yahoo.com'>Link Test</a>"; <-- Does not work
//$body = "Link Test"; <-- Does not work
$body = 'Link Test'; //<-- Does not work
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Steven Parler <". $to . ">\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
As can be seen html is working without links, and links also work providing I don't include "http://" in the link itself. If I do include the "http://" then no email is received / sent at all (I'm unsure which as the mail() command returns true to say it was sent).
I'm not really sure why this isn't working. Could it be some setting that needs changing on my webhost's server? I'm on windows shared hosting.
Thanks in advance for any advice anyone can give me - been pulling my hair out over this lol. :)
Don't build MIME messages by hand. It's just too painful and fragile. Use something like PHPMailer or SwiftMailer to do it for your automatically. You provide the HTML, they'll provide the appropriate headers
I completely agree with #Marc B.
Here are a couple more options: XPertMailer, Zend_Mail, Rmail, HTML MIME MAIL
I've worked with all of these but since I'm working mostly with the Zend Framework I'm lately using Zend_Mail.
Having said that, your hosting provider might be blocking your emails because they might think it's SPAM. Try generating valid html markup that passes validation and see if that helps.
That's very odd behaviour, I'd expect the last 2 to work perfectly. I tried this out on my server, and mail($email, $subject, $body, $headers); worked perfectly with a $body of
text text text: \n http://website.com.
Perhaps it's a setting somewhere? If you're using awardspace, they require a from header to be used, and the mail sent from an e-mail registered with them. Other hosts might have a similar procedure.
Thanks for all the responses. I finally got around it by using the SMTP feature of Swiftmailer which correctly created the email with links.
Out of interest I tried the code I used in my original post again... but this time I used a random web address (http://www.trustedreviews.com). The email arrived... I then tried a lot of other web addresses - google.com, hotmail, yahoo.co.uk etc and they all arrived. Changed it back to (http://www.yahoo.com) and lo and behold the message did not arrive again. So it seems out of the millions of web URL's there are I chose the one that my webhost has decided to block lol...
That said the yahoo link does arrive ok using the smtp function of swift mail; it's only with the php mail function it doesn't seem to work. I guess I'll contact them and ask why it's blocked. I'll most likely stick with using the smtp method as at least it seems like it bypasses any restrictions.
Thanks again for everyone's time and help :)

PHP Mail will not process ".com"

I know this is weird, but when a ".com" is added to the subject or body text to send PHP mail (like - xyz.com), the mail will not send. It's a problem with this hosting company (ipage) but i want to see if their is something i can do in the script itself, because it's a CMS of mines and i would like to keep generalized for everyone who uses it.
script is simple and as follows:
function confirmEmail($username, $email, $confirm_code) {
$sitename = str_replace(".com", " .com", site_name);
$subject = "Your confirmation link here";
$headers .= 'To: '.$username.' <'.$email.'>' . "\r\n";
$headers .= "From: ".$sitename." <".support_email.">". "\r\n";
$message = "Your Comfirmation link \n
Click on this link to activate your account \n
".root."/confirm.php?confirm=$confirm_code";
$message = wordwrap($message, 70);
$sendmail = mail($username,$subject,$message,$headers);
if ( $sendmail ) {
return true;
} else {
return false;
}
}
Now as you can see, i replace the ".com" with " .com"(note the space at the beginning) for the $sitename variable, and that works but the problem is the "root" contains the full path to the givin site so the link is like "xyz.com/confirm.php?confirm=$confirm_code". I can't allow a space before the '.com' in the site path like
"xyz .com/etc..." because the obviously the user can't click the link from within their email.
I know this is a weird problem and I have no clue on what's going on or what to even ask the hosting to change.
*note: site_name and root are defined previously in another page, and they work fine. Also no errors are being returned.
Any ideas out there?
Your code is correct. Your workaround should not be necessary.
This is very likely a provider issue.
Don't know about your environment, but if you have not full control of your mailserver, try sending a mail with a link using telnet so you really see everything. Often spam filters etc are badly configured and don't return the correct codes.
If you don't have any luck with that I recommend switching to an external SMTP server that is well configured. You can perfectly use gmail for that if you dont send tons of mails.

Categories