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...
Related
I can actually send the emails but I want to change the email from name. When I send emails, it comes into the receiver mailbox with my mail id. I want to change that to some other names. Kindly help me to do that. This is my code
<?php
$mailto = $_POST['mail_to'];
$mailSub = $_POST['mail_sub'];
$mailMsg = $_POST['mail_msg'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.gmail.com";
$mail ->Port = 465; // or 587
$mail ->IsHTML(true);
$mail ->Username = "dkdev006#gmail.com";
$mail ->Password = "password";
$mail ->SetFrom("Kreatz.in");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}
Look at the docs on this method.
You have $mail ->SetFrom("Kreatz.in");, which is neither a "nice" name nor an email address, so I don't know what you're trying to do there. You probably want to do this:
$mail ->setFrom('dkdev006#gmail.com', 'Kreatz.in');
Bear in mind that you're using gmail to send through, so you are not allowed to set arbitrary from addresses, though you can create fixed aliases in gmail prefs.
You're also using an old version of PHPMailer, and have based your code on an obsolete example, so get the latest version.
I'm trying to send out E-mails using a PowerShell script, my company's Virus scan (McAfee) is blocking port 25. I know that I can go into McAfee's setting and uncheck the "prevent mass E-mail.. option", I also know that I can call Outlook within my script to send E-mail while user is logged in (which is too intrusive). My question is, is there any other way to send e-mails using PowerShell without using any of the options I mentioned above.
Thank you.
$From = "name#name.com"
$To = "name#name.com"
$Body = Test"
$Subject2 = "Hi"
$SMTPServer ="smtp.com"
Send-MailMessage -From $From -To $To -SmtpServer $SMTPServer -Subject $Subject1 -Body $Body'
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "name#name.com"
$Mail.Subject = "Hi"
$Mail.Body = "Hello"[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Mail.Send() '
Use .NET classes
You could create a [MailMessage] object manually and send it with the [SmtpClient] class, but this may fall foul of the McAfee software in the same way that Send-MailMessage does.
$From = "name#name.com"
$To = "name#name.com"
$Body = "Test"
$Subject2 = "Hi"
$SMTPServer ="smtp.com"
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $Subject2
$message.Body = $Body
$smtpclient = New-Object Net.Mail.SmtpClient($SMTPServer)
$smtpclient.Send($message)
http://exchangeserverpro.com/powershell-send-html-email/
Use Exchange Web Services
In order to use EWS you will need the Microsoft.Exchange.WebServices.dll file, which you may find somewhere such as the following:
C:\Program Files\Microsoft\Exchange\Web Services\1.0
C:\Program Files\Microsoft\Exchange\Web Services\2.2
Download from Microsoft
An example function implementation of this from Mike Pfeiffer's blog is here (and linked below):
Function Send-EWSMailMessage {
[CmdletBinding()]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Object]
$PrimarySmtpAddress,
[Parameter(Position=1, Mandatory=$true)]
[System.String]
$Subject,
[Parameter(Position=2, Mandatory=$true)]
[System.String]
$Body
)
BEGIN {
Add-Type -Path "C:\bin\Microsoft.Exchange.WebServices.dll"
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$user = [ADSI]"LDAP://<SID=$sid>"
}
PROCESS {
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$service.AutodiscoverUrl($user.Properties.mail)
if($PrimarySmtpAddress -is [Microsoft.Exchange.Data.SmtpAddress]) {
$Recipient = $PrimarySmtpAddress.ToString()
} else {
$Recipient = $PrimarySmtpAddress
}
$mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$mail.Subject = $Subject
$mail.Body = $Body
[Void]$mail.ToRecipients.Add($Recipient)
$mail.SendAndSaveCopy()
}
END {
}
}
Sending Email with PowerShell and the EWS Managed API
The following link may be useful, but isn't specifically aimed at PowerShell implementations:
Creating and sending email messages by using the EWS Managed API 2.0
Does the SMTP server you are using support encrypted connection ? If so try:
Send-MailMessage -To $to -From $from -SmtpServer $smtp -UseSsl -Port "587"
I would test it but I don't have McAfee.
I am trying to send an email to user and admin using php-mailer.But when I send the mail, the user Acknowledgement mail is also getting sent to the admin. The e mails getting sent are in proper format. What is the issue here?
My Code is Here:
$name = "User Name";
$email = "user#gmail.com";
$mobile = 'User Phone No.';
$body = "<div><div>Name: <b>$name</b></div> <div>Email: <b>$email</b></div> <div>Mobile: <b>$mobile</b></div></div>";
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail -> isSMTP();
function sendEmail($mail, $from, $password, $to, $body, $subject) {
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail -> SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail -> Debugoutput = 'html';
//Set the hostname of the mail server
$mail -> Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail -> Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail -> SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail -> SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail -> Username = $from;
//Password to use for SMTP authentication
$mail -> Password = $password;
//Set who the message is to be sent from
$mail -> setFrom($from, $from);
//Set who the message is to be sent to
$mail -> addAddress($to, $to);
//Set the subject line
$mail -> Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail -> msgHTML($body);
//Replace the plain text body with one created manually
$mail -> AltBody = 'This is a plain-text message body';
$sucess = $mail -> send();
// //send the message, check for errors
// if (!$sucess) {
// echo "Mailer Error: " . $mail -> ErrorInfo;
// } else {
// echo "Message sent!";
// }
}
$from = "noreplyadmin#gmail.com";
$maiAdmin = "admin#gmail.com";
$password = "password";
$subject = "Enquiry";
// Send Mail to admin
sendEmail($mail, $from, $password, $maiAdmin, $body, $subject);
// Send Mail to User
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($mail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
You are using the same instance and have not cleared the previous out of the PHPMailer $mail instance. You can either clear the old data out of the PHPMailer Instance or do this:
$adminmail = new PHPMailer();
$adminmail -> isSMTP();
$usermail = new PHPMailer();
$usermail -> isSMTP();
sendEmail($adminmail, $from, $password, $maiAdmin, $body, $subject);
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($usermail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
Whatever floats your boat I suppose.
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.
I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}