SwiftMailer - php attach txt file in mail - php

I am trying to put a part with text attachment into mail in my Symfony project.
None of examples I tried works.
On dump(), everything is returning what it should.
Where is the catch?
$projectDir = $this->container->get('kernel')->getProjectDir();
$file = $projectDir . '/web/uploads';
// Send email
$message = $this->container->get('mail')->createTemplatedEmail(
$user->getEmail(),
('Your Text File has arrived!'),
'#WebsiteTemplates/member/view.html.twig',
[
'user' => $user,
]
);
$message->attach(\Swift_Attachment::fromPath($file . '/my-text-file.txt'));
$this->container->get('mail')->sendMessage($message, true);
return;
I know I need to pass it as array for my twig file to work like:
Hello {{ user.email }}!
Here is your Text file: {{ txtFile }}<br>
It returns a file in a email like:
Content-Type: text/x-vcard; name=dmchenry-svb-com.vcf Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=dmchenry-svb-com.vcf QkVHSU46VkNBUkQNClZFUlNJT046My4wDQpSRVY6MjAxOS0wMy0xMVQxNToxODozOVoNCk47Q0hB

Related

PHP MailSo library and attachments

I'm trying to use MailSo library to create MIME message. I have got to the point where next step is to process attachments. Everything is fine when attachments are binary files but when I try to to add plain text attachment as follow
$rResource = "Some plain text goes here";
$sFileName = 'text.txt';
$iFileSize = \strlen("Some plain text goes here");
$bIsInline = false;
$bIsLinked = false;
$sCID = $metadataCID;
$aCustomContentTypeParams = array(\MailSo\Base\Enumerations\Encoding::QUOTED_PRINTABLE_LOWER);
$oMessage->Attachments()->Add(
\MailSo\Mime\Attachment::NewInstance(
$rResource,
$sFileName,
$iFileSize,
$bIsInline,
$bIsLinked,
$sCID,
$aCustomContentTypeParams
)
);
I expect to see that attachment as
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=text.txt
but it always forcing to base64 neither adding charset to content-type part as
Content-Type: text/plain; name="text.txt"
Content-Disposition: attachment; filename="text.txt"
Content-Transfer-Encoding: base64
Any tips on that?
It looks like MailSo library treats all attachments except message/rfc822 as binaries. It will require to rewrite or extend createNewMessageAttachmentBody() private function.

Mail Mime image in email body not showing up using MailMime addHtml(...)

Mail_Mime 1.8.9
Mail 1.2.0
php 5.4.12
project = run from my localhost dev computer (LAN).
Hello, I am attempting to add an image to my email I am sending out using MailMime. The image never loads. I'll go through the steps below:
Here I am stripping everything off of the image but the image name and extension (ie:noimage.jpg). I then create a link for non image related purposes, I then create the image tag using the stripped image name and extension.
$url = substr($baseImage, 36);
$domainName = DomainNameUtil::getWebSiteDomainName();
$projectPath = DomainNameUtil::getPathToProject();
$productUrl = $domainName . $projectPath . "/client/app/#/products/" . $product->id;
$productLink = "<a href='$productUrl'>$product->name</a>";
$string = "<img src='$url' /><br />";
Here I am sending the message
$mailConfig = MailConfigurationUtil::getMailConfigurationData();
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$crlf = "\n";
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($body);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array (
'host' => $mailConfig->mailServer,
'port' => $mailConfig->port,
'auth' => true,
'username' => $mailConfig->userName,
'password' => $mailConfig->password
));
$productManager = new ProductManager();
$ordersManager = new OrdersManager();
$orderVirtualItem = $ordersManager->getOrderVirtualItemByBoxId($boxId);
$product = $productManager->getProductById($orderVirtualItem->itemId);
$url = $product->baseImage;
$domain = DomainNameUtil::getWebSiteDomainName();
$path = DomainNameUtil::getPathToProject();
$r = substr($url, 5);
$finalUrl = $domain . $path . $r;
$mime->addHTMLImage(file_get_contents($finalUrl),'image/jpeg',basename("noimage"),false, "blackstone");
$body = $mime->get();
$mail = $smtp->send($to, $headers, $body);
In the example above, I know the image is called noimage.jpeg, so in the addHtmlImage above I simply stated the name. I am under the impression that the name used in addHtmlImage has to be the same name as the image name in the image src tag in the html body.
When I actually send my email I get the email and I get some text saying there is an image, but the image tag in my email body does not load.
Also, looking at it under firebug it has a proxy attached to the image src. Also it references it as PROXYADDRESS#http://noimage.jpg
Anyone have experience in this that can help me understand why the image is not loading?
For reference, I saw someone posting this method online and tried copying them. before this I had even tried putting the whole url in the image src and not adding the image to MIME, and it did not work.
i got pretty similar problem here.
What i got so far: it depends on phpversion (and installed Mail_Mime).
Mail_Mime seems to be "broken" under php 5.4.30
If i run my script under php 5.2.17 all images show up correctly
If i run my script under php 5.4.30 all images do not show up correctly
when i compare both sourcecodes, it seems, Mail_Mime under 5.4.30 gets messy with the order of boundarys and the Content-Type:
Content-Type: multipart/alternative; in 5.2.17
Content-Type: multipart/related; in 5.4.30enter image description here
When specifying a context-id (the last parameter to addHtmlImage()) you must reference the image in the html message correctly: <img src="cid:context-id#local" />. Of course replacing context id with your own context id for each image to show.

Swift Mailer Dynamic Text Attachement

I'm trying to attach a string as a plain text file to the swift mailer message as explained in the doc:
$debug_data = 'Exception: ' . $e_message . PHP_EOL;
$debug_data .= $file . ': ' . $line . PHP_EOL;
$debug_data .= $trace;
$attach = Swift_Attachment::newInstance($debug_data, 'debug.txt', 'text/plain');
$message->attach($attach);
but this gives me this error:
Error in exception handler: fopen(Content-Type: text/plain; name=debug.txt Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=debug.txt 1ay9wdWJsaWMvaW5kZXgucGhwKDQ5KTog SWxsdW1pbmF0ZVxGb3VuZGF0aW9uXEFwcGxpY2F0aW9uLT5ydW4oKQojMTMge21haW59): failed to open stream: No such file or directory in /vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php:138
any idea?!
It seems you are trying to use it in Laravel that has a attachData method which does'n call fopen. You can use it like below code
$message->attachData($debug_data, 'debug.txt');
Both answers above are not compatible with Symfony 4.0.
For Symfony 4.0:
$attachment = new Swift_Attachment($debug_data, 'debug.txt', 'text/plain');
$message->attach($attachment);
// I post it here, because it's top google result for 'swiftmailer attachment from string'
try something like this:
$attachment = Swift_Attachment::newInstance()
->setFilename('debug.txt')
->setContentType('text/plain')
->setBody($debug_data);

Drafting IMAP emails with an attachment using PHP

I'm trying to add an attachment to emails that I draft but cannot seem to get it to work. I've tried to follow the examples here and here but without success.
Here's what I'm able to do so far:
Connect to the Exchange server
Open the mailbox
Draft an html email
Append the email to the mailbox and have it render the html correctly. (when using text/html as the content-type). Using anything else displays the html as plaintext.
As an additional note, after being drafted, the emails are appended to a mailbox on an Exchange 2010 server and are viewed then sent via Outlook 2010.
Below is my mailer class and the code to draft the email.
mailer.php
<?php
class mailer
{
const USER = 'user';
const PASSWORD = 'pass';
const MAILBOX = '{conn}DRAFTS';
// STRING ORDER: $content-type . $from . $to . $cc . $subject . "\r\n\r\n" . $message
public $message;
public $imap_conn;
public $boundary;
function __construct()
{
// Open the message property so we can start appending strings.
$this->message = '';
// Open the IMAP connection
$this->imap_conn = imap_open(self::MAILBOX,self::USER,self::PASSWORD);
}
public function content_type($string_type)
{
$this->message .= "Content-type:{$string_type}\r\n";
}
public function from($string_from)
{
$this->message .= "From:{$string_from}\r\n";
}
public function to($string_to)
{
$this->message .= "To:{$string_to}\r\n";
}
public function cc($string_cc)
{
$this->message .= "Cc:{$string_cc}\r\n";
}
public function mime($float_mime_version)
{
$this->message .= "MIME-Version:{$float_mime_version}\r\n";
}
public function subject($string_subject)
{
$this->message .= "Subject:{$string_subject}\r\n\r\n";
}
public function message($string_message)
{
$this->message .= "{$string_message}\r\n";
}
public function set_boundary($string_boundary)
{
$this->boundary = $string_boundary;
}
public function append()
{
imap_append($this->imap_conn,self::MAILBOX,$this->message,"\\Draft");
imap_close($this->imap_conn);
}
}
?>
Draft code
// A random hash used for the boundary
$rh = md5(date('c',time()));
$data = chunk_split(base64_encode('Testing'));
$m = new mailer;
$m->set_boundary('--PHP-mixed-' . $rh);
$m->content_type('multipart/mixed; boundary="' . $m->boundary . '"');
$m->mime('1.0');
$m->from('from#mail.com');
$m->to('to#mail.com');
$m->cc('cc1#mail.com,cc2#mail.com');
$m->subject('A new email');
$m->message("
{$m->boundary}
Content-Type: text/html; charset=\"utf-8\"
Content-Transfer-Encoding: base64
Testing my <b>HTML</b>
</br>
{$m->boundary}
Content-Type: application/octet-stream; name=\"test.txt\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"test.txt\"
{$data}
{$m->boundary}--
"));
$m->append();
The message before its appended
Content-type:multipart/mixed; boundary="--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f"
MIME-Version:1.0
From:from#mail.com
To:to#mail.com
Cc:cc1#mail.com
Subject:New Message
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: "base64"
My <b>html</b> content
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f--
Question: How do I add an attachment to an email drafted with PHP via IMAP?
Answer: In short, the issue was in two places.
Improper placement of newlines (\r\n). A single newline should be after each header and two newlines after each ending header of a section. See Figure 1.
Improper boundaries. Boundaries should start with -- followed by a unique string. The ending boundary of the message should begin and end with -- See Figure 2
Figure 1
Content-Type: text/html\r\n
Content-Encoding: base64\r\n\r\n
"Message text here."\r\n
Figure 2
--unique
Content-Type: text/html\r\n
Content-Encoding: base64\r\n\r\n
HTML stuff here.\r\n
--unique
Content-Type: application/octet-stream\r\n
Content-Disposition: attachment; filename=\"logs.csv\"\r\n
Content-Transfer-Encoding: base64\r\n\r\n
Attachment(s)\r\n
--unique--
I plan on doing a more in depth write-up on this as it took me a very long time to get it right.
It would be helpful to have some more information on your debugging so far. Is the file being read successfully? Does anything make it to the mailbox? It would also help me to see an output of the full message before your final append.
I do not know if this will solve it, but from my successful code and your second example, your message should have an extra filename property, and the values wrapped in quotes.
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"

Swift Mailer attachments

I'm creating a CSV on the fly with PHP, I then need to attach this CSV file to the the Swift Mailer Message. I have tried using file_get_content on the created file aswell as using chunk_split(base64_encode(file_get_contents()) on the created file aswell as attaching the file before writing it to disk. Without writing to disk I get Rescource #183 in the CSV, with attaching it with file_get_content I get just a string in each row of the CSV file, anyone know what I'm doing wrong?
if(!file_exists(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv'))
{
if($file = fopen (_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'x+'))
{
foreach ($list as $fields)
{
fputcsv($file, $fields);
}
$attachment['mime'] = 'application/vnd.ms-excel';
$attachment['content'] = file_get_contents($file);
$attachment['name'] = $order.'order';
EDIT
Mail::Send(1, 'order_conf', 'Order CSV Attachment', $success, 'test#email.com', Address, NULL, NULL, $attachment); // attach and send
}
}
Attaching a file into a swift mailer:
$swift =& new Swift(new Swift_Connection_SMTP(MAIL_SMTP_URL, MAIL_SMTP_PORT));
$message =& new Swift_Message($subject);
$recpients =& new Swift_RecipientList();
$sender =& new Swift_Address('info#example.com', 'example.com');
$recpients->addTo('info#example.com', 'www.example.com');
$message->attach(new Swift_Message_Part('this is my body'));
$message->attach(new Swift_Message_Attachment($binarycontents, $fileTitle, $mimeType));
$swift->send($message, $recpients, $sender);
in your case the attaching would be:
$message->attach(new Swift_Message_Attachment(file_get_contents($file), $order.'order.csv', 'application/vnd.ms-excel'));
just for example ofcourse :)
//to set headers of csv file(first row)
$content = "user_id,first_name,last_name,phone,gender,pan_number\r\n";
//this can be dynamic data from database or from anywhere can loop this for multiple rows
$content .= "1,test_fn,test_ln,8888999900,M,ASDD3333\r\n";
//include swiftmailer library in order to run the below code
Yii::$app->mailer->compose()
->setFrom(array('test#test.com'=>'test'))
->setTo('testto#testto.com')
->setSubject('your subject' )
->setHtmlBody('<table><tr><td>your email body message here</td></tr> </table>')
->attachContent($content, ['fileName' => 'user.csv', 'contentType' => 'application/vnd.ms-excel'])->send();

Categories