博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用PHP发送电子邮件?
阅读量:2290 次
发布时间:2019-05-09

本文共 3821 字,大约阅读时间需要 12 分钟。

本文翻译自:

I am using PHP on a website and I want to add emailing functionality. 我在网站上使用PHP,并且想添加电子邮件功能。

I have WAMPSERVER installed. 我已经安装了WAMPSERVER。

How do I send an email using PHP? 如何使用PHP发送电子邮件?


#1楼

参考:


#2楼

If you are interested in html formatted email, make sure to pass Content-type: text/html; 如果您对html格式的电子邮件感兴趣,请确保传递Content-type: text/html; in the header. 在标题中。 Example: 例:

// multiple recipients$to  = 'aidan@example.com' . ', '; // note the comma$to .= 'wez@example.com';// subject$subject = 'Birthday Reminders for August';// message$message = '  Birthday Reminders for August  

Here are the birthdays upcoming in August!

Person Day Month Year
Joe 3rd August 1970
Sally 17th August 1973
';// To send HTML mail, the Content-type header must be set$headers = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// Additional headers$headers .= 'To: Mary
, Kelly
' . "\r\n";$headers .= 'From: Birthday Reminder
' . "\r\n";$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";// Mail itmail($to, $subject, $message, $headers);

For more details, check php function. 有关更多详细信息,请检查php 功能。


#3楼

You could also use PHPMailer class at . 您也可以在使用PHPMailer类。

It allows you to use the mail function or use an smtp server transparently. 它允许您透明地使用邮件功能或使用smtp服务器。 It also handles HTML based emails and attachments so you don't have to write your own implementation. 它还处理基于HTML的电子邮件和附件,因此您不必编写自己的实现。

The class is stable and it is used by many other projects like Drupal, SugarCRM, Yii, and Joomla! 该类是稳定的,它被许多其他项目(例如Drupal,SugarCRM,Yii和Joomla)使用。

Here is an example from the page above: 这是上一页的示例:

isSMTP(); // Set mailer to use SMTP$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers$mail->SMTPAuth = true; // Enable SMTP authentication$mail->Username = 'user@example.com'; // SMTP username$mail->Password = 'secret'; // SMTP password$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted$mail->From = 'from@example.com';$mail->FromName = 'Mailer';$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient$mail->addAddress('ellen@example.com'); // Name is optional$mail->addReplyTo('info@example.com', 'Information');$mail->addCC('cc@example.com');$mail->addBCC('bcc@example.com');$mail->WordWrap = 50; // Set word wrap to 50 characters$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name$mail->isHTML(true); // Set email format to HTML$mail->Subject = 'Here is the subject';$mail->Body = 'This is the HTML message body in bold!';$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo;} else { echo 'Message has been sent';}

#4楼

this is very basic method to send plain text email using mail function. 这是使用邮件功能发送纯文本电子邮件的非常基本的方法。

";mail($to,$subject,$message,$from);

#5楼

You can use a mail web service such as Postmark, Sendgrid etc. 您可以使用邮件网络服务,例如邮戳,Sendgrid等。

Edit: I just use the now. 编辑:我现在只使用 。 I had trouble sending reminder email to my employer's organization due to strict filters. 由于严格的过滤器,我无法向我的雇主组织发送提醒电子邮件。 But Gmail works as long as you don't spam people. 但是,只要您不向他人发送垃圾邮件,Gmail就可以正常工作。


#6楼

Try this: 尝试这个:

转载地址:http://hgcnb.baihongyu.com/

你可能感兴趣的文章