$email->setFrom('your@example.com', 'Your Name'); $email->setTo('someone@example.com'); $email->setCC('another@another-example.com'); $email->setBCC('them@their-example.com'); $email->setSubject('Email Test'); $email->setMessage('Testing the email class.'); $email->send();

设置电子邮件首选项

有21种不同的首选项可用于调整电子邮件的发送方式。您可以按照此处所述手动设置它们,也可以通过存储在配置文件中的偏好设置自动设置它们,如下所述:

通过将一组首选项值传递给电子邮件初始化方法来设置首选项。这是您如何设置某些首选项的示例:

$config['protocol'] = 'sendmail';
$config['mailPath'] = '/usr/sbin/sendmail';
$config['charset']  = 'iso-8859-1';
$config['wordWrap'] = true;
$email->initialize($config);

大多数首选项都具有默认值,如果您未设置它们,则将使用它们。

在配置文件中设置电子邮件首选项

如果您不想使用上述方法来设置首选项,则可以将其放入配置文件中。只需打开 app / Config / Email.php 文件,然后在“电子邮件”属性中设置您的配置。然后保存文件,它将自动使用。 $email->initialize() 如果您在配置文件中设置了首选项,则无需使用该方法。

电子邮件首选项

以下是发送电子邮件时可以设置的所有首选项的列表。

覆盖单词包装

如果您启用了自动换行(建议遵循RFC 822),并且电子邮件中的链接很长,则该链接也可能被自动换行,从而导致收信人无法点击它。使用CodeIgniter,您可以手动覆盖部分消息中的自动换行,如下所示:

The text of your email that
gets wrapped normally.
{unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
More text that will be
wrapped normally.
将您不想用文字包装的物品放在以下位置:{unwrap} {/unwrap}

CodeIgniter\Email\Email
setFrom $ from [ , $name = '' [ $ returnPath = null ] ] )

$from ( string ) – “From” e-mail address 您还可以设置返回路径,以帮助重定向未送达的邮件:

$email->setFrom('you@example.com', 'Your Name', 'returned_emails@example.com');

如果您已将“ smtp”配置为协议,则无法使用Return-Path。

setReplyTo ( $replyto [ , $name = '' ])

$replyto ( string ) – E-mail address for replies
$email->setTo('someone@example.com');
$email->setTo('one@example.com, two@example.com, three@example.com');
$email->setTo(['one@example.com', 'two@example.com', 'three@example.com']);
setCC ( $cc )

$cc ( mixed ) – Comma-delimited string or an array of e-mail addresses 设置密件抄送电子邮件地址。 就像 setTo() 方法一样,它可以是一封电子邮件,一个逗号分隔的列表或一个数组。

如果设置了 $limit ,将启用“批处理模式”,它将以批次发送电子邮件,每个批次不超过指定的 $limit

setSubject ( $subject )

$subject ( string ) – E-mail subject line 将所有电子邮件变量初始化为空状态。 如果您循环运行电子邮件发送方法,则允许使用此方法,以便在两次循环之间重置数据。

foreach ($list as $name => $address)
        $email->clear();
        $email->setTo($address);
        $email->setFrom('your@example.com');
        $email->setSubject('Here is your info '.$name);
        $email->setMessage('Hi ' . $name . ' Here is the info you requested.');
        $email->send();
如果将参数设置为TRUE,则所有附件也会被清除:

$email->clear(true);
send($autoClear = true)

$autoClear (bool) – Whether to clear message data automatically

$email->attach('/path/to/photo1.jpg');
$email->attach('/path/to/photo2.jpg');
$email->attach('/path/to/photo3.jpg');
要使用默认处置方式(附件),请将第二个参数留空,否则请使用自定义处置方式:

$email->attach('image.jpg', 'inline');
您也可以使用以下网址:

$email->attach('http://example.com/filename.pdf');
如果您想使用自定义文件名,则可以使用第三个参数:

$email->attach('filename.pdf', 'attachment', 'report.pdf');
如果需要使用缓冲字符串而不是实际文件,则可以将第一个参数用作缓冲区,将第三个参数用作文件名,将第四个参数用作mime-type:

$email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
setAttachmentCID($filename)

$filename (string) – Existing attachment filename $email->setTo($address); $cid = $email->setAttachmentCID($filename); $email->setMessage('<img src="cid:'. $cid .'" alt="photo1" />'); $email->send();
必须重新创建每个电子邮件的Content-ID,以使其唯一。

返回一个包含任何服务器消息,电子邮件标题和电子邮件消息的字符串。 对调试有用。

您可以选择指定应打印消息的哪些部分。 有效选项包括:headers, subject, body

// You need to pass FALSE while sending in order for the email data
// to not be cleared - if that happens, printDebugger() would have
// nothing to output.
$email->send(false);
// Will only print the email headers, excluding the message subject and body
$email->printDebugger(['headers']);

默认情况下,将打印所有原始数据。