Other Options to Send E-mail Using PowerShell - php

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.

Related

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.

How to change the name text of sender when sending mail with Swift Mailer v3.3.2

I'm using Swift Mailer v3.3.2 to send emails from my app and I need to be able to change the text of the sender.
My code looks like this:
//Sending email
$swift = email::connect();
$email_message = new View('email/email_template');
$subject = "Subject here";
$from = "subdomain#domain.org";
$email_message->content_email = new View('email/content/signup');
$email_message->content_email->user = $user;
$message = $email_message;
$recipients = new Swift_RecipientList;
$recipients->addTo($user->email);
// Build the HTML message
$message = new Swift_Message($subject, $message, "text/html");
if ($swift->send($message, $recipients, $from)) {
;
} else {
;
}
$swift->disconnect();
I want to be able to set the name text of the sender as 'Senders_Name Senders_Surname', even though the sender is still subdomain#domain.org
Any clue on how to do that?
After line
$message = new Swift_Message($subject, $message, "text/html");
you should add
$message->setFrom(new Swift_Address($from , 'Senders_Name Senders_Surname'));
The whole working script below (I've just tested it in 3.3.2 version):
<?php
require ('lib/Swift.php');
require('lib/Swift/Connection/SMTP.php') ;
$smtp = new Swift_Connection_SMTP();
$smtp->setServer('server');
$smtp->setUsername('username');
$smtp->setPassword('password');
$smtp->setEncryption($smtp::ENC_SSL);
$smtp->setPort(465);
$swift = new Swift($smtp);
$swift->connect();
$subject = "Subject here";
$from = 'test#email.com';
$message = 'test message';
$recipients = new Swift_RecipientList;
$recipients->addTo('mymail');
// Build the HTML message
$message = new Swift_Message($subject, $message, "text/html");
$message->setFrom(new Swift_Address($from , 'Senders_Name Senders_Surname'));
if ($swift->send($message, $recipients, $from)) {
;
} else {
;
}
$swift->disconnect();
Below image how the sender is displayed in email client (Thunderbird for me) so it works fine. If you test it in your email client make sure you don't have account from set as one of your mail accounts. In that case email client shows for example "Me" or sth else. The best for test just fill in addTo with your email and smtp settings and leave from and other parts unchanged

Pear Can't send email on Shared Hosting

I was working on a project for a client, it selects an email from a database and then sends an email to that address. Everything worked fine on my VPS server running CentOS 6, but when migrating to their shared hosting the program will no longer send the email. It will select the correct addresses, but no message will be sent, I've already installed Pear Mail and Mail_mime. Any thoughts?
This code connects to the server:
$headers['From'] = 'mail#openmailbox.org';
$headers['To'] = 'mail#openmailbox.org';
$headers['Subject'] = $asunto;
$params['host'] = 'smtp.openmailbox.org';
$params['port'] = '25';
$params['auth'] = 'PLAIN';
$params['username'] = 'mail#openmailbox.org';
$params['password'] = 'password';
This code selects the recipients:
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$addresses[] = $row['email'];
}
$recipients = implode(", ", $addresses);
Hope you can help me!
Here, this is my email sending code
$mail =& Mail::factory('smtp', $params);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail->send($recipients, $headers, $body);
Well, I solved it. I replaced pear mail with the default mail function.

Swiftmailer remove attachment after send

I am trying to remove the attachment files after sending an email with Symfony 2.1 and Swiftmailer but if I delete the file before returning a response object (a redirect), the email does not send.
I suppose this is because symfony sends the email in the response, so when the email has send the attachment has been removed already.
For example:
<?php
// DefaultCotroller.php
$message = \Swift_Message::newInstance($subject)
->setFrom('no-reply#dasi.es')
->setTo($emails_to)
->setBody($body, 'text/html')
->attach(\Swift_Attachment::fromPath('backup.rar'));
$this->get('mailer')->send();
unlink('backup.rar'); // This remove the file but doesn't send the email!
return $this->redirect($this->generateUrl('homepage'));
An option is to create a crontab to clean the files, but I prefer not using it.
Thanks!
You can look at the code that processes memory spools here:
https://github.com/symfony/SwiftmailerBundle/blob/master/EventListener/EmailSenderListener.php
This is used to batch the emails to be sent.
You can add this after your send() call and before your unlink() call to mimic the behavior of sending an email
$transport = $this->container->get('mailer')->getTransport();
$spool = $transport->getSpool();
$spool->flushQueue($this->container->get('swiftmailer.transport.real'));
I am not sure, but the message spool might cause this problem. In SF2 memory spool is used by default, which means the messages are being sent on the kernel terminate event.
So you'd have to flush the spool before deleting the file.
If this is the cause of your problem, look here for a well explained solution:
http://sgoettschkes.blogspot.de/2012/09/symfony-21-commands-and-swiftmailer.html
In order to complete the very good answer of james_t, if you use multiple mailers some changes are needed.
Replace
// Default mailer
$mailer = $this->container->get('mailer');
$subject = '...';
$from = '...';
$to = '...';
$body = '...';
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html')
;
// Put e-mail in spool
$result = $mailer->send($message);
// Flush spool queue
$transport = $mailer->getTransport();
$spool = $transport->getSpool();
$realTransport = $this->container->get('swiftmailer.transport.real')
$spool->flushQueue($realTransport);
By
// Custom mailer
$mailerServiceName = 'myCustomMailer';
$customMailer = $this->container->get("swiftmailer.mailer.".$mailerServiceName);
$subject = '...';
$from = '...';
$to = '...';
$body = '...';
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html')
;
// Put e-mail in spool
$result = $customMailer->send($message);
// Flush spool queue
$transport = $customMailer->getTransport();
$spool = $transport->getSpool();
$realTransport = $this->container->get('swiftmailer.mailer.'.$mailerServiceName.'.transport.real');
$spool->flushQueue($realTransport);

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