In PHP development and utilization of Phpmailer send mail is a common practice, this is more useful than PHP mail, let's see how phpmailer send email with attachments.
. First download the latest version of the package to http://phpmailer.worxware.com/
2. After the download is complete, locate the class.phpmailer.php, class.smtp.php two classes and put them in your own directory!
3. Then create a new PHP file named here: phpmail_jiucool.php
4.phpmail_jiucool.php content is as follows:
Instance
The code is as follows |
Copy Code |
function postmail_jiucool_com ($to, $subject = "", $body = "") { Author:jiucool website:http://www.jiucool.com $to represents the recipient address $subject represents the message header $body represents the message body Error_reporting (E_all); Error_reporting (e_strict); Date_default_timezone_set ("Asia/shanghai");//Set time zone East eight zone Require_once (' class.phpmailer.php '); Include ("class.smtp.php"); $mail = new Phpmailer (); New a Phpmailer object. $body = Eregi_replace ("[]", "', $body); Filtering the content of the message as necessary $mail->charset = "UTF-8";//Set the message encoding, default iso-8859-1, if the Chinese language must be set, otherwise garbled $mail->issmtp (); Setting up using the SMTP service $mail->smtpdebug = 1; Enabling the SMTP debugging feature 1 = errors and messages 2 = messages Only $mail->smtpauth = true; Enable the SMTP authentication feature $mail->smtpsecure = "SSL"; Security protocols $mail->host = "smtp.googlemail.com"; SMTP Server $mail->port = 465; Port number of the SMTP server $mail->username = "SMTP Server user name"; SMTP Server user name $mail->password = "SMTP server password"; SMTP server password $mail->setfrom (' sender's address, such as admin#jiucool.com #换成 @ ', ' sender name '); $mail->addreplyto ("Mail reply address, such as admin#jiucool.com #换成 @", "Mail reply person's name"); $mail->subject = $subject; $mail->altbody = "To view the message, please use an HTML compatible email viewer! -From www.jiucool.com "; Optional, comment out and test $mail->msghtml ($body); $address = $to; $mail->addaddress ($address, "Recipient name"); $mail->addattachment ("Images/phpmailer.gif"); Attachment $mail->addattachment ("Images/phpmailer_mini.gif"); Attachment if (! $mail->send ()) { echo "Mailer Error:". $mail->errorinfo; } else { echo "Message sent! Congratulations, Mail sent successfully! "; } } |
Attention:
Phpmailer If you add an attachment, you must specify the suffix of the attachment in the name of the attachment, and if the attachment suffix is not indicated, the default attachment suffix will be. txt.
Like what
The code is as follows |
Copy Code |
$mail-AddAttachment (' include/id.csv ', ' att ');// |
Path and attachment name of the attachment
If you add an attachment to the same send as above, the attachment that you eventually receive may be att.txt.
AddAttachment can set the attachment encoding and attachment type, such as the above attachment can also be set to
The code is as follows |
Copy Code |
$mail-AddAttachment (' include/id.csv ', ' att.csv ', "binary", "text/comma-separated-values");// |
The path and attachment name of the attachment,
There are several ways to encode attachments: Support 8bit, base64, Binary, and quoted-printable encoding
And the CSV acceptable MIME Type
· Application/octet-stream
· Text/comma-separated-values (recommended)
· Text/csv
Therefore, the file attachment type of CSV format can be any of the above three types
Instance
The code is as follows |
Copy Code |
Require_once (' include/phpmailer/class.phpmailer.php '); Import Phpmailer Class $mail = new Phpmailer (); Create an instance $mail, charset= ' utf-8 '; Set character sets $mail-setlanguage (' ch ', ' include/phpmailer/language/'); Set language type and language files in the same directory $mail-ISSMTP (); Send using SMTP mode $mail-Smtpauth = true; Set whether the server requires SMTP authentication $mail, Host = Smtp_server; SMTP Host Address $mail Port = Smtp_server_port; SMTP Host Port $mail, from = Smtp_user_mail; Sender email Address $mail-fromname = ' Jasonxu '; The sender's user name in the SMTP host $mail-Username = Smtp_user_name; The name of the sender $mail-Password = Smtp_user_pass; The sender's password in the SMTP host $mail-Subject = ' title of test message '; Message subject $mail-altbody = ' text/html '; Set alternate display when the message body does not support HTML $mail Body = ' content of test message ';//email content made $mail-ishtml (true); Whether it is an HTML message $mail-addaddress (' chinajason2008#gmail.com ', ' Jasonxu '); Address and name of the recipient $mail-Addreplyto (' chinajason2008#gmail.com ', ' Jasonxu '); Address and name to reply to when the recipient replies $mail-AddAttachment (' include/id.csv ', ' att.csv ');//the path and attachment name of the attachment if (! $mail, Send ())//Send mail Var_dump ($mail-errorinfo); To view sent error messages |
Just add this, and you can send an attachment with an instance mailbox.
$mail-AddAttachment (' include/id.csv ', ' att.csv ');//the path and attachment name of the attachment
http://www.bkjia.com/PHPjc/631600.html www.bkjia.com true http://www.bkjia.com/PHPjc/631600.html techarticle in PHP development and utilization of Phpmailer send mail is a common practice, this is more useful than PHP mail, the following we look at Phpmailer how to send an email with attachments: first ...