Using mail() to send HTML email

Discussion in 'PHP' started by dfsweb, Jan 21, 2007.

  1. #1
    Hi guys,
    I have been using the mail() command to send HTML formatted email for a while now. I simply add the appropriate headers and it works. I recently moved my hosting from a shared hosting plan with GoDaddy to a Virtual Dedicated Server with GoDaddy and this stopped working?? :confused:

    The emails now go out fine but instead of going out as an HTML email, the receiver receives the actual HTML code displayed in their email. Has anybody else come across something similar?? Please help.
    Regards,
    dfsweb
     
    dfsweb, Jan 21, 2007 IP
  2. sukantab

    sukantab Well-Known Member

    Messages:
    2,075
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Which version of php is used in tgh new server may be that is causing the problem
     
    sukantab, Jan 22, 2007 IP
  3. vvoole

    vvoole Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use PHPmailer class instead mail() This is excellent and easy way to send mail. You can find it here — phpmailer.sourceforge.net
     
    vvoole, Jan 22, 2007 IP
  4. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #4
    do you send customized headers?
     
    linkstraffic, Jan 22, 2007 IP
  5. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #5
    I am using these headers:
    $headers="From: \r\n"."Reply-To: \r\n"."Content-Type: text/html;\r\n charset=\"ISO-8859-1\"\r\n";
    mail($email_address,$subject,$complete_body,$headers);
     
    dfsweb, Jan 22, 2007 IP
  6. StanUK

    StanUK Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    dfsweb, I would follow vvooles advice and use PHPmailer, its just a basic call to a class and exceptionally powerful for sending HTML embedded email.
     
    StanUK, Jan 22, 2007 IP
  7. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #7
    Can PHPMailer be used for sending multiple emails. I need to send out a newsletter to around 3,000 members in my database that are split into mailing lists of 750 each. Can somebody please post a basic script for sending an email using this command to one as well as more than one person? I would really appreciate this. I saw the example on the URL mentioned above but it had a lot of options that I don't need. I just want to send out a basic email (no attachments).
    Thanks!
    dfsweb
     
    dfsweb, Jan 22, 2007 IP
  8. vvoole

    vvoole Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I use something like that:

    <?php
    
    require_once('phpmailer.class.php');
    
    /***
    	 Mail settings
    ***/
    
    $M['method'] = 'mail'; // mail or smtp 
     $M['fromname'] = 'From Name';
    $M['from'] = 'no-reply@site.com';	
    $M['admin'] = '11111@2222222.com';
    $M['lang'] = 'phpmailer.lang-en.php';
    
    class Mailer extends PHPMailer{
           	var $WordWrap = 75;
    		function Mailer(){
    			global $M;
    			include($M['lang']);	
    			$this->language = $PHPMAILER_LANG;
    			$this->Mailer = $M['method'];
    			
    			if($M['method']=='smtp') 
    				$this->Host = $M['smtphost'];
    		}
    			
        function send2user($to, $subject, $body){
        	global $M;
        	$this->ClearAddresses();
        	$this->ClearAttachments();
        	
        	$this->Subject = $subject;
        	$this->Body = $body;
        	$this->AddAddress ($to);
      	$this->FromName = $M['fromname'];
    	$this->From = $M['from'];
            $this->send() || add_log("$this->ErrorInfo\n\n$subject\n$body");
        }
    }
    
    function add_log($log){
    	// put action for log here	
    }
    
    
    /***
    	Database connect
    ***/
    
    $con = mysql_connect('localhost', 'name', 'pass');
    mysql_select_db('database',$con);
    
    $query = "SELECT email FROM newsletter";
    $result = mysql_query($query);
    
    /***
    	Sending
    ***/
    
    if ($result){
    $mailagent = new Mailer();
    while($row = mysql_fetch_row($result)){
        $mailagent->send2user($row[0], 'put subject here', 'put letter here');		
    }
    }
    
    ?>
    Code (markup):
    Note that this is crude code. Actually I split it in some files — mailer.class.php, setting.php and action.php for example. Also I set mark in database if letter is sent successfully or not, set the time of last sending and other useful things.
     
    vvoole, Jan 22, 2007 IP
  9. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #9
    you can use header type too

    
    $email = abc@yahoo.com
    $headers = "from:".$email;
    $headers .= "Content-Type: text/html; charset=us-ascii\n";
    $headers .= "Content-Transfer-Encoding: 7bit";
    
    
    mail("toemail","Subjectl","message",$headers);
    echo "<br>";
    
    
    PHP:
     
    technoguy, Jan 22, 2007 IP
  10. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #10
    may be it's a domain problem, did you set up all corresponding redirections for your domain name on your new hosting plan:
    such as: smtp.yourdomain.com
     
    linkstraffic, Jan 23, 2007 IP
  11. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #11
    I tried just about everything to make this work guys, with no luck. I had almost given up when I sent a test email to my Yahoo account and it worked! :) It seems that for some reason emails that are being sent intra-server, i.e. from the server to any of my email addresses that are hosted on that server, I can see the HTML code instead of the normal HTML. But, if I email myself on an external email address, it is working like it should. Not sure why :confused:
    Regards,
    dfsweb
     
    dfsweb, Jan 23, 2007 IP
  12. baddot

    baddot Active Member

    Messages:
    309
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #12
    baddot, Jan 23, 2007 IP
  13. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Just my 2 (euro)cents: if you plan to send newsletters and mails to a large userbase I would set up a cron job that does this on smaller chunks of users and on a timely basis (say 200 emails per minute)

    Also, as stated here: http://php.net/manual/en/function.mail.php (in the notes), the best way to send large amounts of email is using PEAR::Mail: http://pear.php.net/package/Mail

    Cheers!
     
    picouli, Jan 24, 2007 IP
  14. Bombaywala

    Bombaywala Peon

    Messages:
    1,249
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Why not give a try to phplist - this works all the time! It is prebuilt - you don't have to worry about coding - unless if you love coding :)
     
    Bombaywala, Jan 25, 2007 IP