How to save an Email attachment as an Email message? - php

I am pretty new when it comes to handling emails in php.
Basically I need to read a pop3 mailbox and save the messages to a database and the attachments to a folder. Then mail that same mail (slightly altered) back to another email recipient.
I thought I got this all working, then I realized when the attachment is another mail message I only receive the body as text (after base64_decode).
Is it possible to save an attached email to a file and then send it as attachment in another message?
Any help will be appreciated.
Here is some code to put it into perspective :
// This is the IMAP connection: imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass,OP_SILENT);
$mailConnection = $this->Maildata->mail_login($host,$port,$user,$pass,$folder,$ssl);
// This retrieves the list of mails in the current folder
$messageList = $this->Maildata->mail_list($mailConnection,$message);
foreach ($messageList as $messageId => $messageListData) {
// This reads the mail object into an array
$mailMessage = $this->Maildata->mail_mime_to_array($mailConnection,$messageId,true);
// This loops through each mail
foreach ($mailMessage as $tmpId => $tmpData) {
// Excluded save to database since it is unrelated
// This checks if item is an attachment and saves it.
if (isset($tmpData['is_attachment']) && $tmpData['is_attachment'] === true) {
$tmpName = date('Y-m-d H:i:s', strtotime('now'));
$this->Maildata->mail_attachment_save($tmpData['data'], $tmpName .$tmpData['name']);
}
}
}
P.S. This all works, but imap_fetchbody($mailConnection , $messageId, $prefix); doesn't seem to send all the data I need.

Yes.
You can save attached mail messages as a file, but it will not be in a format that Microsoft Office Outlook will understand. Interestingly enough if you save the file as a .eml then windows live mail will handle the message perfectly.
No webmail service handles attached mail items as outlook and windlows live mail does and gmail ignores them completely.

Related

PHPMailer Sending Body As Text Attachment

I'm trying to set up a contact form for a client who wish to link the data from the form into their Goldmine system.
There is a 3rd party company who have sent me a sample contact form and a sample PHPMailer script, but when I test this script on the client's website it sends out the email, but as an attachment rather than in the body of the email.
I've looked at the PHPMailer script and I cannot see anywhere that references adding attachments so I'm puzzled as to whether there is something in the PHPMailer script I've not seen, or if this is some sort of server setting when the email is being sent? Any help would be appreciated.
The PHPMailer script has $Body which it just keeps adding each part of the form input to and then finally ends with what looks like the relevant bit of code to send the email:
ini_set("SMTP", Decode($SMTP));
if(mail(Decode($SendToEmail), $ToSubject, $Body, "From: GoldMine WebImport <no-reply#xxxxxxx>\r\nContent-Type: text/x-gm-impdata\r\n" )) {
print "Your data has been recorded successfully!\n\n<!--\n".Decode($SendToEmail)."\n".$ToSubject."\n".$Body."\n\n".$OutputAs."\n\n-->";
}
else
{print("There was a mailer failure.\n\n<!--\n".$Body."\n\n-->");}
}
else
{echo "There was no form data passed.";}
EDIT: I should also mention that I've been using Tectite's Formmail system for the same client on their existing contact forms and that sends the email with the content in the main body of the email as it is supposed to. I only seem to be having this problem with PHPMailer, but I can't use Formmail for sending the right info to the Goldmine system.

Sending email with signature using Gmail API

I got the Gmail Rest API sending part working but the e-mail doesn't include the signature and in recipient's inbox the 'from' label is sender's user id not the name of the user.
This is in php.
$mime = new Mail_mime();
$mime->setFrom("ABCD"); //This doesn't work....
$mime->setSubject('Testing');
$mime->setTXTBody('This is a demo mail');
$mime->addTo('a#a.com');
$message_body = $mime->getMessage();
$encodeMessage = base64url_encode($message_body);
$message = new Google_Service_Gmail_Message();
$message->setRaw($encodeMessage);
$send = $service->users_messages->send('me', $message);
Is there anyway to include the signature and change the 'from'?
The signature is not added by the API because it is a setting on the web client, not a global setting for the entire account. If you configure your Gmail account on Thunderbird, Outlook or another email client, Gmail will not add the signature either. You should think about Gmail in two separate ways:
The web client interface, accessible at https://mail.google.com, which is just an email client like any other;
Your inbox, the place where messages end up in, which is completely independent of the clients you use to access it.
In other words, this is an email client-dependent setting, and the only thing the clients do is add a bit of text to the text you write yourself, nothing else.
I know this is old but you can retrieve via API the users signature.
https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings
you can then append to your email that you are composing.

Add mail to Send folder with SwiftMailer

I'm using SwiftMailer for PHP from swiftmailer.org
Everything works well but I wonder if there is a way to add the sent message into the sent folder from the mail account that SwiftMailer is sending from?
That's all, have a nice day.
According to the developer, swiftmailer cannot copy to Sent folder because it is a mail sender and not mailbox manager.
As mentioned on the github page:
Swiftmailer is a library to send emails, not to manage mailboxes. So, this is indeed out of the scope of Swiftmailer.
However, someone from php.net posted a solution that might work for you:
Use SwiftMailer to send the message via PHP.
$message = Swift_Message::newInstance("Subject goes here");
// (then add from, to, body, attachments etc)
$result = $mailer->send($message);
When you construct the message in step 1) above save it to a variable as follows:
$msg = $message->toString();
// (this creates the full MIME message required for imap_append()!!
// After this you can call imap_append like this:
imap_append($imap_conn,$mail_box,$msg."\r\n","\\Seen");
I've had similar problem and Sutandiono's answer got me into right direction. However because of completeness and as I had additional problem connecting to an Exchange 2007 server, I wanted to provide a complete snippet for storing message to Sent folder on IMAP:
$msg = $message->toString();
// $message is instance of Swift_Message from SwiftMailer
$stream = imap_open("{mail.XXXXX.org/imap/ssl/novalidate-cert}", "username", "password", null, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
// connect to IMAP SSL (port 993) without Kerberos and no certificate validation
imap_append($stream,"{mail.XXXXX.org/imap/ssl/novalidate-cert}Sent Items",$msg."\r\n","\\Seen");
// Saves message to Sent folder and marks it as read
imap_close($stream);
// Close connection to the server when you're done
Replace server hostname, username and password with your own information.

Sending an email in php - swiftmailer

I've been searching for days if not weeks on how to automatically send a simple e-mail in php after a user completes my form. I've tried Pear, PHP mail, Swiftmailer, changed my php.ini, tried different servers and I'm going mad from none of it working. I have not successfully sent one e-mail yet. I've searched endlessly but I still have no idea what to do and why nothing is working.
At the moment I'm using Swiftmailer (second time round). I set up a test page with code:
<?php
require_once 'swift/lib/swift_required.php';
// CREATE TRANSPORT CONFIG
$transport = Swift_MailTransport::newInstance();
// CREATE MSG
$message = Swift_Message::newInstance();
// SET PRIORITY TO HIGH
$message->setPriority(2);
// SUBJECT
$message->setSubject('Subject');
// FROM
$message->setFrom(array('example#btopenworld.com'));
// TO
$message->setTo(array('example#googlemail.com'));
// EMAIL BODY
$message->setBody('Test');
// SEND
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
?>
Looking at a similar post someone suggested the last part of the code to see errors and my error is:
Failures:Array ( [0] => example#googlemail.com )
No matter what e-mail I change it to (all using e-mails I have, so real e-mails) it doesn't work. If anyone has any help or suggestions it would be hugely appreciated.
example#btopenworld.com
Unless you are using a btopenworld server to send the E-Mail from, this is not going to work. The E-Mails from address needs to be associated with the server you are sending the message from. You can put the BT address into the reply-to header.
Also, SwiftMailer should have a "debug" switch telling you exactly what goes wrong, but I can't find it in the docs right now. Here is a logger plugin for Swiftmailer that should help if all else fails.

email are not shown up as conversion when Parsed with php imap and sending back to sender

hi I'm working with php imap functionality,
I'm parsing mails from my mail box and storing them into mysql,:
i'm storing following information:
From
To
Subject
Content
Time of receiving mail
After one hour I'm sending back this mail. for sending mail i'm using ses mail of amazon.
I create a mail using the saved data and sending them to my gmail again.
But it is not showing up as group or conversion in gmail.
Here is my code for sending mail:
function sendEmail($pendingMail) {
$sesAccUserName = 'noreply#mydomain.com';
$ses = new AmazonSES(array("key" => AWSAccessKeyId, "secret" => AWSSecretKey));
$source = trim($sesAccUserName);
$destination = array('ToAddresses' => array(trim($pendingMail['email_reminder_send_to_address'])));
$messageBody = $pendingMail['email_reminder_content'];
$messageSubject = $pendingMail['email_reminder_subject'];
$messageArr = array('Subject.Data' => 'Fwd: '.$messageSubject, 'Body.Html.Data' => $messageBody);
$rSendEmail = $ses->send_email($source, $destination, $messageArr);
if ($rSendEmail->status == 200) {
changeStatus($pendingMail['email_reminder_id'], 'completed');
} else {
print_r($rSendEmail);
}
}
Mail sent successfully but it is not grouped with the original message?
I think i googled a lot but not find anything useful..
I'm at beginner level about emails...
will you help me to solve this issue...
Thank you !!!!!!!!!!
I research some gmail inbox mails and found that each mail have a message-id in header,
When we reply to mail the value of message-id is set to a header field In Reply To: of the reply mail (the mail we are sending in reply).
This message-id and In Reply To are responsible for grouping of message in gmail...

Categories