Phpmailer Send Chinese name attachment

Source: Internet
Author: User
Tags php tutorial
Reprint please indicate this article from Skypupil's blog (http://blog.csdn.net/u011956441/article/details/44161587), please respect others ' hard work results, thank you!

My application scenario is: When a user enters an email address and clicks the "Send Mail" button on a Web page, it calls sendmail.php to send a message with an attachment to the user.

The problem is that when the name of the attachment is in English, it is sent correctly, but when the attachment name is Chinese, the user's mailbox will not receive the attachment. Here is my workaround: first sendmail.php code:

 Smtpdebug = 4; Set Debug information display level $mail->charset = ' UTF-8 '; Set the message encoding, default iso-8859-1, if the Chinese this entry must be set to UTF-8$MAIL->ISSMTP (); Set Use SMTP Service $mail->smtpauth = TRUE; Enable the SMTP authentication feature $mail->smtpsecure = ' SSL '; SMTP security protocol $mail->host = ' smtp.gmail.com '; SMTP server $mail->port = 465; The port number of the SMTP server $mail->username = ' yours@gmail.com '; SMTP Server user name (your mailbox account name) $mail->password = ' Password '; SMTP server password (your mailbox login password) $mail->setfrom (' yours@gmail.com ', ' yourName '); Set the sender address and name $mail->addreplyto (' yours@gmail.com ', ' yourName '); Set mail reply person address and name $mail->subject = ' title '; Set the message title $mail->altbody = ' To view the message, switch to the HTML-enabled mail client '; Optional, backwards-compatible consideration $mail->msghtml (' Hello, this is from Gmail '); Set the message content $mail->addaddress ($toEmailAddress); Recipient address//$mail->addattachment (' pdfs/test. pdf '); Add attachment file path, send attachment failed $mail->addattachment (iconv (' utf-8 ', ' gb2312 ', ' pdfs/test. pdf '), ' Test. pdf ');    Add Attachment parameter 1 is the file path, parameter 2 is the filename (the name of the message attachment can be different from the actual local file name) if (! $mail->send ()) {echo "Send failed:". $mail->errorinfo;} else { EchO "Congratulations, the mail was sent successfully! ";}? >

The key to solving the problem in the above code is to use the "$mail->addattachment (Iconv (' utf-8 ', ' gb2312 ', ' pdfs/test. pdf '), ' Test. pdf ');" This line.

Let's take a look at two methods:

addattachment--method

Derived from: Phpmailer::addattachment ()

File: class.phpmailer.php.

Description: Add an attachment.

Parameters: Path, name, encoding, type. Where the path is required and the other is optional

Function prototype:addattachment ($path,$name = ",$ encoding = ' Base64 ',$type = ' Application/octet-stream ') {}

iconv--method

From: PHP (>= 4.0.5)

Function Prototypes: String Iconv (String $in_charset, string $out_charset, String $str)

Description: Encodes the string str from thein_charset to the Out_CharSet.

After understanding the above two methods, we understand that Phpmailer cannot send the attachment of Chinese name, because Phpmailer can't read the UTF-8 encoded Chinese file path correctly, as long as the file path is converted to gb2312 encoding, it can be sent correctly.

Finally, attach the SMTP server port number address: http://blog.wpjam.com/m/gmail-qmail-163mail-imap-smtp-pop3/

The above describes the Phpmailer send the Chinese name attachment, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.