$mail = newNette\Mail\Message;
$mail->setFrom('John
'
)
->addTo('[email protected]')
->addTo('[email protected]')
->setSubject('Order Confirmation')
->setBody("Hello, Your order has been accepted.");
All parameters must be encoded in UTF-8.
In addition to specifying recipients with the addTo() method, you can also specify the recipient of copy with addCc(), or the recipient of blind copy with addBcc(). All these methods, including setFrom(), accepts addressee in three ways:
The body of an email written in HTML is passed using the setHtmlBody() method:
$mail->setHtmlBody('
Hello,
Your order has been accepted.
');
You don't have to create a text alternative, Nette will generate it automatically for you. And if the email does not have a subject set, it will be taken from the </code> element.</p>
<p dir="auto">Images can also be extremely easily inserted into the HTML body of an email. Just pass the path where the images are physically located as the second parameter, and Nette will automatically include them in the email:</p>
<div class="highlight highlight-text-html-php position-relative overflow-auto" data-snippet-clipboard-copy-content="// automatically adds /path/to/images/background.gif to the email
$mail->setHtmlBody(
'<b>Hello</b> <img src="background.gif">',
'/path/to/images'
);">
<pre><span class="pl-c">// automatically adds /path/to/images/background.gif to the email</span>
<span class="pl-s1"><span class="pl-c1">$</span>mail</span>-><span class="pl-en">setHtmlBody</span>(
<span class="pl-s">'<b>Hello</b> <img src="background.gif">'</span>,
<span class="pl-s">'/path/to/images'</span>
);</pre>
</div>
<p dir="auto">The image embedding algorithm supports the following patterns: <code><img src=...></code>, <code><body background=...></code>, <code>url(...)</code> inside the HTML attribute <code>style</code> and special syntax <code>[[...]]</code>.</p>
<p dir="auto">Can sending emails be even easier?</p>
<p dir="auto">Emails are like postcards. Never send passwords or other credentials via email.</p>
<h2 dir="auto"><a id="user-content-attachments" class="anchor" aria-hidden="true" href="#attachments">
<svg class="octicon octicon-link" viewbox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
<path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path>
</svg></a>Attachments</h2>
<p dir="auto">You can, of course, attach attachments to email. Use the <code>addAttachment(string $file, string $content = null, string $contentType = null)</code>.</p>
<div class="highlight highlight-text-html-php position-relative overflow-auto" data-snippet-clipboard-copy-content="// 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!');">
<pre><span class="pl-c">// inserts the file /path/to/example.zip into the email under the name example.zip</span>
<span class="pl-s1"><span class="pl-c1">$</span>mail</span>-><span class="pl-en">addAttachment</span>(<span class="pl-s">'/path/to/example.zip'</span>);
<span class="pl-c">// inserts the file /path/to/example.zip into the email under the name info.zip</span>
<span class="pl-s1"><span class="pl-c1">$</span>mail</span>-><span class="pl-en">addAttachment</span>(<span class="pl-s">'info.zip'</span>, <span class="pl-en">file_get_contents</span>(<span class="pl-s">'/path/to/example.zip'</span>));
<span class="pl-c">// attaches new example.txt file contents "Hello John!"</span>
<span class="pl-s1"><span class="pl-c1">$</span>mail</span>-><span class="pl-en">addAttachment</span>(<span class="pl-s">'example.txt'</span>, <span class="pl-s">'Hello John!'</span>);</pre>
</div>
<h2 dir="auto"><a id="user-content-templates" class="anchor" aria-hidden="true" href="#templates">
<svg class="octicon octicon-link" viewbox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
<path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path>
</svg></a>Templates</h2>
<p dir="auto">If you send HTML emails, it's a great idea to write them in the <a href="https://latte.nette.org" rel="nofollow">Latte</a> template system. How to do it?</p>
<div class="highlight highlight-text-html-php position-relative overflow-auto" data-snippet-clipboard-copy-content="$latte = new Latte\Engine;
$params = [
'orderId' => 123,
];
$mail = new Nette\Mail\Message;
$mail->setFrom('John <john@example.com>')
->addTo('jack@example.com')
->setHtmlBody(
$latte->renderToString('email.latte', $params),
'/path/to/images'
);">
<pre><span class="pl-s1"><span class="pl-c1">$</span>latte</span> = <span class="pl-k">new</span> <span class="pl-v">Latte</span>\<span class="pl-v">Engine</span>;
<span class="pl-s1"><span class="pl-c1">$</span>params</span> = [
<span class="pl-s">'orderId'</span> => <span class="pl-c1">123</span>,
];
<span class="pl-s1"><span class="pl-c1">$</span>mail</span> = <span class="pl-k">new</span> <span class="pl-v">Nette</span>\<span class="pl-v">Mail</span>\<span class="pl-v">Message</span>;
<span class="pl-s1"><span class="pl-c1">$</span>mail</span>-><span class="pl-en">setFrom</span>(<span class="pl-s">'John <john@example.com>'</span>)
-><span class="pl-en">addTo</span>(<span class="pl-s">'jack@example.com'</span>)
-><span class="pl-en">setHtmlBody</span>(
<span class="pl-s1"><span class="pl-c1">$</span>latte</span>-><span class="pl-en">renderToString</span>(<span class="pl-s">'email.latte'</span>, <span class="pl-s1"><span class="pl-c1">$</span>params</span>),
<span class="pl-s">'/path/to/images'</span>
);</pre>
</div>
<p dir="auto">File <code>email.latte</code>:</p>
<div class="highlight highlight-text-html-basic position-relative overflow-auto" data-snippet-clipboard-copy-content="<html>
<head>
<meta charset="utf-8">
<title>Order Confirmation
Hello,
Your order number {$orderId} has been accepted.
">
<html><head><metacharset="utf-8"><title>Order Confirmationtitle><style>body {
background:url("background.png")
}
style>head><body><p>Hello,p><p>Your order number {$orderId} has been accepted.p>body>html>
Nette automatically inserts all images, sets the subject according to the </code> element, and generates text alternative for HTML body.</p>
Sending Emails
Mailer is class responsible for sending emails. It implements the Nette\Mail\Mailer interface and several ready-made mailers are available which we will introduce.
SendmailMailer
The default mailer is SendmailMailer which uses PHP function mail(). Example of use:
It does not send email but sends them through a set of mailers. If one mailer fails, it repeats the attempt at the next one. If the last one fails, it starts again from the first one.
Other parameters in the constructor include the number of repeat and waiting time in miliseconds.
DKIM
DKIM (DomainKeys Identified Mail) is a trustworthy email technology that also helps detect spoofed messages. The sent message is signed with the private key of the sender's domain and this signature is stored in the email header. The recipient's server compares this signature with the public key stored in the domain's DNS records. By matching the signature, it is shown that the email actually originated from the sender's domain and that the message was not modified during the transmission of the message.
After upgrading nette/mail to version 3.1.3 from 3.1.2, the e-mails won't send with exception:
Nette\Mail\SmtpException
SMTP server did not accept STARTTLS with error: 503 5.5.2 Send hello first [DB5EUR03FT018.eop-EUR03.prod.protection.outlook.com]
I suspect this pull request https://github.com/nette/mail/pull/67 to be an issue.
If the attachment file name contains UTF8 characters, the header is not RFC 2047 encoded, which causes email to rely on support of SMTPUTF8 extension, making the email undeliverable to destinations servers that don't support it (gmail supports it, but e.g. email.cz does not).
Steps To Reproduce
$mail = new Nette\Mail\Message;
$mail->setFrom('John <[email protected]>')
->addTo('[email protected]')
->setSubject('Kůň v pÅ™Ãloze')
->setBody("Test")
->addAttachment("kůň.txt", "test");
die($mail->generateMessage());
This worked in the previous version, the bug was introduced with this change https://github.com/nette/mail/commit/6f333738075ba9ee152795dcbbd84569127a3182 which fixed #24, in this change, call to encodeHeader() has been removed. This call should be probably returned, but I'm not sure how exactly in order to not introduce back the issue #24.
I know a workaround is not to use utf8 in attachment file name with e.g. Strings::toAscii($filename), but as long as the encoding works well with other headers like Subject or From, I don't see a reason for this header to be an exception.
Gmail pays particularly close attention to Message ID and Received headers. Message IDs that are formed incorrectly (without brackets <> and with wrong domain after @) can make Gmail think you are a spammer.
Link in HTML mail may be important (like a links to verification, unsubscribe or other marketing behavior). Is important do not loss this information on all variants of e-mail message.
With error suppression, i.e. @stream_socket_client(), will have $errno set to 0 and $error being an empty string ''. This is an undocumented behaviour but leads to inconveniences during debug and development.
According to the document and my tests with PHP 7.0, 7.1 and 7.2 this function does not throw.
Removing the @ sign will have the function populates the actual $errno and $error which I believe is in fact the expected result.
No work. i am attempting to send a mail but it give me this:
Fatal error: Uncaught Error: Class 'Nette\Mail\Message' not found in C:\xampp\htdocs\Pasantias pdf\Registro\componentes\correo.php:4 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Pasantias pdf\Registro\componentes\correo.php on line 4
my code is the following:
use Nette\Mail\Message;
$mail = new Message;
$mail->setFrom('Jorge ********@gmail.com')
->addTo('***@gmail.com')
->setSubject('Order Confirmation')
->setBody("Hello, Your order has been accepted.");
$mailer = new Nette\Mail\SmtpMailer([
'host' => 'smtp.gmail.com',
'username' => '*****@gmail.com',
'password' => '***',
'secure' => 'ssl',
'context' => [
'ssl' => [
'capath' => '/path/to/my/trusted/ca/folder',
],
],
]);
$mailer->send($mail);
Previous behaviour was insufficient, you just got name of command which failed. There was no information why. Now you should see the response of SMTP server. For example:
SMTP server did not accept RCPT TO:<[email protected]> with error: 550 5.1.1 User unknown; rejecting
bad match if image name have ( )
example:
<img src="test/test(1).jpg">
some wron on
https://github.com/nette/mail/blob/master/src/Mail/Message.php#L197
Whenever there is a bigger base64 encoded image in E-mail we get this exception:
Steps To Reproduce
It's clear from bug description
Additional
I'm not sure how to even solve this, avoiding Regex if possible would maybe be best since we never know how big e-mail can be and with base64 encoded images this can become huge quickly...
I know I can set bigger limit, but then again how much memory will it use and when will we hit it again...
Probably only proper solution here would be to add image as attachment and not base64 encoded...
I've managed to avoid issue in this particular mail since we're not using any images so I just set basePath as null so it doesn't go into this part of code, but obviously that's not a solution...
Intended to periodically test the other mailers.
I'm not sure whether default $shuffleProbability should be 0.0 or sth like 0.001 (would be a minor BC break).
variable $mail is not automatically passed to templates, you have to do it yourself (BC break) (but better than {var $mail->subject = "Your new order"} is this <title>Your new order</title>, isn't it?)
if you have linked images (with relative paths) in template, pass base file path to images as second parameter to setHtmlBody()
SendPortal includes subscriber and list management, email campaigns, message tracking, reports and multiple workspaces/domains in a modern, flexible and scalable application.
Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app. This enables your app to not only respond to new emails but also allows it to read and parse existing mails and much more.
This module provides an easy and flexible way to send emails from Mezzio and Laminas MVC applications (formerly known as Zend Expressive and Zend MVC). It allows you to pre-configure emails and transports, and then send those emails at runtime.
PHPMailer – A full-featured email creation and transfer class for PHP Features Probably the world's most popular code for sending email from PHP! Used