PHP code example of nette / mail

1. Go to this page and download the library: Download nette/mail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nette / mail example snippets


$mail = new Nette\Mail\Message;
$mail->setFrom('John <[email protected]>')
	->addTo('[email protected]')
	->addTo('[email protected]')
	->setSubject('Order Confirmation')
	->setBody("Hello, Your order has been accepted.");

$mail->setFrom('[email protected]');
$mail->setFrom('[email protected]', 'John Doe');
$mail->setFrom('John Doe <[email protected]>');

$mail->setHtmlBody('<p>Hello,</p><p>Your order has been accepted.</p>');

// automatically adds /path/to/images/background.gif to the email
$mail->setHtmlBody(
	'<b>Hello</b> <img src="background.gif">',
	'/path/to/images',
);

// inserts the file /path/to/example.zip into the email under the name example.zip
$mail->addAttachment('/path/to/example.zip');

// inserts the file /path/to/example.zip into the email under the name info.zip
$mail->addAttachment('info.zip', file_get_contents('/path/to/example.zip'));

// attaches new example.txt file contents "Hello John!"
$mail->addAttachment('example.txt', 'Hello John!');

$latte = new Latte\Engine;
$params = [
	'orderId' => 123,
];

$mail = new Nette\Mail\Message;
$mail->setFrom('John <[email protected]>')
	->addTo('[email protected]')
	->setHtmlBody(
		$latte->renderToString('/path/to/email.latte', $params),
		'/path/to/images',
	);

$mailer = new Nette\Mail\SendmailMailer;
$mailer->send($mail);

$mailer = new Nette\Mail\SmtpMailer(
	host: 'smtp.gmail.com',
	username: '[email protected]',
	password: '*****',
	encryption: Nette\Mail\SmtpMailer::EncryptionSSL,
);
$mailer->send($mail);

$mailer = new Nette\Mail\FallbackMailer([
	$smtpMailer,
	$backupSmtpMailer,
	$sendmailMailer,
]);
$mailer->send($mail);

$signer = new Nette\Mail\DkimSigner(
	domain: 'nette.org',
	selector: 'dkim',
	privateKey: file_get_contents('../dkim/dkim.key'),
	passPhrase: '****',
);

$mailer = new Nette\Mail\SendmailMailer; // or SmtpMailer
$mailer->setSigner($signer);
$mailer->send($mail);