EWS php e-mail set sender name - php

I send e-mails with ews-php library (exchange 2013) and try to set custom sender's name.
I use the code from the official api: http://jamesarmes.com/php-ews/doc/0.1/examples/email-send.html
$fromAddress = new EWSType_EmailAddressType();
$fromAddress->EmailAddress = 'email#server.de';
$fromAddress->Name = 'From Name';
$msg->From = new EWSType_SingleRecipientType();
$msg->From->Mailbox = $fromAddress;
So it should look like "From Name <email#server.de>". Instead it's "Email <email#server.de>"
Any ideas what went wrong?

Related

How to append thread messages while reply so that new user can see previous conversation in Gmail Api

I want to send reply message from Gmail Api and it is going fine and the message is threaded or appended to the reciever mailbox (A & B user). But If I add new CC user (we name as C) then the new User should see threaded messages which was previously communicated between A & B.
Please help me out if anybody know the solution
<?php
$client = getClient();
$gmail = new Google_Service_Gmail($client);
$message = new Google_Service_Gmail_Message();
$optParam = array();
$referenceId = '';
$thread = $gmail->users_threads->get('someid#gmail.com', $threadId);
$optParam['threadId'] = '16c632fd24536690';
$threadMessages = $thread->getMessages($optParam);
$messageId = $threadMessages[0]->getId();
$subject = "Re: Thread mail test";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->addCustomHeader('In-Reply-To',
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg#mail.gmail.com>');
$mail->addCustomHeader('References',
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg#mail.gmail.com>');
$mail->addAddress($to);
$mail->addcc($cc);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$message->setRaw($raw);
$message->setThreadId($threadId);
$response = $gmail->users_messages->send($userId, $message);
?>
Using Google Apps Script instead of Gmail API
would allow you to use message.forward which would send all the messages of a thread. You could implement it by
List all thread messages with thread.getMessages()
Get the last thread message with threadMessages[threadMessages.length-1]
Forward the last thread message to desired recipients (A, B and C) with message.forward()
If you want to stick to Gmail API, a workaround I can think of would imply to
get all thread messages
Get their raw contents
Append the raw contents of each thread message to the body of the message to send
An example:
function myFunction() {
var myThread = GmailApp.getThreadById("PASTE HERE THE THREAD ID");
var threadMessages = MyThread.getMessages();
var lastMessage = threadMessages[threadMessages.length-1];
lastMessage.forward("emailA", "emailB", "emailC");
}
The Simple way to send the Message in thread is as
1. Message Body should be like this
2. Convert message to base64 encode.
3. Use https://developers.google.com/gmail/api/v1/reference/users/messages/send to send an email using thread-id.
4.enjoy coding.

add e-mail recipient into php script

CC or forward is not possible to set on mailserver for authorisation mail sent by Joomla itself, but we'd like to store these e-mails.
Question is: how to set it in php of specific plugin? (plugin is sending these e-mails)
code:
// send auth email to user who signed ...
if ($signature_verification = (int)$this->settings->get('security.signature_verification', 0)) {
// unpublished, visitor must verify it first
$this->db->set('published', 0);
$config = JFactory::getConfig();
$from = $config->get('mailfrom', '');
$fromname = $config->get('fromname', '');
$recipient = (string)$this->db->get('email', '');
When I replace last line wth: $recipient = ('my#email.com'), then I get that message, but i want one for visitor and copy for me.
Thanks for advice
OK, actually this piece of code initiates sending of that mail:
if (
$this->sendMail(
$from,
$fromname,
$recipient,
$subject,
$body
) !== true
) {
throw new phpmailerException(JText::_('PLG_CONTENT_CDPETITIONS_EMAIL_SEND_FAILED'), 500);
}
When I make copy of that code, paste it below, and replace $recipient with my e-mail, it works: I have the same message delivered on both adresses. But I need it have it like CC (carbon copy) and have original recipient adress in header of mail, which is delivered to me.
Use the built in mailer methods for Joomla:
$msg = "This is my email message.";
$subject = "Database Update Email";
$to = (string)$this->db->get('email');
$config = JFactory::getConfig();
$fromemail = $config->get('mailfrom');
$fromname = $config->get('fromname');
$from = array($fromemail,$fromname);
$mailer = JFactory::getMailer();
$mailer->setSender($from);
$mailer->addRecipient($to);
$mailer->addRecipient('you1#yourdomain.com');
$mailer->addRecipient('you2#yourdomain.com');
$mailer->addCC('you3#yourdomain.com');
$mailer->addBCC('you4#yourdomain.com');
$mailer->setSubject($subject);
$mailer->setBody($msg);
$mailer->isHTML();
$mailer->send();
This should use PHP to send an HTML email to whomever you want and copy other users on the email through either direct send, CC, or BCC depending on which method you use.

PhpMailer sends to gmail but not to hotmail accounts

I know this has been asked before, but i wanted to show my case because i tried to solve it with other answers but i coundt solve it.
I m trying to do a Contact form but when i choose the email of the receiver if i choose #hotmail.com or #advancedinstitute.cl they dont receive the email.
I tried to choose another type of email like gmail, yahoo or terra and the email arrives without problem.
I know it must be because the spam policies of those servers but i dont know how to fix it.
This is my code of the phpMailer:
$mail = new PHPMailer();
$mail->Host = "localhost";
$mail->Hostname = "Advanced Institute";
$mail->From = $_POST["email"];
$mail->FromName = $_POST["email"];
$mail->Subject = "Quiero contactarme con Advanced Institute";
$mail->AddAddress("carmeng.advanced#terra.cl","Advanced Institute");
$mail->AddReplyTo($_POST["email"]);
$mail->IsHTML(true); // El correo se envía como HTML
$body = '<html><body><div>Nombre : '.$_POST["nombre"].'<br> Email : '.$_POST["email"].'<br> Telefono : '.$_POST["telefono"].'<br> Asunto : '.$_POST["asunto"].'<br> Comentarios: '.$_POST["comentarios"].'<br> </div></body></html>';
$mail->Body = $body;
if(!$mail->Body) {
echo 'Error: ' . $mail->ErrorInfo;
}else{
$mail->Send();
I hope my english was enough for you guys to understand my problem.
THANK YOU IN ADVANCED
You probably need to set up SPF records for the domain name.
Further information: http://en.wikipedia.org/wiki/Sender_Policy_Framework
Recommended SPF Wizard http://www.kitterman.com/spf/validate.html
Also the from address should be from the sending domain name, not user input.

send email using gmail-api and google-api-php-client

I am using https://github.com/google/google-api-php-client and I want to send a test email with a user's authorized gmail account.
This is what I have so far:
$msg = new Google_Service_Gmail_Message();
$msg->setRaw('gp1');
$service->users_messages->send('me', $msg);
This results in a bounce email because I have no clue how to set the raw message. I see the bounce in the inbox of my authenticated user. I want to learn how to set values for 'To', 'Cc', 'Bcc', 'Subject', and 'Body' of the email. I believe I will need to do a 64 encoding on that raw data as well. And I might want to use some html in the body of my email.
Please help to provide a working example of sending an email using the gmail-api and the google-api-php-client.
Here is the bounced email in the inbox:
Bounce -nobody#gmail.com- 12:58 PM (7 minutes ago)
to me
An error occurred. Your message was not sent.
‚ Date: Thu, 24 Jul 2014 10:58:30 -0700 Message-Id: CABbXiyXhRBzzuaY82i9iODEiwxEJWO1=jCcDM_TH-
I asked a more specific question which has led me to an answer. I am now using PHPMailer to build the message. I then extract the raw message from the PHPMailer object. Example:
require_once 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$subject = "my subject";
$msg = "hey there!";
$from = "myemail#gmail.com";
$fname = "my name";
$mail->From = $from;
$mail->FromName = $fname;
$mail->AddAddress("tosomeone#somedomain.com");
$mail->AddReplyTo($from,$fname);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$m = new Google_Service_Gmail_Message();
$data = base64_encode($mime);
$data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
$m->setRaw($data);
$service->users_messages->send('me', $m);
I've used this solution as well, worked fine with a few tweaks:
When creating the PHPMailer object, the default encoding is set to '8bit'.
So I had to overrule that with:
$mail->Encoding = 'base64';
The other thing i had to do was tweaking the MIME a little to make it POST ready for the Google API, I've used the solution by ewein:
Invalid value for ByteString error when calling gmail send API with base64 encoded < or >
Anyway, this is how I've solved your problem:
//prepare the mail with PHPMailer
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "base64";
//supply with your header info, body etc...
$mail->Subject = "You've got mail!";
...
//create the MIME Message
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
//create the Gmail Message
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);
$message = $service->users_messages->send('me',$message);
Perhaps this is a bit beyond the original question, but at least in my case that "test email" progressed to regularly sending automated welcome emails "from" various accounts. Though I've found much that is helpful here, I've had to cobble things together from various sources.
In the hope of helping others navigate this process, here's a distilled version of what I came up with.
A few notes on the following code:
This assumes that you've jumped through the hoops to create and download the JSON for a Google Service Account (under credentials in the developer dashboard it's the bottom section.)
For clarity, I've grouped all the bits you should need to set in the "Replace this with your own data" section.
That me in the send() method is a key word that means something like "send this email with the current account."
// This block is only needed if you're working outside the global namespace
use \Google_Client as Google_Client;
use \Google_Service_Gmail as Google_Service_Gmail;
use \Google_Service_Gmail_Message as Google_Service_Gmail_Message;
// Prep things for PHPMailer
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
// Grab the needed files
require_once 'path/to/google-api/vendor/autoload.php';
require_once 'path/to/php-mailer/Exception.php';
require_once 'path/to/php-mailer/PHPMailer.php';
// Replace this with your own data
$pathToServiceAccountCredentialsJSON = "/path/to/service/account/credentials.json";
$emailUser = "sender#yourdomain.com"; // the user who is "sending" the email...
$emailUserName = "Sending User's Name"; // ... and their name
$emailSubjectLine = "My Email's Subject Line";
$recipientEmail = "recipient#somdomain.com";
$recipientName = "Recipient's Name";
$bodyHTML = "<p>Paragraph one.</p><p>Paragraph two!</p>";
$bodyText = "Paragraph one.\n\nParagraph two!";
// Set up credentials and client
putenv("GOOGLE_APPLICATION_CREDENTIALS={$pathToServiceAccountCredentialsJSON}");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
// We're only sending, so this works fine
$client->addScope(Google_Service_Gmail::GMAIL_SEND);
// Set the user we're going to pretend to be (Subject is confusing here as an email also has a "Subject"
$client->setSubject($emailUser);
// Set up the service
$mailService = new Google_Service_Gmail($client);
// We'll use PHPMailer to build the raw email (since Google's API doesn't do that) so prep it
$mailBuilder = new PHPMailer();
$mailBuilder->CharSet = "UTF-8";
$mailBuilder->Encoding = "base64";
// Not set up the email you want to send
$mailBuilder->Subject = $emailSubjectLine;
$mailBuilder->From = $emailUser;
$mailBuilder->FromName = $emailUserName;
try {
$mailBuilder->addAddress($recipientEmail, $recipientName);
} catch (Exception $e) {
// Handle any problems adding the email address here
}
// Add additional recipients, CC, BCC, ReplyTo, if desired
// Then add the main body of the email...
$mailBuilder->isHTML(true);
$mailBuilder->Body = $bodyHTML;
$mailBuilder->AltBody = $bodyText;
// Prep things so we have a nice, raw message ready to send via Google's API
try {
$mailBuilder->preSend();
$rawMessage = base64_encode($mailBuilder->getSentMIMEMessage());
$rawMessage = str_replace(['+', '/', '='], ['-', '_', ''], $rawMessage); // url safe
$gMessage = new Google_Service_Gmail_Message();
$gMessage->setRaw($rawMessage);
// Send it!
$result = $mailService->users_messages->send('me', $gMessage);
} catch (Exception $e) {
// Handle any problems building or sending the email here
}
if ($result->labelIds[0] == "SENT") echo "Message sent!";
else echo "Something went wrong...";
Create the mail with phpmailer works fine for me in a local enviroment. On production I get this error:
Invalid value for ByteString
To solve this, remove the line:
$mail->Encoding = 'base64';
because the mail is two times encoded.
Also, on other questions/issues I found the next:
use
strtr(base64_encode($val), '+/=', '-_*')
instead of
strtr(base64_encode($val), '+/=', '-_,')
I Used https://developers.google.com/gmail/api/v1/reference/users/messages/send
and able to send email successfully.

PHP to SMS - Change the "From Name"

I am writing a small web app that uses PHP code to email a phone number (the person with the phone number sees it as a text message, and not as an email of course). Every phone service has email to text. For example, Verizon in the US uses #vtext.com.
My problem is this, the FROM on the SMS always says "6245" which is apparently standard for SMS's from the Verizon email domain (vtext.com). Can I change this in code with a more human readable From rather than this seemingly random number?
Here is my code using PHP mailer:
$from = $_POST['email'];
$from = filter_var($from, FILTER_SANITIZE_EMAIL);
$message .= $guest . ' waiting at Office. Checked in at ';
$message .= strftime("%l:%M %p (%A %b %e, %Y)", time());
// PHP SMTP mail version
$mail = new PHPMailer();
$result = mysql_query("SELECT * FROM users WHERE onduty = 1");
$recipients = array();
while ($row = mysql_fetch_array($result)) {
$recipients[] = $row['phone'] . $row['carrier'];
}
foreach ($recipients as $email) {
$mail -> AddAddress($email);
}
$from_name = "Riverstone Notification";
$subject = "Person in Office";
$mail -> IsSMTP();
$mail -> Host = "relay-hosting.secureserver.net";
$mail -> Port = 25;
$mail -> SMTPAuth = false;
$mail -> Username = "EMAIL_USER";
$mail -> Password = "EMAIL_PASS";
$mail -> FromName = $from_name;
$mail -> From = $from;
$mail -> Subject = $subject;
$mail -> Body = $message;
$result = $mail -> Send();
No, when you're using the Verizon's service, that no. is going to be standard, because it comes from Verizon's SMS gateway.
You would have to get a paid service, if you need flexibility on that.
Maybe the same thing your dealing with,
When I use to send sms (Tmobile = tmomail.net) If I don't put an email with the same ending as my website in the $headers, it adds My hosting user id at secureserver.net... Bunch of noise.... When I add From:3333#abc.com and say my website is abc.com it keeps it clean. From the research I've gotten, its does that to try and stop spam, so it looks for something to say this email is coming from this site type of thing...

Categories