Sending raw email header with SwiftMailer - php

What I want is to send raw header using SwiftMailer
Currently this is what I can do with SwiftMailer
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.");
$message->setFrom("account#bank.com", "Your bank");
Using $message->setFrom() I can set the From field in the header but what I want is something like this
$rawHeader = 'MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="_=_swift_v4_1467746603_bb0677c43b2ba275b8602c907ef7228a_=_"
List-Unsubscribe: <http://go.proctorgallagher.com/unsubscribe/u/45802/41c5731bb75f7959e5880279725cb9b7b4f8996214976c36a0f93e36e88e7705/876424750>
X-Report-Abuse-To: abuse#pd25.com
x-job: 7215816_12853
--_=_swift_v4_1467746603_bb0677c43b2ba275b8602c907ef7228a_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable'
$message->rawHeader($rawHeader);
Or something where I don't have to let the swiftmailer know that Content-Type is field and value is text/plain
I mean whatever I put get sent as it is don't have to put everything separately.

Have you tried:
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.", "text/plain", "utf-8");

Related

Prepare email body for PHPMailer

I stuck in a problem using 1C-Bitrix framework with PHPMailer. The problem connected with the email body.
1C-Bitrix framework prepares body depending on options enabled. For example, if I turn on the option "Create text vesion of html-email" the body will look like this:
---------alt8045b59706
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This is my email
---------alt8045b59706
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<h1> This is my email </h1>
---------alt8045b59706--
If I the email template will have an attached txt file, the body will look like this:
---------mix4215b5973f
Content-Type: multipart/alternative; boundary="-------alt3795b5973f"
---------alt3795b5973f
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This is my email
---------alt3795b5973f
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<h1> This is my email </h1>
---------alt3795b5973f--
---------mix4215b5973f
Content-Type: text/plain; name="=?UTF-8?B?dGVzdC50eHQ=?="
Content-Transfer-Encoding: base64
Content-ID: <427605>
dGVzdCB0ZXh0Cg==
---------mix4215b5973f--
And, finally, the standard email body will look like this:
<h1> This is my email </h1>
I can't just feed such email body to PHPMailer. It doesn't work correctly. I tried msgHtml() - also doesn't work.
But standard php mail() func works well with such email body.
I found that in PHPMailer I need to set Body and AltBody separately. But then, what is the best way for me to parse such email body.
Maybe somebody knows a tool for it?
Or PHPMailer has a built in methods for it?
Maybe I need to just manually set correct "Content-Type" header (framework tells me correct Content-Type)
Thanks, in advance.
You don't need to think about those things - PHPMailer does it for you. This is all you need to do to make that structure:
$mail->isHTML();
$mail->Body = '<h1> This is my email </h1>';
$mail->AltBody = 'This is my email';
$mail->addAttachment('path/to/file.pdf');
Also, the MIME structure in the example you gave for the message body with an attachment is incorrect, so I wouldn't expect it to work in any context.

unable to send email as both plain text and html in cakephp

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->to($emailContentArray['To']);
$Email->from(array($emailContentArray['From'] => Configure::read('FROM_NAME')));
$Email->subject($emailContentArray['Subject']);
$Email->emailFormat('both');
$response=$Email->send($emailContentArray['Body']);
if we check the resulting email it looks like:-
Content-Type: multipart/mixed; boundary="782f009f669cbcf2faafff59fe0eeb5d"
Content-Transfer-Encoding: 8bit
X-Identified-User: {:test25.xyzzzz.com:testttt.com} {sentby:program running on server}
--782f009f669cbcf2faafff59fe0eeb5d
Content-Type: multipart/alternative; boundary="alt-782f009f669cbcf2faafff59fe0eeb5d"
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
In text/plain the email should not show the html tags - how can I get both html and plain text to be sent correctly?
Encode the content and the subject in base64:
"=?utf-8?b?".base64_encode($s)."?=";
And before sending the emails don't forget to set the header:
'Content-Transfer-Encoding: base64';
If I'm not wrong you can make to set header: $Email->setHeader('X-Content-Transfer-Encoding', 'base64');
You can take a look at Cakephp sending UTF-8 Emails and lineLength
And at http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

imap_fetchbody can't handle mails sent with phpmailer?

I am sending some emails with phpmailer with the regular example on their website.
the email is sent, and it arrives perfectly to the sent address.
after the mail is sent, I am saving the email to my sent items folder with imap_append, using the info of this answer Sent mails with phpmailer don't go to "Sent" IMAP folder
and in third place, I am creating a custom made webmail which reads my imap folders.
the problem is that I can't get the html body of the email sent correctly.
When I open the mail on sent items on thunderbird, or my phone, the body is shown perfectly.
but when I try to get the html body with php_imap functions I can't make it!!.
I created an isolated file to fetch the body of the msg like this:
$body = imap_fetchbody($conn,3392,1,FT_UID);
that returns this:
--b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ REMOVED CONTENT --------------------- klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ
REMOVED CONTENT
---------------------
klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b--
If I try
$body = imap_fetchbody($conn,3392,1.2,FT_UID);
don't get anything
If I try
$body = imap_fetchbody($conn,3392,2,FT_UID);
don't get anything, it seems that PHPMailer creating the emailbody in a way that imap_fetchbody can't handle?
thank you

Zend Mail Imap: Fetch Body of Multipart?

I have a small "problem" with Zend_Mail_Storage_Imap and MultiPart Mails.
ContentType: multipart/alternative;
boundary=f46d043bd88a9f5d9004c87d2ad3
Part 1 has the Text of the Message, but with headers inside the content, so when i try
$part->getContent();
--f46d043bd88a9f5d9004c87d2ad3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hallo
is the result, how to extract the header information now?
Instead of:
$part->getContent();
do:
$part->getHeader();
to get Headers or if you want ONLY content then:
try:
while ($part->isMultipart()) {
$part = $message->getPart(1);
}
or
$message = $mail->getMessage($messageNum);
for content without body.
If issue persists, check your mail on mail client's like Gmail. Sometimes, we add header information twice (accidentally) whilst sending the mail.
Hope this helps :)

PHP mail() specific question

So, I would like to use mail() to send registration emails for my website, however I'd like to make it look nice while falling back to good old plaintext when necessary; a mixed message email.
However I would like it to be sent from John Doe who's email is johndoe#example.com to recipient#example.com.
The HTML code should be <html><head><title>HTML email!</title></head><body><p>This is HTML!</p></body</html> and the plaintext message should be This is plaintext.
What would be the arguments to mail() to accomplish this? I know a lot of it deals with changing the header in some crazy way.
Thanks so much!
Use something like SwiftMailer instead, as it has nice things like header injection prevention. With that in mind, yes, you have to set custom headers and use a multi-part body to achieve what you want:
/***************************************************************
Creating Email: Headers, BODY
1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
***************************************************************/
#---->Headers Part
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
AKAM;
#---->BODY Part
$Body =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
This is a multi-part message in MIME format.
--$boundary1
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary1
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary1--
AKAM;
Source: http://www.php.net/manual/en/function.mail.php#83491
This is a lot of work. Which, once again, is why I recommend having a library that can handle all of this for you, plus other features.
Everything is here (Example #4): http://php.net/manual/en/function.mail.php

Categories