Viewed   148 times

How can i send an Email using PHP at windows Azure?

i am using simple mail function:

$to .= 'email-Id';
$subject = " Test Subject";

$headers  = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'To: '.$to.'' . "rn";
$headers .= 'From: '.$name. '<'.$email.'>' . "rn";

echo $message='email text here';
@mail($to, $subject, $message, $headers);

 Answers

2

To send emails using PHP you have a few options:

Option 1: Use SMTP

You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the SMTP value to an external SMTP server you can use. SMTP servers are not part of the Windows Azure features at the moment.

[mail function]
SMTP = mail.mycompany.com

Option 2: Use sendmail

You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the sendmail_path value to the sendmail executable.

[mail function]
sendmail_path = "C:wampsendmailsendmail.exe -t"

Since sendmail doesn't exist in Windows, you'll need to use the fake sendmail for windows: http://glob.com.au/sendmail/

Option 3: Use a mail/smtp service

You could use a service like SendGrid to send your emails (they have an offer for Azure users: http://sendgrid.com/azure.html). They'll take care of sending out the email, you'll just need to call the REST api:

$sendgrid = new SendGrid('username', 'password');
$mail = new SendGridMail();
$mail->addTo('[email protected]')->
       setFrom('[email protected]')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('<strong>Hello World!</strong>');
$sendgrid->smtp->send($mail);
Wednesday, September 7, 2022
 
keews
 
1

Azure has no such facility to generate a zip file for a bundle of blobs for you. Azure Storage is just... storage. You'll need to download each of your blobs via php sdk (or directly via api if you so choose).

And if you want the content zip'd, you'll need to zip the content prior to storing in blob storage.

Your code (in your question) won't work as-is, since get_file_contents() expects to work with file I/O, and that's not how to interact with blobs. Rather, you'd do something like this:

$getBlobResult = $blobClient->getBlob("<containername>", "<blobname>");
file_put_contents("<localfilename>", $getBlobResult->getContentStream());
Friday, September 2, 2022
 
woerndl
 
4

@Gaurav: this is true, but occurs only on the second loop (I forgot to add the two lines when posting my code).

I've been trying to find out what's wrong for at least half a day. Finally I got it: It is due to an older version of the Windows Azure PHP SDK which has a "bug". I stumbled across this "bug" at the bottom of this thread: https://github.com/Azure/azure-sdk-for-php/issues/702 The older version of Windows Azure SDK uses _encodeODataUriValue which seems to be unnecessary.

Monday, August 8, 2022
 
bitbonk
 
1

First download the JavaMail API and make sure the relevant jar files are in your classpath.

Here's a full working example using GMail.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Main {

    private static String USER_NAME = "*****";  // GMail user name (just the part before "@gmail.com")
    private static String PASSWORD = "********"; // GMail password
    private static String RECIPIENT = "[email protected]";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

Naturally, you'll want to do more in the catch blocks than print the stack trace as I did in the example code above. (Remove the catch blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)


Thanks to @jodonnel and everyone else who answered. I'm giving him a bounty because his answer led me about 95% of the way to a complete answer.

Thursday, November 10, 2022
 
5

For sending out the actual e-mail I would recommend using the PHPMailer library, it makes everything much easier.

Wednesday, October 12, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :