I am trying to add a script from an older site that I have recently replaced with drupal. The script uses the php mail() function, but this doesn't seem to work within drupal.

So a little research I found that there is the drupal_mail() function, which I have had a little luck with. However I am running into two issues:

(1) I can't use this function to send a subject line with a dash in it. when I do have a subject line with a dash then drupal_mail() doesn't do anything
(2) I can't figure out how to cc or bcc one or several emails...I try to place it in the header but drupal_mail() sends the email but it doesn't go to any of the cc or bcc in the list

So my question is

(1) How do I use the php mail() function in drupal so that I don't have to completely rewrite my tiny script? Is this even possible to do?
(2) If I have no other options and I have to use the drupal_mail() option, then what do I need to do in order to fix any of my two issues above?

Thanks in advance!

Comments

CleanCutRogue’s picture

I have used the php mail() function many times from drupal sites... it seems to work fine for me. Here is an example where I use it to send a dealer order to the site's email address, then use it again to send a confirmation to the user (a "dealer"). Note that I used html in the document and so had to add some additional header info. Hopefully this formats okay...

    $user=user_load(array('uid'=>$user->uid));
    $To = variable_get('site_mail','support@provingzone.com');
    $From = $user->mail;
    $Subject = 'Dealer Order';
    $Body="<h1>DEALER:</h1>";
    $Body.="<table>";
    $Body.="<tr><td>Company Name</td><td>".$user->profile_companyname."</td></tr>";
    $Body.="<tr><td>Vender/Federal Tax Number</td><td>".$user->profile_vender_fedtax_number."</td></tr>";
    $Body.="<tr><td>Contact Name</td><td>".$user->profile_primarycontact."</td></tr>";
    $Body.="<tr><td>Email Address</td><td>".$user->mail."</td></tr>";
    $Body.="<tr><td>Phone Number</td><td>".$user->profile_phone."</td></tr>";
    $Body.="<tr><td>Fax Number</td><td>".$user->profile_fax."</td></tr>";
    $Body.="<tr><td>Website</td><td>".$user->website."</td></tr>";
    $Body.="<tr><td>Shipping Address</td><td>".$user->profile_shipping_street."<br> ".$user->profile_shipping_city.', '.$user->profile_shipping_state.'  '.$user->profile_shipping_zipcode."<br>".$user->profile_shipping_zone."</td></tr>";
    $Body.="<tr><td>Billing Address</td><td>".$user->profile_billing_street."<br> ".$user->profile_billing_city.', '.$user->profile_billing_state.'  '.$user->profile_billing_zipcode."</td></tr>";
    $Body.="</table>";
    $Body.="<br><br>";
    $Body.="<h1>ORDER:</h1>";
    $cart=eval('return '.user_load(array('uid'=>$user->uid))->cart.';');
    if ($cart){
        $keys=array_keys($cart);
        $Body.= '<table style="border:1px solid black;width:100%"><thead><tr valign="bottom" style="background:#DCDCDC; border:1px solid black;"><th style="border:1px solid black;">Qty*</th><th style="border:1px solid black;">Item#</th><th style="border:1px solid black;">Product</th><th style="text-align:right;border:1px solid black;">Unit Price</th><th style="border:1px solid black;">&nbsp;</th></tr></thead><tbody>';
        $sum=0;
        foreach($keys as $key){
            $v=explode('v',$key);
            $k=$v[0];
            $n=node_load($k);
            $selcount=1;
            $sel=explode("\n",$n->field_variations[0]['value']);
            foreach($sel as $s){
                $seltitle=explode(":",$s); $seltitle=$seltitle[0];
                $s=substr($s,strlen($seltitle)+1);
                $s=explode(',',$s);
                $count=1;
                foreach($s as $ss) {
                    $showss=explode(":",$ss); 
                    $n->variation[$selcount][$count]=array($showss[0],$showss[1],$showss[2]); 
                    $count++; 
                }
                $selcount++;
            }
            $variation=NULL;
            $itemnumber=($n->field_productitemnumberuse[0]['value']==1?'':$n->field_proditemnumber[0]['value']);
            $price=$n->field_prodprice[0]['value'];
            for($x=1;$v[$x];$x++){
                $variation[]=trim($n->variation[$x][$v[$x]][0]);
                $itemnumber.=trim($n->variation[$x][$v[$x]][1]);
                $price+=($n->variation[$x][$v[$x]][2]);
            }
            $sum+=($cart[$key]*$price);
            $Body.='<tr valign="top" style="border:1px solid black;"><td style="border:1px solid black;">'.$cart[$key].'</td><td style="border:1px solid black;">'.$itemnumber.'</td><td style="border:1px solid black;">'.$n->title.($variation!=NULL?'<br>&nbsp;&nbsp;&nbsp;('.implode(',',$variation).')':'').'</td><td style="text-align:right;border:1px solid black;">'.number_format($price,2).' ea</td><td style="text-align:right;border:1px solid black;">'.number_format($cart[$key]*$price,2).'</td></tr>';
        }
        $Body.='<tr><td> </td><td> </td><td> </td><td align="right"> <b>Total:</b></td><td style="border:1px solid black; background:#fff;text-align:right;">$'. number_format($sum,2). '</td></tr>';
        $Body.='</tbody></table>';
    }  
    $headers = "From: $From\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $Body=html_entity_decode($Body);
    mail($To,$Subject,$Body,$headers);

    $headers = "From: $To\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail($From,'Thank you for your order',"This is a copy of the order as it was received.  Please retain it for your records.\n\n\n".$Body,$headers);
    user_save($user,array('cart'=>''));
    drupal_goto('thankyou');

JoshuaKissoon’s picture

Hey i did a tutorial on this can be founSending HTML and Plain text Mail with Drupal 6d here:

Also to add cc, you can add these lines to the module found at the above link, add it to line 80:
$message['headers']['Cc] = 'cc@google.com'
$message['headers']['Bcc] = 'cc@google.com'