Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php to send email with attachments

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use php to send email with attachments, the article is very detailed, has a certain reference value, interested friends must read it!

The code is as follows:

/ * PHPMailer is a PHP function package for sending email. The features it provides include:

*。 Specify multiple recipients, CC address, BCC address and reply address when sending mail

*。 Multiple email encodings are supported, including: 8 bit, Base64, binary and quoted-printable

*。 Support for SMTP authentication

*。 Support for redundant SMTP servers

*。 Messages with attachments and Html format are supported

*。 Custom header

*。 Support for embedding pictures in messages

*。 Flexible debugging

*。 Tested compatible SMTP servers include: Sendmail,qmail,Postfix,Imail,Exchange, etc.

*。 Can run on any platform

PhpMailer is a very powerful php send email class, you can set the sending email address, reply address, email subject, rich text content, upload attachments.

Official website: http://phpmailer.worxware.com/

Download address: http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list

, /

Require_once ('include/PHPMailer/class.phpmailer.php'); / / Import the PHPMAILER class

$mail = new PHPMailer (); / / create an instance

$mail-> CharSet='utf-8'; / / set character set

$mail-> SetLanguage ('ch','include/PHPMailer/language/'); / / set the language type and the directory where the language files are located

$mail-> IsSMTP (); / / send it in SMTP mode

$mail-> SMTPAuth = true; / / sets 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 user name of the sender in the SMTP host

$mail-> Username = SMTP_USER_NAME; / / name of the sender

$mail-> Password = SMTP_USER_PASS; / / Sender's password on the SMTP host

$mail-> Subject = 'Test message title'; / / message subject

$mail-> AltBody = 'text/html'; / / sets the alternate display when the message body does not support HTML

$mail-> Body = 'Test the content of the message'; / / the content of the message is made

$mail-> IsHTML (true); / / whether it is a HTML email

$mail-> AddAddress ('chinajason2008#gmail.com','jasonxu'); / / address and name of the recipient

$mail-> AddReplyTo ('chinajason2008#gmail.com','jasonxu'); / / the address and name to which the recipient replies

$mail-> AddAttachment ('include/id.csv','att.csv'); / / path and attachment name of the attachment

If (! $mail-> Send ()) / / send email

Var_dump ($mail-> ErrorInfo); / / View the error message sent

Note: when phpmailer adds attachments, the attachment suffix must be specified in the attachment name. If the attachment suffix is not specified, the default attachment suffix will be .txt.

For example, $mail-> AddAttachment ('include/id.csv','att'); / / the path and name of the attachment

If you add an attachment and send it as above, the final attachment you receive may be att.txt.

AddAttachment can set the attachment coding method and attachment type. For example, the attachment added above can also be set to

$mail-> AddAttachment ('include/id.csv','att.csv', "binary", "text/comma-separated-values"); / / the path and name of the attachment,

There are probably several encoding methods for attachments: 8bit, base64, binary and and quoted-printable are supported.

And the acceptable MIME Type of CSV

Application/octet-stream

Text/comma-separated-values (recommended)

Text/csv

Therefore, the attachment type of a file in csv format can be any of the above three

An example of email sent in a previous project to sort out an abbreviated version that is easy to apply:

The copy code is as follows:

$body=$_smtp_body

$mail=new PHPMailer (); / / get a PHPMailer instance

/ / $mail- > SMTPSecure='tls'

$mail- > CharSet= "utf-8"; / / set the encoding

$mail- > IsSMTP (); / / set to send mail in SMTP mode

$mail- > Host=$_smtp_server;// sets the address of the SMTP mail server

$mail- > Port=$_smtp_port;// sets the port of the mail server. The default is 25.

$mail- > From=$_smtp_from_mail; / / set the email address of the sender

$mail- > FromName=$_smtp_from_name;// sets the name of the sender

$mail- > Username=$_smtp_username

$mail- > Password=$_smtp_password

$mail- > AddAddress ("$email", ""); / / set the address (parameter 1) and name (parameter 2) of the pickup

$mail- > SMTPAuth=true;// enables SMTP authentication

$mail- > Subject=$_smtp_subject;// sets the title of the message

/ / $mail- > AltBody= "text/html"

$mail- > Body=$body;// message content

$mail- > IsHTML (true); / / set whether the content is of html type

/ / $mail- > WordWrap=50; / / set the number of characters per line

/ / $mail- > AddReplyTo ("samzhang@tencent.com", "samzhang"); / / set the address of the recipient of the reply

$mail- > SMTPDebug=0

If ($mail- > Send ()) {/ / send email

Exit 'ok'

} else {

Exit 'fail'

}

I probably remember that there were inexplicable and strange problems when I first used PHPMailer, and I spent a lot of time looking for information on the Internet before it was finally solved. At present, remember that the server PHP environment can not disable the fsockopen function, otherwise the mail can not be sent, but there is also a solution. In short, at the beginning of the use, there is always something wrong, due to a long time, now want to come, do not know what has been changed. Therefore, the PHPMailer directory files that are being used now are packaged and uploaded to CSDN for the convenience of future use, as well as for friends who are worried about it. PHPMailer download: http://xiazai.jb51.net/201304/yuanma/PHPMailer_jb51net.rar

In addition, the contents of the problems that occurred at that time are sorted out as follows:

1 、 Error: Could not connect to SMTP host

Reason 1: non-mail system requires different smtp requests, but all allow uppercase, some do not support lowercase, such as NetEase, Tencent mailbox. (as for whether this is the case, I have not tested it. Anyway, it will be changed to uppercase, which will not affect it.)

Solution:

The copy code is as follows:

Public function IsSMTP () {

$this- > Mailer = 'SMTP'; / / smtp-> SMTP; that is, it used to be lowercase but now uppercase.

}

/ / Choose the mailer and send through it

Switch ($this- > Mailer) {

Case 'sendmail':

Return $this- > SendmailSend ($header, $body)

Case 'SMTP':// also changes smtp-> SMTP; that is, it used to be lowercase, but now it is uppercase.

Return $this- > SmtpSend ($header, $body)

Case 'mail':

Default:

Return $this- > MailSend ($header, $body)

}

2 、 SMTP Error: Could not connect to SMTP host

Reason: some virtual hosts, or servers, block the "fsockopen () function" for security reasons, so that the mail cannot be sent.

Solution:

Enable the fsockopen function

First, remove the following two semicolons from php.ini

; extension=php_sockets.dll

; extension=php_openssl.dll

Replace fsockopen function

You can replace the fsockopen function in the class.smtp.php file with the pfsockopen function

3 、 Could not instantiate mail function

Reason:

The parameters set are incorrect. I used gmail to do some basic tests. I need to set other parameters this time.

Solution:

$mail- > SMTPSecure = 'tls'; / / only need to add this sentence

Note: I have never encountered this kind of error, so in the above example, I have added comments on this content. If you encounter this kind of mistake, you can use this sentence to try.

The above is all the contents of the article "how to use php to send an email with attachments". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report