PHP mail sending class PHPMailer usage example _ PHP Tutorial

Source: Internet
Author: User
PHP mail sending class PHPMailer usage example details. PHP mail sending class PHPMailer usage example detailed examples this article describes PHP mail sending class PHPMailer usage, and detailed the specific operation steps. Share it with you for your reference. PHP mail sending PHPMailer usage example

The example in this article describes PHPMailer usage of PHP mail sending class and detailed the Operation steps. Share it with you for your reference. The procedure is as follows:

1. install sendmail on the server

1

Sudo apt-get install sendmail

2. start sendmail

1

Sudo/etc/init. d/sendmail start

3. modify php. ini

1

2

3

4

[Mail function]

SMTP = localhost

Smtp_port = 25

Sendmail_from = me@example.com

4. Function sendMail Function:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

/* Call PHPMailer to send an email

* @ Param String $ receiver recipient

* @ Param String $ sender

* @ Param String $ sender_name if the sender name is null, use the sender address instead.

* @ Param String $ subject

* @ Param String $ content

* @ Param boolean $ whether ishtml is an html email

* @ Param Array $ attachements attachment

* @ Return boolean

*/

Function sendMail ($ receiver, $ sender, $ sender_name, $ subject, $ content, $ ishtml = true, $ attachments = array ()){

Include_once "class-phpmailer.php ";

If (empty ($ receiver) | empty ($ sender) | empty ($ subject) | empty ($ content )){

Return false;

}

$ Mail = new PHPMailer ();

// $ Mail-> IsSMTP (); // sent by smtp

// $ Mail-> Host = "smtp.gmail.com"; // SMTP server

// $ Mail-> Port = 465; // SMTP Port

// $ Mail-> SMTPSecure = 'SSL '; // encryption method

// $ Mail-> SMTPAuth = true; // enable SMTP Authentication

// $ Mail-> Username = "username"; // User name

// $ Mail-> Password = "password"; // Password

$ Mail-> IsMail (); // The using PHP mail () function may prompt that this email may not be sent by the following users.

$ Mail-> From = $ sender; // sender

$ Mail-> FromName = $ sender_name; // sender alias

$ Mail-> AddReplyTo ($ sender); // reply

$ Mail-> AddAddress ($ email er); // recipient

// Send in html format

If ($ ishtml ){

$ Mail-> IsHTML (true );

}

// Send the attachment

If ($ attachments ){

If (is_array ($ attachments )){

$ Send_attachments = array ();

$ Tmp_attachments = array_slice ($ attachments, 0, 1 );

If (! Is_array (array_pop ($ tmp_attachments ))){

If (isset ($ attachments ['path']) {

Array_push ($ send_attachments, $ attachments );

} Else {

Foreach ($ attachments as $ attachment ){

Array_push ($ send_attachments, array ('path' => $ attachment ));

}

}

} Else {

$ Send_attachments = $ attachments;

}

Foreach ($ send_attachments as $ attachment ){

$ Attachment ['name'] = isset ($ attachment ['name'])? $ Attachment ['name']: null;

$ Attachment ['encoding'] = isset ($ attachment ['encoding'])? $ Attachment ['encoding']: 'base64 ';

$ Attachment ['type'] = isset ($ attachment ['type'])? $ Attachment ['type']: 'application/octet-stream ';

If (isset ($ attachment ['path']) & file_exists ($ attachment ['path']) {

$ Mail-> AddAttachment ($ attachment ['path'], $ attachment ['name'], $ attachment ['encoding'], $ attachment ['type']);

}

}

} Elseif (is_string ($ attachments )){

If (file_exists ($ attachments )){

$ Mail-> AddAttachment ($ attachments );

}

}

}

$ Mail-> Subject = $ subject; // mail title

$ Mail-> Body = $ content; // email content

Return $ mail-> Send ();

}

// DEMO example:

$ Author ER = 'author er @ test.com ';

$ Sender = 'sender @ test.com ';

$ Sender_name = 'sender name ';

$ Subject = 'subjecct ';

$ Content = 'content ';

// All four formats are supported

$ Attachments = 'attachment1.jpg ';

$ Attachments = array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg ');

$ Attachments = array('attachment1.jpg ', 'attachment2.jpg ');

$ Attachments = array (

Array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg '),

Array ('path' => 'attachment2.jpg ', 'name' => 'attachment 2.jpg '),

Array ('path' => 'attachment3.jpg ', 'name' => 'attachment 3.jpg '),

);

$ Flag = sendMail ($ sender er, $ sender, $ sender_name, $ subject, $ content, true, $ attachments );

Echo $ flag;

?>

Examples in this article describes the PHP mail sending class PHPMailer usage, and describes the specific operation steps in detail. Share it with you for your reference. With...

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.