SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    mail isn't working!

    I have done a couple of mail stuff on some of my pages, but now I tried it on a new page for the company I'm working in. But this time it seems like it isn't sending e-mail like it's supposed to do.
    Since I'm also included in the send list (as a fake user) I should get a message as well, but now when I tried to send this I didn't recieve a message, so I guess the other people didn't get one either.

    I sent one last week and it seems like some of them got it and some didn't.
    Hmmm, can anyone see what might be the error in this code?
    Might look a bit weird to you real PHP code people, since I'm working in Dreamweaver.

    PHP Code:
    <body>
    <p><a href="index2.php">BACK TO MENU</a></p>
    <p><?php echo $row_rs_mail['title']; ?><br />
      <?php echo $row_rs_mail['text']; ?></p>
    <p>

    <?php do { ?>
      <?php echo $row_rs_kunder['name']; ?> <?php echo $row_rs_kunder['lastname']; ?><?php echo $row_rs_kunder['company']; ?>
    <br />
    <?php

    //headers for the email
    $from_name "TheCompany";
    $from_email "info@company.com";
    $headers "From: \"".$from_name."\" <".$from_email.">\n"
    $headers .= "Return-Path: <".$from_email.">\n"
    $headers .= "MIME-Version: 1.0\n"
    $headers .= "Content-Type: text/HTML; charset=UTF-8\n";

    $to $row_rs_kunder['email'];
    $subject $row_rs_mail['title'];
    $message $row_rs_mail['text'].' '."\n\n".' '.'<br /><br />This is your username:'.' '.$row_rs_kunder['email'].' '.'<br />Your personal code is:'.' '.$row_rs_kunder['code'].' '.'<br /><br />This is the place: www.company.com/codepage<br /><br />';
    mail($to$subject$message$headers);
    ?>

    <?php echo $to?><br />
    <?php echo $subject?><br />
    <?php echo $message?>

    <?php
    $customer 
    $row_rs_kunder['customer_id'];
    $sql "UPDATE table SET sent='$sent' WHERE customer_id ='$customer'"
    mysql_query($sql) or die(mysql_error());

    ?>
    <?php 
    } while ($row_rs_kunder mysql_fetch_assoc($rs_kunder)); ?>
    </p>
    </body>
    Oh, the last thing is where I set the table to the SENT number that is in the URL, so I know what message number has been sent.
    If something goes wrong I can always try to send the message again and only the people who haven't been updated with the message id will get the message again.
    But, since people don't seem to get this anyway, I'm very worried...
    The same kind of code is working on other pages I've done, but maybe I've done something wrong here that I just can't see....

    Please help!!!
    Last edited by brad62; Jun 22, 2009 at 07:07. Reason: Added the last info after the code. To explain a part.

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Was it a Hotmail account ?

    I think your mail is being sent fine, but being blocked by the mail reader.

    Mail is a tricky thing. I've found that I had to be sure to do many things precisely (skip the line after the headers, leave no spaces after each part) for the mail to not get blocked. And your mail can be blocked outright too... as in, not even shown in the junk box.

    Very frustrating, so for testing I use a webmail account which I know has no filters at all. Then, once I know the mail is being sent, I start branching off to test in Hotmail, G-Mail, Yahoo. Then my webmail, Outlook and Thunderbird.

    Another thing to look at that may be getting your mail blocked... be sure you have all your To: , From: , ReplyTo: and ReturnPath: headers, and that these are all in the Name <address> format.

    Echo the PHP and look it through... something may be off that you're not picking up while staring at your un-parsed code.

    Be sure that you broke your lines in the right places.

    And for G-Mail, be sure that your closing multipart boundary is correct.

    And most importantly...

    TEST IN ALL PLATFORMS YOU CAN !!




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd just use a Mime Mail class, as it handles the specifics of Mime for you.

    eg:
    http://pear.php.net/package/Mail_Mime
    http://swiftmailer.org/

    Some people are going to the point of just using the Google (Mail transfer agent) MTA to transfer their email. ie: point your MX entries to the Google MTAs.

    If you run your own mail server, there are so many rules to follow doing it as a hobby will almost end up in getting blacklisted.

    If you're on a shared server, you run the risk of getting blacklisted because someone else decides to send spam, or a spammer gets into the mail server somehow. (badly written scripts with mail header injections, badly set up MTA's etc.)

    So the best bet is either getting a dedicated mail server, or just use a free service like gmail to route your email. I hate the idea of giving them your emails, but what works works...
    Fiji Web Design - Enterprise Web Design

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to check your mail log on your server shortly after you send the email. If your email was rejected by the receipient it will come back as bounced and your will see it in mail log. Also any other problems will be in the mail log
    The location of mail log depends on your server and mail program. On Linux its usually in /var/log/maillog
    And of cause the logging must be enabled for your email program. This also depends on your server and email program.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sharedlog.com View Post
    You need to check your mail log on your server shortly after you send the email. If your email was rejected by the receipient it will come back as bounced and your will see it in mail log. Also any other problems will be in the mail log
    The location of mail log depends on your server and mail program. On Linux its usually in /var/log/maillog
    And of cause the logging must be enabled for your email program. This also depends on your server and email program.
    My site is on a web hosting server and I'm not sure they let me see all of that. I only have my folder where I put my stuff. Then I also have access to a phpmyadmin page.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Some hosting packages (mine anyway) won't allow you to send email unless you use the following code:

    PHP Code:
    mail ($to$title$body$headers"-f".$_REQUEST['email']); 
    $_REQUEST['email'] is from the form input <input type="text" name="email" />.

    Cheers,

    Jon

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jonpugh114 View Post
    Hello,

    Some hosting packages (mine anyway) won't allow you to send email unless you use the following code:

    PHP Code:
    mail ($to$title$body$headers"-f".$_REQUEST['email']); 
    $_REQUEST['email'] is from the form input <input type="text" name="email" />.

    Cheers,

    Jon
    I'd make sure to use escapeshellarg() on $_REQUEST['email']. As it is any option can be added to the end of it. I'm sure PHP will escape any commands, so you can't do something like:

    Code:
    user@example.com | /usr/bin/php home/badboy/myshellscript.php
    or similar. But it still is risky to do that.

    Same goes for the $headers and escaping any \r and \n.
    Fiji Web Design - Enterprise Web Design

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by digital-ether View Post
    I'd make sure to use escapeshellarg() on $_REQUEST['email']. As it is any option can be added to the end of it. I'm sure PHP will escape any commands, so you can't do something like:

    Code:
    user@example.com | /usr/bin/php home/badboy/myshellscript.php
    or similar. But it still is risky to do that.

    Same goes for the $headers and escaping any \r and \n.
    Hmmmm, I didn't understand these two last posts.
    What are they supposed to do and what are they doing? And how?

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brad62 View Post
    Hmmmm, I didn't understand these two last posts.
    What are they supposed to do and what are they doing? And how?
    If your PHP is configured to send mail through the sendmail program (or anything that looks like sendmail), which is likely is (it could also be configured to use SMTP directly), then you can use the code supplied:

    Code:
    mail ($to, $title, $body, $headers, "-f".$_REQUEST['email']);
    note the "-f".$_REQUEST['email'] added as a 5th parameter.

    This parameter passes directly to the sendmail program as an option.

    See the list of sendmail options: http://www.courier-mta.org/sendmail.html

    The -f option allows you to explicitly set the sender of the email.

    This is the same as doing "from:sender@domain.com" in the headers, only it overrides it, if I am correct.

    This is what the post by jonpugh114 is proposing as a possible solution to your problem.

    I was just pointing out that the problem with passing a variable from HTTP ie: $_REQUEST['email'] to the sendmail program, even as an option - is that it can do things you do not intend. Just like not escaping variables in SQL queries, or HTML entities in HTML etc.
    Fiji Web Design - Enterprise Web Design

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I will try this. Thanks a lot for all your help here. I kinda love this place.

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I take it that the best thing to do is test $_request['email'] with a regular expression and convert it to a variable, and use that in mail()?

    Cheers,

    Jon

  12. #12
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jonpugh114 View Post
    So I take it that the best thing to do is test $_request['email'] with a regular expression and convert it to a variable, and use that in mail()?

    Cheers,

    Jon
    Depends on where you're using it. If you are going to pass it to the shell (as in the sendmail option) then you should use:

    Code:
    escapeshellarg($_REQUEST['email'])
    If you're passing it as a mail header then you should make sure to remove mime header delimiters \r and \n.

    Code:
    str_replace(array("\r", "\n"), '', $_REQUEST['email']);
    ps: str_replace() or strpos() etc. are multitudes faster then preg_replace(), preg_match etc. so for simplistic tasks it is better to use the string functions instead of pattern matching.
    Fiji Web Design - Enterprise Web Design


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •