Search code examples
yiiyii-components

sending mail from yii


public function mailsend($name, $contact_no, $email,$website,$content,$subject,$address ) 
{
    $message        = new YiiMailMessage;
    $message->view  = 'viewfilenm';
    $message->setBody(array(), 'text/html');
    $body           = $message->message->getBody();
    /******  preg_replace :Perform a regular expression search and replace ******/
    $body           = preg_replace('/\[FNAME]/',$name,$body); 
    $body           = preg_replace('/\[CONTENT]/',$content,$body);
    $find           = array("[CONTACT_NO]"=>$contact_no,"[FNAME]"=>$name,"[EMAIL]"=>$email,"[ADDRESS]"=>$address);
    /****** strtr :Translate characters or replace substrings ******/
    $newstr         = strtr($content, $find);
    $body           = str_replace($content,$newstr,$body);
    $body           = preg_replace('/\[CONTACT_NO]/',$contact_no,$body);
    $body           = preg_replace('/\[EMAIL]/',$email,$body);
    $body           = preg_replace('/\[SUBJECT]/',$subject,$body);
    $body           = preg_replace('/\[WEBSITE]/',$website,$body);
    $message->message->setBody($body, 'text/html');
    $message->subject = $subject;
    $message->addTo($email);
    $message->from   = ('[email protected]');
    Yii::app()->mail->send($message);
}

in $message->addTo() if I pass my gmail id then in gmail I got the mail. but if i pass my yahoo or other id in $message->addTo() then I don't get the mail and no error also display.


Solution

  • If email is getting delivered to some addresses but not others, it's probably not an issue with your code. It's probably your sever.

    Email delivery is complicated. Almost every endpoint (Gmail, Yahoo, etc) has different spam rules. The biggest problem you are going to face is getting the IP address you are sending from to be recognized as "safe".

    I've had the best luck sending email using an established SMTP server (like Gmail) as my send agent in Yii.

    Here are a some other resources about deliverability:

    http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html http://www.engineyard.com/blog/2009/how-to-ensure-your-email-gets-delivered/