开发者

PEAR PHP mailing with name from sender

开发者 https://www.devze.com 2023-03-15 17:57 出处:网络
I am using PEAR\'s smtp mailer in PHP and it is working great with attachments CC and BB as well as multiple recipients.My boss needs the sender to say the name of the sender when it is sent to an ema

I am using PEAR's smtp mailer in PHP and it is working great with attachments CC and BB as well as multiple recipients. My boss needs the sender to say the name of the sender when it is sent to an email. Our sales reps say that this is a must. I tried to follow the directions but when I use the code below the email wont send. Im guessing that all email types within the mailer have to be written in the same format. For example

 //GET EMAIL OF USER
        $result = mysql_query("SELECT email, email_pass, fullname FROM u_perinfo WHERE user_id = '$_SESSION[uid]'")
开发者_开发百科        or die("There was an error when grabbing your email information");
        if(mysql_num_rows($result) > 0){
            $row = mysql_fetch_array($result);
            if($row[0] != ''){
                $from = "$row[2] <$row[0]>";
            }
            $email_pass = $row[1];
        }

        if(!empty($additional)){
            $email .= ", $additional";
        }

        $recipients = array();
        $headers["Subject"] = $subject;
        $headers["From"] = "$from";
        $headers["To"] = "$email";
        $headers["Cc"] = "$cc";
        $headers["Bcc"] = "$bcc";

will produce John Doe <johndoe@gmail.com> for the from variable Whereas the the line $from = "$row[2] <$row[0]>;"; turned to $from = $row[0] will produce just johndoe@gmail.com When doing it with the later (with just the email) it sends the information and works perfectly. When trying to incorporate the name it doesn't work at all. Am I doing something wrong? Any help would be greatly appreciated.

UPDATE: this is my mail code that you asked for

$crlf = "\n";
        $mime = new Mail_mime($crlf);
        // Setting the body of the email
        $mime->setTXTBody($mailmsg);
          $mime->setHTMLBody($mailmsg);                                           body = $mime->get();
        $headers = $mime->headers($headers);

        //$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information.  \r\n\r\n(Please do not share your login information.)$accountinfo";
        /* SMTP server name, port, user/passwd */
        $smtpinfo["host"] = "smtp.emailsrvr.com";
        $smtpinfo["port"] = "25";
        $smtpinfo["auth"] = true;
        $smtpinfo["username"] = "$from";
        $smtpinfo["password"] = "$email_pass";
        /* Create the mail object using the Mail::factory method */
        $mail_object =& Mail::factory("smtp", $smtpinfo);
        /* Ok send mail */
        $mail_object->send($recipients, $headers, $body);


My problem was, that the name should be quoted as the following:

$from = '"Mywebsite.com" <noreply@mywebsite.com>';

if NAME contains ()<>@,;\:". the string should be quoted

Hope it helps someone ;)


You should Use

$from = $row[2]." <".$row[0]."> ";

instead

$from = "$row[2] <$row[0]>;";

Also It can be problem with double semicolon. Use only one semicolon at the end.


Add another header:

$fullName = $row[2];
$headers["X-Sender"] = "<$fullName>";
0

精彩评论

暂无评论...
验证码 换一张
取 消