April 20, 2012

Prevent links from auto hyperlinking in Outlook etc using PHP

Question by Ben Carey

I know that this can be done using settings in Outlook, but that only sorts the issue for myself.

What I would like to do is use PHP to prevent text from being hyperlinked just because there is an @ sign etc…

As far as I can see, the only option for me is to encode all @ signs to their HTML numeric entity like so:

Something like this:

$message = str_replace('@','@',$message);

However, if possible, I do not want this to happen if the @ sign is part of an email address.

Therefore I need something like this:

// SOME_REGEX will match any @ sign that is NOT part of an email address
$message = preg_replace('SOME_REGEX','@',$message);

Can anybody think of any other better methods? Are there any flaws in this plan? Can anyone suggest a good regular expression for this? I am struggling to write a regex that matches an @ sign if it is not part of an email address

Thanks in advance

Answer by Anthony

This will not work if the email address is wrapped in anything not defined in the trim list.

$chunked_message = explode(" ", $message);

foreach($chunked_message as $chunk) {
    $clean_chunked_message[] = 
               (!filter_var(trim($chunk, " -().?!trn", FILTER_VALIDATE_EMAIL)) 
               ? str_replace('@', '@' $chunk) : $chunk;
}

$clean_message = implode(" ", $clean_chunked_message);

Good luck.

Answer by Starx

This is a feature of mail application to detect link when it is found and make it click able.

A dirty trick to escape this situation is to use space in between the links.

Example:

http://ww w.you tube.com/v=.......
April 7, 2012

PHP mail() function goes to junk @ Outlook

Question by Robinjoeh

When using the PHP mail() function, the email goes to the junk folder when I use the word “utvecklat” somewhere in my message. (it doesn’t matter where)

But when I’m sending an email from Outlook to another Outlook-account (within the same mail server) it goes to the inbox.

Therefore, I think the problem is related to the mail headers.

I’m using this PHP code:

$subject='=?UTF-8?B?'.base64_encode($subject).'?=';
$emailTo="{$toEmail}";

$boundary = md5(date('U'));

$headers = "MIME-Version: 1.0rn";
$headers .= "Content-Type: multipart/alternative;boundary={$boundary}rn";
$headers .= "From: "{$fromName}" <{$fromEmail}>rn";

$message = "This is a MIME encoded message."; 

$message .= "rnrn--" . $boundary . "rn";
$message .= "Content-type: text/html;charset=utf-8rnrn";
$message .= $message_tmp;

$message .= "rnrn--" . $boundary . "--";

mail($emailTo, $subject, $message, $headers, "-f {$fromEmail}");

The email should be in HTML format. It doesn’t matter if I use “utvecklat“, the email will go to the junk folder anyway.

So, what should I do to avoid the spam filter in Outlook? (must be something to do with my email headers, because mail from Outlook to Outlook within the same mailserver goes to the inbox.)

Thank you!

UPDATE!

Here you have a email sent from a outlook account to another. Can someone “convert” all this headers to a correct PHP mail() code? Thanks!

Return-Path: <example@domain.com>
Delivered-To: robin@example.com
Received: from localhost (localhost [127.0.0.1])
    by example.example.com (Postfix) with ESMTP id 9C67EC21B12
    for <robin@example.com>; Sat,  7 Apr 2012 17:58:14 +0200 (CEST)
X-Virus-Scanned: Debian amavisd-new at example.example.com
X-Spam-Flag: NO
X-Spam-Score: -2.47
X-Spam-Level: 
X-Spam-Status: No, score=-2.47 required=6.31 tests=[ALL_TRUSTED=-1,
    BAYES_00=-1.9, HTML_MESSAGE=0.001, MIME_HTML_MOSTLY=0.428,
    TVD_SPACE_RATIO=0.001] autolearn=ham
Received: from example.example.com ([000.000.000.00])
    by localhost (example.example.com [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id HIlqLaU+2IIL for <robin@example.com>;
    Sat,  7 Apr 2012 17:58:11 +0200 (CEST)
Received: from Ciccidator (00-000-00-000-no56.tbcn.telia.com [00.00.00.000])
    by example.example.com (Postfix) with ESMTPA id 866F2C2059C
    for <robin@example.com>; Sat,  7 Apr 2012 17:58:11 +0200 (CEST)
From: "Example Sender" <example@domain.com>
To: <robin@example.com>
Subject: utvecklat
Date: Sat, 7 Apr 2012 17:58:01 +0200
Message-ID: <000001cd14d7$35d38070$a17a8150$@example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_000_0001_01CD14E7.F95C9E90"
X-Mailer: Microsoft Outlook 14.0
Thread-Index: Ac0U1zWPte9QsazETMuTKG6LDnf1fg==
Content-Language: sv

Answer by Starx

Omitting the junk mail covers a lot of ground, and can never be a 100% solution. Because email filtration might be different from one span engine to other.

  • One the best way to confirm you always receive the mail is to the add the sender’s email address on the recipient’s address book.

  • Another, you should confirm if the sender’s email is not blacklisted on the internet.

  • Always confirm you have sent the emails with correct header.

  • Use Email Libraries like PHP Mailer

Fruther more: Check out this answer about MX entries.

March 12, 2012

two foreach loops combined for email sending

Question by Bowser Entertainment

I am trying to take a two text areas with multiple emails, one with the “from” emails, and one with the “to” emails. Combine the emails line by line and send emails out accordingly.

Ex:

“To” list:

        mike@gmail.com

        nick@hotmail.com

        adam@yahoo.com

“From” list:

        ashley@gmail.com

        brittney@yahoo.com

        racheal@hotmail.com

I want a email sent to:

   mike@gmail.com from ashley@gmail.com

   nick@hotmail.com from brittney@yahoo.com

   adam@yahoo.com from racheal@hotmail.com

Any help would be greatly appreciated. Thanks in advanced.

Below is the script I got so far, It sends to multiple emails from one email.

 <?php
 if (isset($_POST['submit']))
 {

    // Execute this code if the submit button is pressed.
    $raw_email_account = $_POST['email_from'];
    $email = $_POST['email_to'];
    $sent = "";
    $invalid = "";

    //Separating each line to be read by foreach
    $list = explode("n",$email);

    //Rendering the separeted data from each line
    foreach($list AS $data) {
                //Separating each line to be read by foreach
            $item = explode(":",$data);
            $mail_body = '<html><body>email here</body></html>';
                $subject = "subject here";
                $headers  = "From:".$raw_email_account."rn";
                $headers .= "Content-type: text/htmlrn";
                $to = $item[0];

                $mail_result = mail($to, $subject, $mail_body, $headers);

            if ($mail_result) {
               $valid++;
            } else {
                   // write into an error log if the mail function fails
                   $invalid++;
            }
    }

}
?>
<html>
<head>
</head>
<body>
<form action="email_sender.php" method="POST">
<div align="center">From Email Accounts: <textarea name="email_from" cols="100"    rows="60"></textarea></div><br />
<div align="center">To Email Accounts: <textarea name="email_to" cols="100" rows="60">        </textarea></div><br />
<div align="center"><input type="submit" name="submit"></div>
<br>
Valids: <?php echo $valid;?>
<br>
Invalids: <?php echo $invalid;?>
</body>
</html>

Answer by Vitalmax

Add logic that provides same count of $email and $raw_email_account arrays before foreach loop.

$list = explode("n",$email);
$list2 = explode("n", $raw_email_account);

foreach($list AS $key=>$data) {
            ...
            $headers  = "From:".$list2[$key]."rn";
            ...
}

Answer by Starx

If the array is by default, the indexes will coincide. So you can do something like this.

$toList = array(..);
$fromList = array(...);

foreach($toList as $key => $value) {
    $toAddress = $value;
    $fromAddress = $fromList[$key]; 
    //..
    //.. Go on with you mail function
}
March 11, 2012

PHP mail() error 500

Question by Chevi

As part of the registration process on a website, I have added mail confirmation. But for some reason, the mail function throws an Internal server error in there.

The strange thing is that if I create a test script, with the exact same email (all the same parameters) it works fine, and sends the email.

The mail is sent from a function in a Class, in case that helps. I didn’t post the code because it isn’t really relevant, even if I try mail('email@email.com','subject','email'); it fails with the 500 error!

The server error logs don’t show anything at all, anyone knows what may cause such a problem?

Technology:

The server is running php through mod_fastcgi, although this problem also happens if I switch to mod_suphp.

Updates:

UPDATE:
I’ll try to explain this better, the mail function works perfectly if called from another file, with the same parameters. The problem here is something that combined with the mail function causes an error 500. The rest of the file where it’s called is fine too, if I comment the mail function everything works. The way it gets called is an AJAX request to a file that calls a function where the mail is sent (Just in case this helps)

UPDATE 2:
In response to answers so far, here is more information I did not share previously:
OS: CentOS release 5.8
When I say error 500, I mean that the server returns only an HTTP 500 status code.
The server does not show anything in any error log
The most important thing is that if I create a file called test.php, with only mail('address@domain.com','Subject','Message'), it works just fine. When called from this other file, 500 status code returned.
What I am asking is if anyone knows, probably from experience, what could be causing this.

UPDATE 3:
Someone had the same problem yesterday: PHP's mail() function causes a 500 Internal Server Error only after a certain point in the code

UPDATE 4:
After some testing, I have discovered that the 500 stats code is only returned when the script is called via AJAX. If I create a file called test.php, and I simply place the mail function and test it, it works. Calling it via AJAX doesn’t, any ideas?

Answer by Chevi

After some more hours of testing I have found the problem!
I was using window.location to redirect the user to a new page after the AJAX call was completed, in it’s callback function.
Apparently if you modify it after an AJAX call to a php script that uses the mail() function, the server returns a 500 status code in the request

Answer by Starx

With the amount of input you have provided, it is very hard to tell, how that error occured.

Error: 500 are Interval Server Errors and can have more than one reasons for occuring.

  • A malformed php cgi script
  • An invalid directive in an .htaccess or other config file
  • Limitation imposed by file system and server software.
    May be you are attachcing a file to be to be sent
  • Missing Line Breaks (rn) in the headers

Try every solution listed from this cPanel Forums

December 2, 2011

Is it safe to put email addreses in PHP variables against email harvesters?

Question by enchance

Is it safe to use either of these lines without having the emails being picked up by email harvesters?

My email: <a href="mailto:<?php echo 'testing@example.com'; ?>"><?php echo 'testing@example.com'; ?></a>

or this one with variables

<?php $email = 'testing@example.com'; ?>
My email: <a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a>

or does it even matter for as long as it’s generated by PHP then it’s safe?

Answer by Jeremy Banks

Those will provide no protection at all. PHP is run on the server side. It will generate an HTML document and send it to the client. From the perspective of a bot or a user this is exactly the same as if you had just put the email address in a normal HTML document:

My email: <a href="mailto:testing@example.com">testing@example.com</a>

Answer by Starx

Your both example with give the same HTML output

My email: <a href="mailto:testing@example.com">testing@example.com</a>

and YES it is vulnerable to crawler.

If email address is such of such importance.

Create a contact form, add some captcha and send the form details through PHP, without exposing the email address anywhere in the page.

January 10, 2011

How to avoid original msg while reply using PHP?

Question by VinothPHP

Dear all,
how to avoid original message while reply for incoming mail using php, Am download the mail from mail server, the message content ll be combine with original messages, so its confused, and some of html entities are placed with that content,because of that original messages,
So how to avoid that original messages using php, anyone can help me…

Thanks in Advance….

Regards,
Vinoth S

Answer by Starx

I am not sure, if this is your answer but

$message = preg_replace ('/<[^>]*>/', '', $message);

OR

$message = strip_tags($message);

Will remove all the html entities from your message? Try it

August 2, 2010

php: send email when ftp-upload was a success?

Question by matt

hey guys,
i wonder what i’m doing wrong? I’m working on a ftp-upload with php. if files are uploaded successfully i want to get a confirmation email. just a simple email.

if my connection to the FTP Server was a success i’m calling the sendmail() function! it’s not working!

        function sendmail() {
            $EmailFrom = "do-not-reply@mypage.com";
            $EmailTo = "myemail@mypage.com";
            $Subject = "File uploaded to your FTP Server";
            $Body = "Howdy, files have just been transferred to your Server.";
            // Email Headers with UTF-8 encoding
            $email_header = "From: " . $EmailFrom . "rn";
            $email_header .= "Content-Type: text/plain; charset=UTF-8rn";
            $email_header .= "Reply-To: " . $EmailFrom . " rn";
            $success = mail($EmailTo, $Subject, $Body, $email_header);
            if ($success){
              print "success with EMAIL";
            }
            else{
              print "error with EMAIL";
            }
        }

any idea what i’m doing wrong here? does the $EmailFrom value have to be an actual Emailaddress? It’s just not working. Neither success nor error gets printed out. And nothing of my code AFTER the function-call gets executed.

thank you for your help

Answer by Starx

Well if you want to sendmail() when ftp upload is success do something like this

$status = move_uploaded_file($src,$destination);
if($status) { sendmail(); }

What this will do this, first $status will hold the boolean value whether the upload is successful, and if it suceeds then it will call your sendmail() function

June 8, 2010

Why is this mail going straight to SPAM box?

Question by Starx

I am using the following script to send mail

<?
extract($_POST);
$subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
$mail = @mail($send,$subject,$content);
if($mail) { echo "Your feedback has been sent"; }
else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>

But this is always ending up in the spam Folder. Why?

Answer by Starx

You have to use headers while you send mail, to prove that the mail arrives from a genuine source and not a bot.

Try this!

<?
  extract($_POST);
  $subject = "Feedback from ".$name." (".$email.", Ph: ".$phone.")";
  $headers  = 'MIME-Version: 1.0' . "rn";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
  $headers .= 'From:'.$email."rn";
  $headers .= 'Reply-To: '.$email;
  $mail = @mail($feedback,$subject,$content,$headers);
  if($mail) { echo "Your feedback is send"; }
  else { echo "We are sorry for the inconvienience, but we could not send your feedback now."; }
?>
...

Please fill the form - I will response as fast as I can!