1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Email Function

Discussion in 'PHP' started by adamjblakey, Jul 23, 2014.

  1. #1
    Hi,

    I have the following function which im using to send emails from my website. It works fine however on some email accounts it wont come through. I know this will be to do with their spam / firewall settings but i was just wondering if there is any alterations that need to be done to this to make sure it stands the best chance of the email being sent to the reciptient.

    function email_users($to_email, $esubject, $to_name, $from_company, $from_email, $content){
    
                # -=-=-=- MIME BOUNDARY
               
                $mime_boundary = "----$from_company----".md5(time());
               
                # -=-=-=- MAIL HEADERS
               
                $to = $to_email;
    
                $subject = $esubject;
               
                $headers = "From: $from_company <$from_email>\n";
                $headers .= "Reply-To: $from_company <$from_email>\n";
                $headers .= "MIME-Version: 1.0\n";
                $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
               
                # -=-=-=- TEXT EMAIL PART
               
                $message = "--$mime_boundary\n";
                $message .= "Content-Type: text/plain; charset=UTF-8\n";
                $message .= "Content-Transfer-Encoding: 8bit\n\n";
               
                $message .= "Dear $to_name ,\n\n";
               
                # -=-=-=- HTML EMAIL PART
                
                $message .= "--$mime_boundary\n";
                $message .= "Content-Type: text/html; charset=UTF-8\n";
                $message .= "Content-Transfer-Encoding: 8bit\n\n";
               
                $message .= "<html>\n";
                $message .= "<body style=\"font-family:Tahoma, Geneva, sans-serif; font-size:11px; color:#333; line-height:18px;\">\n";
                $message .= "$content";
    
                    $message .= "<br>\n";
               
                $message .= "<br><span style='font-size:10px; color:#B9B8B8; line-height:12px;'><br><b>Email disclaimer</b><br>This email and its attachments may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of $from_company. If you are not the intended recipient of this email and its attachments, you must take no action based upon them, nor must you copy or show them to anyone. Please contact the sender if you believe you have received this email in error.</span>";
               
                $message .= "</body>\n";
                $message .= "</html>\n";
                  
                # -=-=-=- FINAL BOUNDARY
               
                $message .= "--$mime_boundary--\n\n";
               
                # -=-=-=- SEND MAIL
               
                $mail_sent = @mail( $to, $subject, $message, $headers );
    }
    
    Code (markup):
     
    adamjblakey, Jul 23, 2014 IP
  2. CIScripts

    CIScripts Member

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    30
    #2
    In most cases, the reason an email ends up in the spam folder is not within the code, it's within the content.

    If you have all your Reverse DNS & SPF Records setup and working, the only thing left to check is the email content.

    Here's my personal recommendation, read a really good article by MailChimp here:

    http://mailchimp.com/resources/guides/how-to-avoid-spam-filters/html/
     
    CIScripts, Jul 25, 2014 IP
    sarahk likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    You sent that to me it would instantly go to my spam box -- know why?

    Because I have my setup configured to automatically treat ALL HTML e-mails as spam, since no legitimate e-mail has any excuse to use HTML in it.

    Oh, and you really should be doing /r/n instead of just /n -- a lot of mailers will reject mail if you don't use both. I'd also suggest easing up on the ridiculous number of unnecessary hyphens in your mime-boundary declaration... and probably swing an axe at the number of unnecessary variable addition declarations when you could just newline the string addition... and probably lose the 'extra variables for nothing" like $to and make sure to strip formatting characters from anything you plug into $header.

    Of course even as HTML e-mail you're busted since you're trying to use CSS in it, when really HTML e-mail clients don't support anything more than HTML 3.2 in a reliable fashion.
     
    deathshadow, Jul 27, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Just a quick cleanup of your code, this may function better...

    function headerClean($value) {
    	return str_replace(["\n", "\r"], ' ', $value);
    }
    
    function email_users($to, $subject, $name, $company, $email, $content) {
    
    	$boundary = md5($company . time());
    	$lineBoundary = '--' . $boundary . "\r\n";
    	
    	$from = '<' . headerClean($email) . '>' . headerClean($email);
     
    	$headers = 
    		'From: ' . $from . "\r\n" .
    		'Reply-To: ' . $from . "\r\n" .
    		"MIME-Version: 1.0\r\n" .
    		'Content-Type: multipart/alternative; boundary="' . $boundary . "\"\r\n" .
    		'X-Mailer: PHP/' . phpversion();
     
    	$message = 
    		$lineBoundary .
    		"Content-Type: text/plain; charset=UTF-8\r\n" .
    		"Content-Transfer-Encoding: 8bit\r\n\r\n".
     
    		'Dear ' . $name . "\r\n\r\n" .
    		$content . "\r\n\r\n" .
    		
    		$lineBoundary . 
    		"Content-Type: text/html; charset=UTF-8\r\n" .
    		"Content-Transfer-Encoding: 8bit\r\n\r\n" .
    		
    		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en"><head>
    <title>', htmlspecialchars($subject), '</title>
    </head><body>
    
    <font face="tahoma,geneva,sans-serif" size="-1" color="#333333">
    	' . htmlspecialchars($content) . '
    </font><br>
    <br>
    <font face="tahoma,geneva,sans-serif" size="-2" color="#B9B8B8">
    	<b>Email disclaimer</b><br>
    	This email and its attachments may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of $from_company. If you are not the intended recipient of this email and its attachments, you must take no action based upon them, nor must you copy or show them to anyone. Please contact the sender if you believe you have received this email in error.
    </font>
    
    </body></html>' . "\r\n\r\n" . 
    
    		$lineBoundary;
     
    	return mail($to, headerClean($subject), $message, $headers);
    	
    }
    Code (markup):
    *NOTE* don't override errors with @ -- bad practice. Only use doubles when you need to like when you have \n or \r -- parses faster when it's not bytecode cached... and don't use CSS since most mail clients make a right mess of that; meaning you have to dial the clock back to the worst of 1997 style coding; which IMHO is just another reason to NOT using HTML in e-mails in the first blasted place.

    Notice I strip line-feeds from any values to be plugged into 'headers' -- you have to do this or people could pass in their e-mail or other values characters that could be used to inject their own header commands into it.
     
    deathshadow, Jul 27, 2014 IP
    malky66 likes this.